将事件侦听器添加到多个组合框
Adding event listener to several comboboxes
我基本上是在尝试根据组合框采用的值在用户窗体上显示不同的文本框集。我创建了一个名为 CControlEvents 的 class 模块,我在其中描述了当我更改组合框的值时应该发生的事件:
Private WithEvents mclsCbx As MSForms.ComboBox
Private WithEvents UnitmclsTbx As MSForms.TextBox
Private WithEvents UnitmclsTbxLabel As MSForms.Label
Private WithEvents SpecMinMoymclsTbxLabel As MSForms.Label
Private WithEvents SpecMaxMoymclsTbxLabel As MSForms.Label
Private WithEvents SpecMinIndmclsTbxLabel As MSForms.Label
Private WithEvents SpecMaxIndmclsTbxLabel As MSForms.Label
Private WithEvents SpecMinMoymclsTbx As MSForms.TextBox
Private WithEvents SpecMaxMoymclsTbx As MSForms.TextBox
Private WithEvents SpecMinIndmclsTbx As MSForms.TextBox
Private WithEvents SpecMaxIndmclsTbx As MSForms.TextBox
Private WithEvents ListmclsTbxLabel As MSForms.Label
Private WithEvents ListmclsTbx As MSForms.TextBox
Private mMyProperty As Integer
Public Property Set Cbx(ByVal clsCbx As MSForms.ComboBox): Set mclsCbx = clsCbx: End Property
Public Property Get Cbx() As MSForms.ComboBox: Set Cbx = mclsCbx: End Property
'Property pour les textbox et labels
'TextBox d'unité
Public Property Set UnitTbx(ByVal clsTbx As MSForms.TextBox): Set UnitmclsTbx = clsTbx: End Property
Public Property Get UnitTbx() As MSForms.TextBox: Set UnitTbx = UnitmclsTbx: End Property
'Label d'unité
Public Property Set UnitTbxLabel(ByVal clsTbx As MSForms.Label): Set UnitmclsTbxLabel = clsTbx: End Property
Public Property Get UnitTbxLabel() As MSForms.Label: Set UnitTbxLabel = UnitmclsTbxLabel: End Property
'TextBox de spécification minimum moyenne
Public Property Set SpecMinMoyTbx(ByVal clsTbx As MSForms.TextBox): Set SpecMinMoymclsTbx = clsTbx: End Property
Public Property Get SpecMinMoyTbx() As MSForms.TextBox: Set SpecMinMoyTbx = SpecMinMoymclsTbx: End Property
'Label de spécification minimum moyenne
Public Property Set SpecMinMoyTbxLabel(ByVal clsTbx As MSForms.Label): Set SpecMinMoymclsTbxLabel = clsTbx: End Property
Public Property Get SpecMinMoyTbxLabel() As MSForms.Label: Set SpecMinMoyTbxLabel = SpecMinMoymclsTbxLabel: End Property
'Label de spécification maximum moyenne
Public Property Set SpecMaxMoyTbxLabel(ByVal clsTbx As MSForms.Label): Set SpecMaxMoymclsTbxLabel = clsTbx: End Property
Public Property Get SpecMaxMoyTbxLabel() As MSForms.Label: Set SpecMaxMoyTbxLabel = SpecMaxMoymclsTbxLabel: End Property
'TextBox de spécification max moy
Public Property Set SpecMaxMoyTbx(ByVal clsTbx As MSForms.TextBox): Set SpecMaxMoymclsTbx = clsTbx: End Property
Public Property Get SpecMaxMoyTbx() As MSForms.TextBox: Set SpecMaxMoyTbx = SpecMaxMoymclsTbx: End Property
'TextBox de spécification minimum individuelle
Public Property Set SpecMinIndTbx(ByVal clsTbx As MSForms.TextBox): Set SpecMinIndmclsTbx = clsTbx: End Property
Public Property Get SpecMinIndTbx() As MSForms.TextBox: Set SpecMinIndTbx = SpecMinIndmclsTbx: End Property
'Label de spécification minimum individuelle
Public Property Set SpecMinIndTbxLabel(ByVal clsTbx As MSForms.Label): Set SpecMinIndmclsTbxLabel = clsTbx: End Property
Public Property Get SpecMinIndTbxLabel() As MSForms.Label: Set SpecMinIndTbxLabel = SpecMinIndmclsTbxLabel: End Property
'Label de spécification maximum individuelle
Public Property Set SpecMaxIndTbxLabel(ByVal clsTbx As MSForms.Label): Set SpecMaxIndmclsTbxLabel = clsTbx: End Property
Public Property Get SpecMaxIndTbxLabel() As MSForms.Label: Set SpecMaxIndTbxLabel = SpecMaxIndmclsTbxLabel: End Property
'TextBox de spécification max individuelle
Public Property Set SpecMaxIndTbx(ByVal clsTbx As MSForms.TextBox): Set SpecMaxIndmclsTbx = clsTbx: End Property
Public Property Get SpecMaxIndTbx() As MSForms.TextBox: Set SpecMaxIndTbx = SpecMaxIndmclsTbx: End Property
'TextBox de liste déroulante
Public Property Set ListTbx(ByVal clsTbx As MSForms.TextBox): Set ListmclsTbx = clsTbx: End Property
Public Property Get ListTbx() As MSForms.TextBox: Set ListTbx = ListmclsTbx: End Property
'Label de liste déroulante
Public Property Set ListTbxLabel(ByVal clsTbx As MSForms.Label): Set ListmclsTbxLabel = clsTbx: End Property
Public Property Get ListTbxLabel() As MSForms.Label: Set ListTbxLabel = ListmclsTbxLabel: End Property
Public Property Get MyProperty() As Integer
MyProperty = mMyProperty
End Property
Public Property Let Transition(Value As Integer)
mMyProperty = Value
End Property
Private Sub mclsCbx_Change()
If (Me.Cbx.Text = "NUM") Then
UnitTbx.Visible = True
UnitmclsTbxLabel.Visible = True
SpecMinMoymclsTbxLabel.Visible = True
SpecMinMoymclsTbx.Visible = True
SpecMaxMoymclsTbx.Visible = True
SpecMaxMoymclsTbxLabel.Visible = True
SpecMinIndmclsTbxLabel.Visible = True
SpecMinIndmclsTbx.Visible = True
SpecMaxIndmclsTbx.Visible = True
SpecMaxIndmclsTbxLabel.Visible = True
ListmclsTbx.Visible = False
ListmclsTbxLabel.Visible = False
Else
If (Me.Cbx.Text = "LIST") Then
UnitTbx.Visible = False
UnitmclsTbxLabel.Visible = False
SpecMinMoymclsTbxLabel.Visible = False
SpecMinMoymclsTbx.Visible = False
SpecMaxMoymclsTbx.Visible = False
SpecMaxMoymclsTbxLabel.Visible = False
SpecMinIndmclsTbxLabel.Visible = False
SpecMinIndmclsTbx.Visible = False
SpecMaxIndmclsTbx.Visible = False
SpecMaxIndmclsTbxLabel.Visible = False
ListmclsTbx.Visible = True
ListmclsTbxLabel.Visible = True
End If
End If
End Sub
在用户窗体的代码中,我动态添加了这样的组合框:
Set gclsControlEvents = New CControlEvents
'On attribue la comboBox à une propriété de la classe créée
Set gclsControlEvents.Cbx = oleCbx
'On attribue une zone de texte à une propriété de la classe créée
Set gclsControlEvents.UnitTbx = numUnitTextBox
'Label unité
Set gclsControlEvents.UnitTbxLabel = UnitmclsTbxLabel
'Label spéc moy min
Set gclsControlEvents.SpecMinMoyTbxLabel = SpecMinMoyTbxLabel
'Zone de texte spéc moy min
Set gclsControlEvents.SpecMinMoyTbx = SpecMinMoymclsTbx
'Label spéc max min
Set gclsControlEvents.SpecMaxMoyTbxLabel = SpecMaxMoyTbxLabel
'Zone de texte spéc moy max
Set gclsControlEvents.SpecMaxMoyTbx = SpecMaxMoymclsTbx
所以,我基本上是在用户窗体代码中添加控件并将它们分配给 class 的属性,然后我可以根据需要简单地 hide/show。问题是,当我尝试根据这个原则添加几个组合框时,使用 for 循环,它只适用于最后一个组合框。
编辑:for循环
For i = 1 To NewSheetModelColumnsCount
Set Lbl = Frame1.Controls.Add("Forms.Label.1", "lbl1")
Set txtB1 = Frame1.Add("Forms.TextBox.1")
'Create the combobox
Set oleCbx = Frame1.Add("Forms.ComboBox.1") 'Bug at this line
With oleCbx
.name = "list" & i
.Height = 15
.Width = 100
.Left = 70
.Top = 10 * i * 3
.AddItem "NUM"
.AddItem "LIST"
End With
With txtB1
.name = "chkDemo" & i
.Height = 15
.Width = 100
.Left = 230
.Top = 10 * i * 3
End With
Lbl.Caption = "Colonne n°" + CStr(i)
Lbl.Top = txtB1.Top
Lbl.Left = 10
Set lbl2 = Frame1.Controls.Add("Forms.Label.1", "lbl2")
lbl2.Caption = "Intitulé : "
lbl2.Top = txtB1.Top
lbl2.Left = 180
lbl2.Visible = True
lbl2.Width = 50
Set UnitmclsTbxLabel = UserForm1.Frame1.Controls.Add("Forms.Label.1", "UnitmclsTbxLabel")
Set ListmclsTbxLabel = UserForm1.Frame1.Controls.Add("Forms.Label.1", "ListmclsTbxLabel")
Set SpecMinMoyTbxLabel = UserForm1.Frame1.Controls.Add("Forms.Label.1", "SpecMinMoyTbxLabel")
Set SpecMaxMoyTbxLabel = UserForm1.Frame1.Controls.Add("Forms.Label.1", "SpecMinMoyTbxLabel")
Set SpecMinIndTbxLabel = UserForm1.Frame1.Controls.Add("Forms.Label.1", "SpecMinIndTbxLabel")
Set SpecMaxIndTbxLabel = UserForm1.Frame1.Controls.Add("Forms.Label.1", "SpecMinIndTbxLabel")
UnitmclsTbxLabel.Caption = "Unité : "
UnitmclsTbxLabel.Left = 360
UnitmclsTbxLabel.Visible = False
Set numUnitTextBox = UserForm1.Frame1.Add("Forms.TextBox.1")
Set SpecMinMoymclsTbx = UserForm1.Frame1.Add("Forms.TextBox.1")
Set SpecMaxMoymclsTbx = UserForm1.Frame1.Add("Forms.TextBox.1")
Set SpecMinIndmclsTbx = UserForm1.Frame1.Add("Forms.TextBox.1")
Set SpecMaxIndmclsTbx = UserForm1.Frame1.Add("Forms.TextBox.1")
Set ListTextBox = UserForm1.Frame1.Add("Forms.TextBox.1")
SpecMinMoymclsTbx.Width = 50
SpecMaxMoymclsTbx.Width = 50
SpecMinIndmclsTbx.Width = 50
SpecMaxIndmclsTbx.Width = 50
With numUnitTextBox
.name = "Unit" & i
.Height = 15
.Width = 50
.Left = 360 + UnitmclsTbxLabel.Width - 40
.Top = 10 * i * 3
.Value = "Unit"
End With
ListTextBox.Top = numUnitTextBox.Top
ListTextBox.Left = numUnitTextBox.Left + numUnitTextBox.Width - 40
ListTextBox.Visible = False
ListTextBox.Width = 200
ListmclsTbxLabel.Caption = "Options : "
ListmclsTbxLabel.Visible = False
ListmclsTbxLabel.Top = numUnitTextBox.Top
ListmclsTbxLabel.Left = 360
UnitmclsTbxLabel.Top = numUnitTextBox.Top
SpecMinMoyTbxLabel.Top = numUnitTextBox.Top
SpecMinMoyTbxLabel.Left = numUnitTextBox.Left + numUnitTextBox.Width + 5
SpecMinMoyTbxLabel.Caption = "Spéc min Moy : "
SpecMinMoyTbxLabel.Visible = False
SpecMinMoymclsTbx.Top = numUnitTextBox.Top
SpecMinMoymclsTbx.Visible = False
SpecMinMoymclsTbx.Left = SpecMinMoyTbxLabel.Left + SpecMinMoyTbxLabel.Width - 15
SpecMaxMoyTbxLabel.Visible = False
SpecMaxMoyTbxLabel.Caption = "Spéc max moy : "
SpecMaxMoyTbxLabel.Left = SpecMinMoymclsTbx.Left + SpecMinMoymclsTbx.Width
SpecMaxMoyTbxLabel.Top = numUnitTextBox.Top
SpecMaxMoymclsTbx.Visible = False
SpecMaxMoymclsTbx.Top = numUnitTextBox.Top
SpecMaxMoymclsTbx.Left = SpecMaxMoyTbxLabel.Left + SpecMaxMoyTbxLabel.Width
SpecMinIndTbxLabel.Top = numUnitTextBox.Top
SpecMinIndTbxLabel.Left = SpecMaxMoymclsTbx.Left + SpecMaxMoymclsTbx.Width + 5
SpecMinIndTbxLabel.Caption = "Spéc min Ind : "
SpecMinIndTbxLabel.Visible = False
SpecMinIndmclsTbx.Top = numUnitTextBox.Top
SpecMinIndmclsTbx.Visible = False
SpecMinIndmclsTbx.Left = SpecMinIndTbxLabel.Left + SpecMinIndTbxLabel.Width - 15
SpecMaxIndTbxLabel.Visible = False
SpecMaxIndTbxLabel.Caption = "Spéc max moy : "
SpecMaxIndTbxLabel.Left = SpecMinIndmclsTbx.Left + SpecMinIndmclsTbx.Width
SpecMaxIndTbxLabel.Top = numUnitTextBox.Top
SpecMaxIndmclsTbx.Visible = False
SpecMaxIndmclsTbx.Top = numUnitTextBox.Top
SpecMaxIndmclsTbx.Left = SpecMaxIndTbxLabel.Left + SpecMaxIndTbxLabel.Width
numUnitTextBox.Visible = False
Set gclsControlEvents = New CControlEvents
'On attribue la comboBox à une propriété de la classe créée
Set gclsControlEvents.Cbx = oleCbx
'On attribue une zone de texte à une propriété de la classe créée
Set gclsControlEvents.UnitTbx = numUnitTextBox
'Label unité
Set gclsControlEvents.UnitTbxLabel = UnitmclsTbxLabel
'Label spéc moy min
Set gclsControlEvents.SpecMinMoyTbxLabel = SpecMinMoyTbxLabel
'Zone de texte spéc moy min
Set gclsControlEvents.SpecMinMoyTbx = SpecMinMoymclsTbx
'Label spéc max min
Set gclsControlEvents.SpecMaxMoyTbxLabel = SpecMaxMoyTbxLabel
'Zone de texte spéc moy max
Set gclsControlEvents.SpecMaxMoyTbx = SpecMaxMoymclsTbx
'Label spéc moy min
Set gclsControlEvents.SpecMinIndTbxLabel = SpecMinIndTbxLabel
'Zone de texte spéc moy min
Set gclsControlEvents.SpecMinIndTbx = SpecMinIndmclsTbx
'Label spéc max min
Set gclsControlEvents.SpecMaxIndTbxLabel = SpecMaxIndTbxLabel
'Zone de texte spéc moy max
Set gclsControlEvents.SpecMaxIndTbx = SpecMaxIndmclsTbx
'Zone de texte des options de la liste déroulante
Set gclsControlEvents.ListTbx = ListTextBox
'Label Options liste déroulante
Set gclsControlEvents.ListTbxLabel = ListmclsTbxLabel
Let gclsControlEvents.Transition = i
Next i
编辑:
我试图通过创建一个包含尽可能多的 class CControlEvents 实例的变体数组来解决这个问题,因为我需要组合框,然后在每次迭代时使用索引 i-1 处的这个数组的元素,但它没有用,这是代码:
If (NewSheetModelColumnsCount > 0) Then
For i = 1 To 6
Controls("headerTextBox" + CStr(i)).Visible = True
Controls("CheckBox" + CStr(i)).Visible = True
Next i
For i = 11 To 16
Controls("label" + CStr(i)).Visible = True
Next i
Me.indicationLabel.Visible = False
'Name of the frame
With Me.Frame1
'This will create a vertical scrollbar
.ScrollBars = fmScrollBarsVertical
'Change the values of 2 as Per your requirements
.ScrollHeight = 30 * NewSheetModelColumnsCount + 50
.ScrollWidth = 30 * 50 + 50
'
End With
'Tentative
Dim classList As Variant
ReDim classList(0 To NewSheetModelColumnsCount - 1)
For i = 0 To NewSheetModelColumnsCount - 1
Set classList(i) = New CControlEvents
Next i
For i = 1 To NewSheetModelColumnsCount
Set Lbl = Frame1.Controls.Add("Forms.Label.1", "lbl1")
Set txtB1 = Frame1.Add("Forms.TextBox.1")
' Set ListBoxB1 = Frame1.Controls.Add("Forms.ComboBox.1")
' With ListBoxB1
' .Name = "list" & i
' .Height = 15
' .Width = 100
' .Left = 70
' .Top = 10 * i * 3
' .AddItem "NUM"
' .AddItem "LIST"
' End With
'Create the combobox
Set oleCbx = Frame1.Add("Forms.ComboBox.1") 'Bug at this line
With oleCbx
.name = "list" & i
.Height = 15
.Width = 100
.Left = 70
.Top = 10 * i * 3
.AddItem "NUM"
.AddItem "LIST"
End With
With txtB1
.name = "chkDemo" & i
.Height = 15
.Width = 100
.Left = 230
.Top = 10 * i * 3
End With
Lbl.Caption = "Colonne n°" + CStr(i)
Lbl.Top = txtB1.Top
Lbl.Left = 10
Set lbl2 = Frame1.Controls.Add("Forms.Label.1", "lbl2")
'Set txtB2 = Frame1.Add("Forms.TextBox.1")
' With txtB2
' .name = "unitTextBox" & i
' .Height = 15
' .Width = 100
' .Left = 240
' .Top = 10 * i * 3
' .Value = "txtB2"
' End With
lbl2.Caption = "Intitulé : "
lbl2.Top = txtB1.Top
lbl2.Left = 180
lbl2.Visible = True
lbl2.Width = 50
Set UnitmclsTbxLabel = UserForm1.Frame1.Controls.Add("Forms.Label.1", "UnitmclsTbxLabel")
Set ListmclsTbxLabel = UserForm1.Frame1.Controls.Add("Forms.Label.1", "ListmclsTbxLabel")
Set SpecMinMoyTbxLabel = UserForm1.Frame1.Controls.Add("Forms.Label.1", "SpecMinMoyTbxLabel")
Set SpecMaxMoyTbxLabel = UserForm1.Frame1.Controls.Add("Forms.Label.1", "SpecMinMoyTbxLabel")
Set SpecMinIndTbxLabel = UserForm1.Frame1.Controls.Add("Forms.Label.1", "SpecMinIndTbxLabel")
Set SpecMaxIndTbxLabel = UserForm1.Frame1.Controls.Add("Forms.Label.1", "SpecMinIndTbxLabel")
UnitmclsTbxLabel.Caption = "Unité : "
UnitmclsTbxLabel.Left = 360
UnitmclsTbxLabel.Visible = False
Set numUnitTextBox = UserForm1.Frame1.Add("Forms.TextBox.1")
Set SpecMinMoymclsTbx = UserForm1.Frame1.Add("Forms.TextBox.1")
Set SpecMaxMoymclsTbx = UserForm1.Frame1.Add("Forms.TextBox.1")
Set SpecMinIndmclsTbx = UserForm1.Frame1.Add("Forms.TextBox.1")
Set SpecMaxIndmclsTbx = UserForm1.Frame1.Add("Forms.TextBox.1")
Set ListTextBox = UserForm1.Frame1.Add("Forms.TextBox.1")
SpecMinMoymclsTbx.Width = 50
SpecMaxMoymclsTbx.Width = 50
SpecMinIndmclsTbx.Width = 50
SpecMaxIndmclsTbx.Width = 50
With numUnitTextBox
.name = "Unit" & i
.Height = 15
.Width = 50
.Left = 360 + UnitmclsTbxLabel.Width - 40
.Top = 10 * i * 3
.Value = "Unit"
End With
ListTextBox.Top = numUnitTextBox.Top
ListTextBox.Left = numUnitTextBox.Left + numUnitTextBox.Width - 40
ListTextBox.Visible = False
ListTextBox.Width = 200
ListmclsTbxLabel.Caption = "Options : "
ListmclsTbxLabel.Visible = False
ListmclsTbxLabel.Top = numUnitTextBox.Top
ListmclsTbxLabel.Left = 360
UnitmclsTbxLabel.Top = numUnitTextBox.Top
SpecMinMoyTbxLabel.Top = numUnitTextBox.Top
SpecMinMoyTbxLabel.Left = numUnitTextBox.Left + numUnitTextBox.Width + 5
SpecMinMoyTbxLabel.Caption = "Spéc min Moy : "
SpecMinMoyTbxLabel.Visible = False
SpecMinMoymclsTbx.Top = numUnitTextBox.Top
SpecMinMoymclsTbx.Visible = False
SpecMinMoymclsTbx.Left = SpecMinMoyTbxLabel.Left + SpecMinMoyTbxLabel.Width - 15
SpecMaxMoyTbxLabel.Visible = False
SpecMaxMoyTbxLabel.Caption = "Spéc max moy : "
SpecMaxMoyTbxLabel.Left = SpecMinMoymclsTbx.Left + SpecMinMoymclsTbx.Width
SpecMaxMoyTbxLabel.Top = numUnitTextBox.Top
SpecMaxMoymclsTbx.Visible = False
SpecMaxMoymclsTbx.Top = numUnitTextBox.Top
SpecMaxMoymclsTbx.Left = SpecMaxMoyTbxLabel.Left + SpecMaxMoyTbxLabel.Width
SpecMinIndTbxLabel.Top = numUnitTextBox.Top
SpecMinIndTbxLabel.Left = SpecMaxMoymclsTbx.Left + SpecMaxMoymclsTbx.Width + 5
SpecMinIndTbxLabel.Caption = "Spéc min Ind : "
SpecMinIndTbxLabel.Visible = False
SpecMinIndmclsTbx.Top = numUnitTextBox.Top
SpecMinIndmclsTbx.Visible = False
SpecMinIndmclsTbx.Left = SpecMinIndTbxLabel.Left + SpecMinIndTbxLabel.Width - 15
SpecMaxIndTbxLabel.Visible = False
SpecMaxIndTbxLabel.Caption = "Spéc max moy : "
SpecMaxIndTbxLabel.Left = SpecMinIndmclsTbx.Left + SpecMinIndmclsTbx.Width
SpecMaxIndTbxLabel.Top = numUnitTextBox.Top
SpecMaxIndmclsTbx.Visible = False
SpecMaxIndmclsTbx.Top = numUnitTextBox.Top
SpecMaxIndmclsTbx.Left = SpecMaxIndTbxLabel.Left + SpecMaxIndTbxLabel.Width
numUnitTextBox.Visible = False
'Set gclsControlEvents = New CControlEvents
'On attribue la comboBox à une propriété de la classe créée
Set classList(i - 1).Cbx = oleCbx
'On attribue une zone de texte à une propriété de la classe créée
Set classList(i - 1).UnitTbx = numUnitTextBox
'Label unité
Set classList(i - 1).UnitTbxLabel = UnitmclsTbxLabel
'Label spéc moy min
Set classList(i - 1).SpecMinMoyTbxLabel = SpecMinMoyTbxLabel
'Zone de texte spéc moy min
Set classList(i - 1).SpecMinMoyTbx = SpecMinMoymclsTbx
'Label spéc max min
Set classList(i - 1).SpecMaxMoyTbxLabel = SpecMaxMoyTbxLabel
'Zone de texte spéc moy max
Set classList(i - 1).SpecMaxMoyTbx = SpecMaxMoymclsTbx
'Label spéc moy min
Set classList(i - 1).SpecMinIndTbxLabel = SpecMinIndTbxLabel
'Zone de texte spéc moy min
Set classList(i - 1).SpecMinIndTbx = SpecMinIndmclsTbx
'Label spéc max min
Set classList(i - 1).SpecMaxIndTbxLabel = SpecMaxIndTbxLabel
'Zone de texte spéc moy max
Set classList(i - 1).SpecMaxIndTbx = SpecMaxIndmclsTbx
'Zone de texte des options de la liste déroulante
Set classList(i - 1).ListTbx = ListTextBox
'Label Options liste déroulante
Set classList(i - 1).ListTbxLabel = ListmclsTbxLabel
Let classList(i - 1).Transition = i
' Set Lbl3 = Frame1.Controls.Add("Forms.Label.1", "lbl3")
' Set txtB3 = Frame1.Add("Forms.TextBox.1")
' With txtB3
' .Name = "specMin" & i
' .Height = 15
' .Width = 200
' .Left = 410
' .Top = 10 * i * 3
' End With
' Lbl3.Caption = "Eléments : "
' Lbl3.Top = txtB3.Top
' Lbl3.Left = 360
Next i
'
'On modifie la visibilité des éléments pour ne permettre que la saisie du modèle courant
Me.RedefineModelParamButton.Visible = True
'
Me.Label1.Visible = False
Me.Label2.Visible = False
Me.Label3.Visible = False
Me.Label21.Visible = False
Me.NewSheetModelLabelTextBox.Visible = False
Me.NewSheetModelColumnsNumberTextBox.Visible = False
Me.SheetCategoryComboBox.Visible = False
Me.EnterNewSheetModelColumnsCountValidateButton.Visible = False
Me.EnterNewSheetModelColumnsCountCancelButton.Visible = False
Me.fcmIndexComboBox1.Visible = False
Me.fcmIndexComboBox2.Visible = False
End If
在 User-Form
中声明一个 VBA-Collection
并将 class 的实例存储在此集合中 Combo-Boxes
。在您的 class 中声明一个 Combo-Box
变量 WithEvents
以便您可以处理 Combo-Box
的事件。 HTH.
User Form
Private m_combos As New Collection
Private Sub UserForm_Initialize()
Set m_combos = New Collection
Dim i
Dim newCombo As CControlEvents
For i = 0 To 2
Set newCombo = New CControlEvents
With newCombo
Set .Combo = Me.Frame1.Add("Forms.ComboBox.1")
.Combo.Top = 20 * i
.Combo.AddItem "A"
.Combo.AddItem "B"
.Combo.AddItem "C"
End With
m_combos.Add newCombo
Next
End Sub
CControlEvents
Private WithEvents m_combo As MSForms.ComboBox
Private Sub m_combo_Change()
MsgBox "Change: " & Me.Combo.Name
End Sub
Public Property Get Combo() As MSForms.ComboBox
Set Combo = m_combo
End Property
Public Property Set Combo(ByVal newCombo As MSForms.ComboBox)
Set m_combo = newCombo
End Property
我基本上是在尝试根据组合框采用的值在用户窗体上显示不同的文本框集。我创建了一个名为 CControlEvents 的 class 模块,我在其中描述了当我更改组合框的值时应该发生的事件:
Private WithEvents mclsCbx As MSForms.ComboBox
Private WithEvents UnitmclsTbx As MSForms.TextBox
Private WithEvents UnitmclsTbxLabel As MSForms.Label
Private WithEvents SpecMinMoymclsTbxLabel As MSForms.Label
Private WithEvents SpecMaxMoymclsTbxLabel As MSForms.Label
Private WithEvents SpecMinIndmclsTbxLabel As MSForms.Label
Private WithEvents SpecMaxIndmclsTbxLabel As MSForms.Label
Private WithEvents SpecMinMoymclsTbx As MSForms.TextBox
Private WithEvents SpecMaxMoymclsTbx As MSForms.TextBox
Private WithEvents SpecMinIndmclsTbx As MSForms.TextBox
Private WithEvents SpecMaxIndmclsTbx As MSForms.TextBox
Private WithEvents ListmclsTbxLabel As MSForms.Label
Private WithEvents ListmclsTbx As MSForms.TextBox
Private mMyProperty As Integer
Public Property Set Cbx(ByVal clsCbx As MSForms.ComboBox): Set mclsCbx = clsCbx: End Property
Public Property Get Cbx() As MSForms.ComboBox: Set Cbx = mclsCbx: End Property
'Property pour les textbox et labels
'TextBox d'unité
Public Property Set UnitTbx(ByVal clsTbx As MSForms.TextBox): Set UnitmclsTbx = clsTbx: End Property
Public Property Get UnitTbx() As MSForms.TextBox: Set UnitTbx = UnitmclsTbx: End Property
'Label d'unité
Public Property Set UnitTbxLabel(ByVal clsTbx As MSForms.Label): Set UnitmclsTbxLabel = clsTbx: End Property
Public Property Get UnitTbxLabel() As MSForms.Label: Set UnitTbxLabel = UnitmclsTbxLabel: End Property
'TextBox de spécification minimum moyenne
Public Property Set SpecMinMoyTbx(ByVal clsTbx As MSForms.TextBox): Set SpecMinMoymclsTbx = clsTbx: End Property
Public Property Get SpecMinMoyTbx() As MSForms.TextBox: Set SpecMinMoyTbx = SpecMinMoymclsTbx: End Property
'Label de spécification minimum moyenne
Public Property Set SpecMinMoyTbxLabel(ByVal clsTbx As MSForms.Label): Set SpecMinMoymclsTbxLabel = clsTbx: End Property
Public Property Get SpecMinMoyTbxLabel() As MSForms.Label: Set SpecMinMoyTbxLabel = SpecMinMoymclsTbxLabel: End Property
'Label de spécification maximum moyenne
Public Property Set SpecMaxMoyTbxLabel(ByVal clsTbx As MSForms.Label): Set SpecMaxMoymclsTbxLabel = clsTbx: End Property
Public Property Get SpecMaxMoyTbxLabel() As MSForms.Label: Set SpecMaxMoyTbxLabel = SpecMaxMoymclsTbxLabel: End Property
'TextBox de spécification max moy
Public Property Set SpecMaxMoyTbx(ByVal clsTbx As MSForms.TextBox): Set SpecMaxMoymclsTbx = clsTbx: End Property
Public Property Get SpecMaxMoyTbx() As MSForms.TextBox: Set SpecMaxMoyTbx = SpecMaxMoymclsTbx: End Property
'TextBox de spécification minimum individuelle
Public Property Set SpecMinIndTbx(ByVal clsTbx As MSForms.TextBox): Set SpecMinIndmclsTbx = clsTbx: End Property
Public Property Get SpecMinIndTbx() As MSForms.TextBox: Set SpecMinIndTbx = SpecMinIndmclsTbx: End Property
'Label de spécification minimum individuelle
Public Property Set SpecMinIndTbxLabel(ByVal clsTbx As MSForms.Label): Set SpecMinIndmclsTbxLabel = clsTbx: End Property
Public Property Get SpecMinIndTbxLabel() As MSForms.Label: Set SpecMinIndTbxLabel = SpecMinIndmclsTbxLabel: End Property
'Label de spécification maximum individuelle
Public Property Set SpecMaxIndTbxLabel(ByVal clsTbx As MSForms.Label): Set SpecMaxIndmclsTbxLabel = clsTbx: End Property
Public Property Get SpecMaxIndTbxLabel() As MSForms.Label: Set SpecMaxIndTbxLabel = SpecMaxIndmclsTbxLabel: End Property
'TextBox de spécification max individuelle
Public Property Set SpecMaxIndTbx(ByVal clsTbx As MSForms.TextBox): Set SpecMaxIndmclsTbx = clsTbx: End Property
Public Property Get SpecMaxIndTbx() As MSForms.TextBox: Set SpecMaxIndTbx = SpecMaxIndmclsTbx: End Property
'TextBox de liste déroulante
Public Property Set ListTbx(ByVal clsTbx As MSForms.TextBox): Set ListmclsTbx = clsTbx: End Property
Public Property Get ListTbx() As MSForms.TextBox: Set ListTbx = ListmclsTbx: End Property
'Label de liste déroulante
Public Property Set ListTbxLabel(ByVal clsTbx As MSForms.Label): Set ListmclsTbxLabel = clsTbx: End Property
Public Property Get ListTbxLabel() As MSForms.Label: Set ListTbxLabel = ListmclsTbxLabel: End Property
Public Property Get MyProperty() As Integer
MyProperty = mMyProperty
End Property
Public Property Let Transition(Value As Integer)
mMyProperty = Value
End Property
Private Sub mclsCbx_Change()
If (Me.Cbx.Text = "NUM") Then
UnitTbx.Visible = True
UnitmclsTbxLabel.Visible = True
SpecMinMoymclsTbxLabel.Visible = True
SpecMinMoymclsTbx.Visible = True
SpecMaxMoymclsTbx.Visible = True
SpecMaxMoymclsTbxLabel.Visible = True
SpecMinIndmclsTbxLabel.Visible = True
SpecMinIndmclsTbx.Visible = True
SpecMaxIndmclsTbx.Visible = True
SpecMaxIndmclsTbxLabel.Visible = True
ListmclsTbx.Visible = False
ListmclsTbxLabel.Visible = False
Else
If (Me.Cbx.Text = "LIST") Then
UnitTbx.Visible = False
UnitmclsTbxLabel.Visible = False
SpecMinMoymclsTbxLabel.Visible = False
SpecMinMoymclsTbx.Visible = False
SpecMaxMoymclsTbx.Visible = False
SpecMaxMoymclsTbxLabel.Visible = False
SpecMinIndmclsTbxLabel.Visible = False
SpecMinIndmclsTbx.Visible = False
SpecMaxIndmclsTbx.Visible = False
SpecMaxIndmclsTbxLabel.Visible = False
ListmclsTbx.Visible = True
ListmclsTbxLabel.Visible = True
End If
End If
End Sub
在用户窗体的代码中,我动态添加了这样的组合框:
Set gclsControlEvents = New CControlEvents
'On attribue la comboBox à une propriété de la classe créée
Set gclsControlEvents.Cbx = oleCbx
'On attribue une zone de texte à une propriété de la classe créée
Set gclsControlEvents.UnitTbx = numUnitTextBox
'Label unité
Set gclsControlEvents.UnitTbxLabel = UnitmclsTbxLabel
'Label spéc moy min
Set gclsControlEvents.SpecMinMoyTbxLabel = SpecMinMoyTbxLabel
'Zone de texte spéc moy min
Set gclsControlEvents.SpecMinMoyTbx = SpecMinMoymclsTbx
'Label spéc max min
Set gclsControlEvents.SpecMaxMoyTbxLabel = SpecMaxMoyTbxLabel
'Zone de texte spéc moy max
Set gclsControlEvents.SpecMaxMoyTbx = SpecMaxMoymclsTbx
所以,我基本上是在用户窗体代码中添加控件并将它们分配给 class 的属性,然后我可以根据需要简单地 hide/show。问题是,当我尝试根据这个原则添加几个组合框时,使用 for 循环,它只适用于最后一个组合框。
编辑:for循环
For i = 1 To NewSheetModelColumnsCount
Set Lbl = Frame1.Controls.Add("Forms.Label.1", "lbl1")
Set txtB1 = Frame1.Add("Forms.TextBox.1")
'Create the combobox
Set oleCbx = Frame1.Add("Forms.ComboBox.1") 'Bug at this line
With oleCbx
.name = "list" & i
.Height = 15
.Width = 100
.Left = 70
.Top = 10 * i * 3
.AddItem "NUM"
.AddItem "LIST"
End With
With txtB1
.name = "chkDemo" & i
.Height = 15
.Width = 100
.Left = 230
.Top = 10 * i * 3
End With
Lbl.Caption = "Colonne n°" + CStr(i)
Lbl.Top = txtB1.Top
Lbl.Left = 10
Set lbl2 = Frame1.Controls.Add("Forms.Label.1", "lbl2")
lbl2.Caption = "Intitulé : "
lbl2.Top = txtB1.Top
lbl2.Left = 180
lbl2.Visible = True
lbl2.Width = 50
Set UnitmclsTbxLabel = UserForm1.Frame1.Controls.Add("Forms.Label.1", "UnitmclsTbxLabel")
Set ListmclsTbxLabel = UserForm1.Frame1.Controls.Add("Forms.Label.1", "ListmclsTbxLabel")
Set SpecMinMoyTbxLabel = UserForm1.Frame1.Controls.Add("Forms.Label.1", "SpecMinMoyTbxLabel")
Set SpecMaxMoyTbxLabel = UserForm1.Frame1.Controls.Add("Forms.Label.1", "SpecMinMoyTbxLabel")
Set SpecMinIndTbxLabel = UserForm1.Frame1.Controls.Add("Forms.Label.1", "SpecMinIndTbxLabel")
Set SpecMaxIndTbxLabel = UserForm1.Frame1.Controls.Add("Forms.Label.1", "SpecMinIndTbxLabel")
UnitmclsTbxLabel.Caption = "Unité : "
UnitmclsTbxLabel.Left = 360
UnitmclsTbxLabel.Visible = False
Set numUnitTextBox = UserForm1.Frame1.Add("Forms.TextBox.1")
Set SpecMinMoymclsTbx = UserForm1.Frame1.Add("Forms.TextBox.1")
Set SpecMaxMoymclsTbx = UserForm1.Frame1.Add("Forms.TextBox.1")
Set SpecMinIndmclsTbx = UserForm1.Frame1.Add("Forms.TextBox.1")
Set SpecMaxIndmclsTbx = UserForm1.Frame1.Add("Forms.TextBox.1")
Set ListTextBox = UserForm1.Frame1.Add("Forms.TextBox.1")
SpecMinMoymclsTbx.Width = 50
SpecMaxMoymclsTbx.Width = 50
SpecMinIndmclsTbx.Width = 50
SpecMaxIndmclsTbx.Width = 50
With numUnitTextBox
.name = "Unit" & i
.Height = 15
.Width = 50
.Left = 360 + UnitmclsTbxLabel.Width - 40
.Top = 10 * i * 3
.Value = "Unit"
End With
ListTextBox.Top = numUnitTextBox.Top
ListTextBox.Left = numUnitTextBox.Left + numUnitTextBox.Width - 40
ListTextBox.Visible = False
ListTextBox.Width = 200
ListmclsTbxLabel.Caption = "Options : "
ListmclsTbxLabel.Visible = False
ListmclsTbxLabel.Top = numUnitTextBox.Top
ListmclsTbxLabel.Left = 360
UnitmclsTbxLabel.Top = numUnitTextBox.Top
SpecMinMoyTbxLabel.Top = numUnitTextBox.Top
SpecMinMoyTbxLabel.Left = numUnitTextBox.Left + numUnitTextBox.Width + 5
SpecMinMoyTbxLabel.Caption = "Spéc min Moy : "
SpecMinMoyTbxLabel.Visible = False
SpecMinMoymclsTbx.Top = numUnitTextBox.Top
SpecMinMoymclsTbx.Visible = False
SpecMinMoymclsTbx.Left = SpecMinMoyTbxLabel.Left + SpecMinMoyTbxLabel.Width - 15
SpecMaxMoyTbxLabel.Visible = False
SpecMaxMoyTbxLabel.Caption = "Spéc max moy : "
SpecMaxMoyTbxLabel.Left = SpecMinMoymclsTbx.Left + SpecMinMoymclsTbx.Width
SpecMaxMoyTbxLabel.Top = numUnitTextBox.Top
SpecMaxMoymclsTbx.Visible = False
SpecMaxMoymclsTbx.Top = numUnitTextBox.Top
SpecMaxMoymclsTbx.Left = SpecMaxMoyTbxLabel.Left + SpecMaxMoyTbxLabel.Width
SpecMinIndTbxLabel.Top = numUnitTextBox.Top
SpecMinIndTbxLabel.Left = SpecMaxMoymclsTbx.Left + SpecMaxMoymclsTbx.Width + 5
SpecMinIndTbxLabel.Caption = "Spéc min Ind : "
SpecMinIndTbxLabel.Visible = False
SpecMinIndmclsTbx.Top = numUnitTextBox.Top
SpecMinIndmclsTbx.Visible = False
SpecMinIndmclsTbx.Left = SpecMinIndTbxLabel.Left + SpecMinIndTbxLabel.Width - 15
SpecMaxIndTbxLabel.Visible = False
SpecMaxIndTbxLabel.Caption = "Spéc max moy : "
SpecMaxIndTbxLabel.Left = SpecMinIndmclsTbx.Left + SpecMinIndmclsTbx.Width
SpecMaxIndTbxLabel.Top = numUnitTextBox.Top
SpecMaxIndmclsTbx.Visible = False
SpecMaxIndmclsTbx.Top = numUnitTextBox.Top
SpecMaxIndmclsTbx.Left = SpecMaxIndTbxLabel.Left + SpecMaxIndTbxLabel.Width
numUnitTextBox.Visible = False
Set gclsControlEvents = New CControlEvents
'On attribue la comboBox à une propriété de la classe créée
Set gclsControlEvents.Cbx = oleCbx
'On attribue une zone de texte à une propriété de la classe créée
Set gclsControlEvents.UnitTbx = numUnitTextBox
'Label unité
Set gclsControlEvents.UnitTbxLabel = UnitmclsTbxLabel
'Label spéc moy min
Set gclsControlEvents.SpecMinMoyTbxLabel = SpecMinMoyTbxLabel
'Zone de texte spéc moy min
Set gclsControlEvents.SpecMinMoyTbx = SpecMinMoymclsTbx
'Label spéc max min
Set gclsControlEvents.SpecMaxMoyTbxLabel = SpecMaxMoyTbxLabel
'Zone de texte spéc moy max
Set gclsControlEvents.SpecMaxMoyTbx = SpecMaxMoymclsTbx
'Label spéc moy min
Set gclsControlEvents.SpecMinIndTbxLabel = SpecMinIndTbxLabel
'Zone de texte spéc moy min
Set gclsControlEvents.SpecMinIndTbx = SpecMinIndmclsTbx
'Label spéc max min
Set gclsControlEvents.SpecMaxIndTbxLabel = SpecMaxIndTbxLabel
'Zone de texte spéc moy max
Set gclsControlEvents.SpecMaxIndTbx = SpecMaxIndmclsTbx
'Zone de texte des options de la liste déroulante
Set gclsControlEvents.ListTbx = ListTextBox
'Label Options liste déroulante
Set gclsControlEvents.ListTbxLabel = ListmclsTbxLabel
Let gclsControlEvents.Transition = i
Next i
编辑:
我试图通过创建一个包含尽可能多的 class CControlEvents 实例的变体数组来解决这个问题,因为我需要组合框,然后在每次迭代时使用索引 i-1 处的这个数组的元素,但它没有用,这是代码:
If (NewSheetModelColumnsCount > 0) Then
For i = 1 To 6
Controls("headerTextBox" + CStr(i)).Visible = True
Controls("CheckBox" + CStr(i)).Visible = True
Next i
For i = 11 To 16
Controls("label" + CStr(i)).Visible = True
Next i
Me.indicationLabel.Visible = False
'Name of the frame
With Me.Frame1
'This will create a vertical scrollbar
.ScrollBars = fmScrollBarsVertical
'Change the values of 2 as Per your requirements
.ScrollHeight = 30 * NewSheetModelColumnsCount + 50
.ScrollWidth = 30 * 50 + 50
'
End With
'Tentative
Dim classList As Variant
ReDim classList(0 To NewSheetModelColumnsCount - 1)
For i = 0 To NewSheetModelColumnsCount - 1
Set classList(i) = New CControlEvents
Next i
For i = 1 To NewSheetModelColumnsCount
Set Lbl = Frame1.Controls.Add("Forms.Label.1", "lbl1")
Set txtB1 = Frame1.Add("Forms.TextBox.1")
' Set ListBoxB1 = Frame1.Controls.Add("Forms.ComboBox.1")
' With ListBoxB1
' .Name = "list" & i
' .Height = 15
' .Width = 100
' .Left = 70
' .Top = 10 * i * 3
' .AddItem "NUM"
' .AddItem "LIST"
' End With
'Create the combobox
Set oleCbx = Frame1.Add("Forms.ComboBox.1") 'Bug at this line
With oleCbx
.name = "list" & i
.Height = 15
.Width = 100
.Left = 70
.Top = 10 * i * 3
.AddItem "NUM"
.AddItem "LIST"
End With
With txtB1
.name = "chkDemo" & i
.Height = 15
.Width = 100
.Left = 230
.Top = 10 * i * 3
End With
Lbl.Caption = "Colonne n°" + CStr(i)
Lbl.Top = txtB1.Top
Lbl.Left = 10
Set lbl2 = Frame1.Controls.Add("Forms.Label.1", "lbl2")
'Set txtB2 = Frame1.Add("Forms.TextBox.1")
' With txtB2
' .name = "unitTextBox" & i
' .Height = 15
' .Width = 100
' .Left = 240
' .Top = 10 * i * 3
' .Value = "txtB2"
' End With
lbl2.Caption = "Intitulé : "
lbl2.Top = txtB1.Top
lbl2.Left = 180
lbl2.Visible = True
lbl2.Width = 50
Set UnitmclsTbxLabel = UserForm1.Frame1.Controls.Add("Forms.Label.1", "UnitmclsTbxLabel")
Set ListmclsTbxLabel = UserForm1.Frame1.Controls.Add("Forms.Label.1", "ListmclsTbxLabel")
Set SpecMinMoyTbxLabel = UserForm1.Frame1.Controls.Add("Forms.Label.1", "SpecMinMoyTbxLabel")
Set SpecMaxMoyTbxLabel = UserForm1.Frame1.Controls.Add("Forms.Label.1", "SpecMinMoyTbxLabel")
Set SpecMinIndTbxLabel = UserForm1.Frame1.Controls.Add("Forms.Label.1", "SpecMinIndTbxLabel")
Set SpecMaxIndTbxLabel = UserForm1.Frame1.Controls.Add("Forms.Label.1", "SpecMinIndTbxLabel")
UnitmclsTbxLabel.Caption = "Unité : "
UnitmclsTbxLabel.Left = 360
UnitmclsTbxLabel.Visible = False
Set numUnitTextBox = UserForm1.Frame1.Add("Forms.TextBox.1")
Set SpecMinMoymclsTbx = UserForm1.Frame1.Add("Forms.TextBox.1")
Set SpecMaxMoymclsTbx = UserForm1.Frame1.Add("Forms.TextBox.1")
Set SpecMinIndmclsTbx = UserForm1.Frame1.Add("Forms.TextBox.1")
Set SpecMaxIndmclsTbx = UserForm1.Frame1.Add("Forms.TextBox.1")
Set ListTextBox = UserForm1.Frame1.Add("Forms.TextBox.1")
SpecMinMoymclsTbx.Width = 50
SpecMaxMoymclsTbx.Width = 50
SpecMinIndmclsTbx.Width = 50
SpecMaxIndmclsTbx.Width = 50
With numUnitTextBox
.name = "Unit" & i
.Height = 15
.Width = 50
.Left = 360 + UnitmclsTbxLabel.Width - 40
.Top = 10 * i * 3
.Value = "Unit"
End With
ListTextBox.Top = numUnitTextBox.Top
ListTextBox.Left = numUnitTextBox.Left + numUnitTextBox.Width - 40
ListTextBox.Visible = False
ListTextBox.Width = 200
ListmclsTbxLabel.Caption = "Options : "
ListmclsTbxLabel.Visible = False
ListmclsTbxLabel.Top = numUnitTextBox.Top
ListmclsTbxLabel.Left = 360
UnitmclsTbxLabel.Top = numUnitTextBox.Top
SpecMinMoyTbxLabel.Top = numUnitTextBox.Top
SpecMinMoyTbxLabel.Left = numUnitTextBox.Left + numUnitTextBox.Width + 5
SpecMinMoyTbxLabel.Caption = "Spéc min Moy : "
SpecMinMoyTbxLabel.Visible = False
SpecMinMoymclsTbx.Top = numUnitTextBox.Top
SpecMinMoymclsTbx.Visible = False
SpecMinMoymclsTbx.Left = SpecMinMoyTbxLabel.Left + SpecMinMoyTbxLabel.Width - 15
SpecMaxMoyTbxLabel.Visible = False
SpecMaxMoyTbxLabel.Caption = "Spéc max moy : "
SpecMaxMoyTbxLabel.Left = SpecMinMoymclsTbx.Left + SpecMinMoymclsTbx.Width
SpecMaxMoyTbxLabel.Top = numUnitTextBox.Top
SpecMaxMoymclsTbx.Visible = False
SpecMaxMoymclsTbx.Top = numUnitTextBox.Top
SpecMaxMoymclsTbx.Left = SpecMaxMoyTbxLabel.Left + SpecMaxMoyTbxLabel.Width
SpecMinIndTbxLabel.Top = numUnitTextBox.Top
SpecMinIndTbxLabel.Left = SpecMaxMoymclsTbx.Left + SpecMaxMoymclsTbx.Width + 5
SpecMinIndTbxLabel.Caption = "Spéc min Ind : "
SpecMinIndTbxLabel.Visible = False
SpecMinIndmclsTbx.Top = numUnitTextBox.Top
SpecMinIndmclsTbx.Visible = False
SpecMinIndmclsTbx.Left = SpecMinIndTbxLabel.Left + SpecMinIndTbxLabel.Width - 15
SpecMaxIndTbxLabel.Visible = False
SpecMaxIndTbxLabel.Caption = "Spéc max moy : "
SpecMaxIndTbxLabel.Left = SpecMinIndmclsTbx.Left + SpecMinIndmclsTbx.Width
SpecMaxIndTbxLabel.Top = numUnitTextBox.Top
SpecMaxIndmclsTbx.Visible = False
SpecMaxIndmclsTbx.Top = numUnitTextBox.Top
SpecMaxIndmclsTbx.Left = SpecMaxIndTbxLabel.Left + SpecMaxIndTbxLabel.Width
numUnitTextBox.Visible = False
'Set gclsControlEvents = New CControlEvents
'On attribue la comboBox à une propriété de la classe créée
Set classList(i - 1).Cbx = oleCbx
'On attribue une zone de texte à une propriété de la classe créée
Set classList(i - 1).UnitTbx = numUnitTextBox
'Label unité
Set classList(i - 1).UnitTbxLabel = UnitmclsTbxLabel
'Label spéc moy min
Set classList(i - 1).SpecMinMoyTbxLabel = SpecMinMoyTbxLabel
'Zone de texte spéc moy min
Set classList(i - 1).SpecMinMoyTbx = SpecMinMoymclsTbx
'Label spéc max min
Set classList(i - 1).SpecMaxMoyTbxLabel = SpecMaxMoyTbxLabel
'Zone de texte spéc moy max
Set classList(i - 1).SpecMaxMoyTbx = SpecMaxMoymclsTbx
'Label spéc moy min
Set classList(i - 1).SpecMinIndTbxLabel = SpecMinIndTbxLabel
'Zone de texte spéc moy min
Set classList(i - 1).SpecMinIndTbx = SpecMinIndmclsTbx
'Label spéc max min
Set classList(i - 1).SpecMaxIndTbxLabel = SpecMaxIndTbxLabel
'Zone de texte spéc moy max
Set classList(i - 1).SpecMaxIndTbx = SpecMaxIndmclsTbx
'Zone de texte des options de la liste déroulante
Set classList(i - 1).ListTbx = ListTextBox
'Label Options liste déroulante
Set classList(i - 1).ListTbxLabel = ListmclsTbxLabel
Let classList(i - 1).Transition = i
' Set Lbl3 = Frame1.Controls.Add("Forms.Label.1", "lbl3")
' Set txtB3 = Frame1.Add("Forms.TextBox.1")
' With txtB3
' .Name = "specMin" & i
' .Height = 15
' .Width = 200
' .Left = 410
' .Top = 10 * i * 3
' End With
' Lbl3.Caption = "Eléments : "
' Lbl3.Top = txtB3.Top
' Lbl3.Left = 360
Next i
'
'On modifie la visibilité des éléments pour ne permettre que la saisie du modèle courant
Me.RedefineModelParamButton.Visible = True
'
Me.Label1.Visible = False
Me.Label2.Visible = False
Me.Label3.Visible = False
Me.Label21.Visible = False
Me.NewSheetModelLabelTextBox.Visible = False
Me.NewSheetModelColumnsNumberTextBox.Visible = False
Me.SheetCategoryComboBox.Visible = False
Me.EnterNewSheetModelColumnsCountValidateButton.Visible = False
Me.EnterNewSheetModelColumnsCountCancelButton.Visible = False
Me.fcmIndexComboBox1.Visible = False
Me.fcmIndexComboBox2.Visible = False
End If
在 User-Form
中声明一个 VBA-Collection
并将 class 的实例存储在此集合中 Combo-Boxes
。在您的 class 中声明一个 Combo-Box
变量 WithEvents
以便您可以处理 Combo-Box
的事件。 HTH.
User Form
Private m_combos As New Collection
Private Sub UserForm_Initialize()
Set m_combos = New Collection
Dim i
Dim newCombo As CControlEvents
For i = 0 To 2
Set newCombo = New CControlEvents
With newCombo
Set .Combo = Me.Frame1.Add("Forms.ComboBox.1")
.Combo.Top = 20 * i
.Combo.AddItem "A"
.Combo.AddItem "B"
.Combo.AddItem "C"
End With
m_combos.Add newCombo
Next
End Sub
CControlEvents
Private WithEvents m_combo As MSForms.ComboBox
Private Sub m_combo_Change()
MsgBox "Change: " & Me.Combo.Name
End Sub
Public Property Get Combo() As MSForms.ComboBox
Set Combo = m_combo
End Property
Public Property Set Combo(ByVal newCombo As MSForms.ComboBox)
Set m_combo = newCombo
End Property