可以从UserForm2的代码访问Excel UserForm1的动态对象?不是来自模块的代码



我有一个"类";我的动态标签的模块whit声明和事件

Option Explicit
Public WithEvents ClassLabel As MSForms.Label
Public Sub ClassLabel_Click() 
If InStr(ClassLabel.Name, "LB_Label") Then
Set CurrentLabel = ClassLabel   
'Bla Bla Bla
End If
End Sub

我创建了15个动态标签;FR_运行时间";UserForm1的框架,我已经将其保存在一个普通模块1的Controls_Init((Sub的数组中,如下所示

Option Explicit
Public gArrayClassLabel() As New Class
Public CurrentLabel As MSForms.Label
Public Sub Controls_Init()
Dim Row As Integer
Dim nRow As Integer
Dim H As Integer
Dim LB_Label As MSForms.Label        
nRow = 15
H = 30
ReDim gArrayClassLabel(1 To nRow)        
For Row = 1 To nRow
Set LB_Label = UserForm1.FR_Runtime.Controls.Add("Forms.Label.1")
With LB_Label
.Name = "LB_Label" & Row
.Caption = "    Label " & Row & ", 2"
.Left = 100
.Top = H
.Width = 75
.Height = 18
.ForeColor = vbRed
.BackColor = vbWindowBackground
.BorderStyle = fmBorderStyleSingle
.SpecialEffect = fmSpecialEffectSunken
End With
Set gArrayClassLabel(Row).ClassLabel = LB_Label    
H = H + 30
Next Row
End Sub

从我的UserForm1.FR_Runtime_Exit((事件(或任何其他UserForm1,Module1代码(,我可以访问第五个动态标签,如下

Private Sub FR_Runtime_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Set glbCurrentLabel = gArrayClassLabel(5).ClassLabel
With UserForm1.TextBox1
.Text = Trim$(glbCurrentLabel.Caption)
.Left = glbCurrentLabel.Left
.Top = glbCurrentLabel.Top
.SelStart = 0
.SelLength = Len(glbCurrentLabel.Caption)
End With
End Sub

从UserForm2,我可以访问在设计时创建的标签:UserForm1.Label3.Caption = "This is an design time Label"

我可以从UserForm2的子中访问gArrayClassLabel(5(.ClassLabel吗?不是来自Module1代码。

感谢Tragamor,我没有考虑类模块的属性。这是新的";类";模块

Public WithEvents ClassLabel As MSForms.Label
Private sContent As String
'new Get property
Property Get Content() As String
Content= sContent 
End Property
'new Let property
Property Let Content(NumLbl As String)
sContent = gArrayClassLabel(NumLbl).ClassLabel.Caption
End Property
Public Sub ClassLabel_Click() 
If InStr(ClassLabel.Name, "LB_Label") Then
Set CurrentLabel = ClassLabel   
'Bla Bla Bla
End If
End Sub

我可以从代码中的任何地方访问第五个动态标签Caption,如下所示

Dim NumDynamicLabel As New Class
'I use Let Content property to obtain the fifth dynamic label Caption
NumDynamicLabel.Content = 5 

Dim CaptionDynamicLabel As String
'I use the Get Content Property to retrieve Caption of fifth dynamic label 
CaptionDynamicLabel = NumDynamicLabel.Content 

最新更新