我在用户表单上有 4 个以上的组合框。当它们触发时,它们触发相同的事件。我想做的是找出哪个 ComboBox 触发了事件。组合框的创建取决于有多少组件。生成 ComboBox 的代码如下所示:
For j = 0 To UBound(ComponentList) - 1
'Set Label
num = j + 1
Set control = UserForm1.Controls.Add("Forms.Label.1", "ComponentLabel" & CStr(num) & ":", True)
With control
.Caption = "Component " & CStr(num)
.Left = 30
.Top = Height
.Height = 20
.Width = 100
.Visible = True
End With
'set ComboBox
Set combo = UserForm1.Controls.Add("Forms.ComboBox.1", "Component" & num & ":", True)
With combo
.List = ComponentList()
.Left = 150
.Top = Height
.Height = 20
.Width = 50
.Visible = True
Set cButton = New clsButton
Set cButton.combobox = combo
coll.Add cButton
End With
Height = Height + 30
Next j
这很好用,我可以获得用户选择的值,但我找不到使用了哪个 ComboBox。下面的代码是它触发的事件(clsButton
):
Public WithEvents btn As MSForms.CommandButton
Public WithEvents combobox As MSForms.combobox
Private combolist() As String
Private Sub btn_Click()
If btn.Caption = "Cancel" Then
MsgBox "Cancel"
Unload UserForm1
Variables.ComponentSelectionError = False
ElseIf btn.Caption = "Enter" Then
MsgBox "enter"
Unload UserForm1
Variables.ComponentSelectionError = True
End If
End Sub
Private Sub combobox_Click()
MsgBox combobox.Value
End Sub
上面这段代码由 Doug Glancy 精心编写,以使事件与代码生成的 ComboBoxes 一起工作。
如何获取触发事件的 ComboBox?即名称或某种其他形式的标识。