Excel VBAcombobox标识
我在用户表单上有4个以上的combobox。 当他们开火时,他们发射同样的事件。 我试图做的是找出哪个combobox触发事件。 combobox是根据有多less个组件创build的。 生成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
这工作得很好,我可以得到用户select的值,但我找不到哪个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? 即姓名或其他forms的身份certificate。
在.class中.Name
不会出现在combobox的intellisense列表中,因为MSForms.ComboBox
实际上并没有名称属性本身(在F2对象浏览器中查看它),而该属性是由Control
基类提供的:
Private Sub combobox_Click() MsgBox combobox.Value MsgBox combobox.Name '// no hint but still works '//cast to a Control to get the formal control interface with .Name Dim ctrl As Control: Set ctrl = combobox MsgBox ctrl.Name End Sub
在search了500多个网页(花了很长时间)之后,我终于回答了自己的问题。
这是我使用的,它工作,并点击某些combobox时触发:
Private Sub combobox_Click() MsgBox combobox.Value If combobox = UserForm1.Controls("Component0") Then MsgBox "Success1" End If If combobox = UserForm1.Controls("Component1") Then MsgBox "Success2" End If End Sub
希望这可以用于需要它的其他人。
也许再次参考btn.Combobox? 类似于你如何将combobox分配给button,但是反过来:
set combobox = btn.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 '*******EDIT******** with cButton .combobox = combo .Indx = j end With 'cButton '******************* coll.Add cButton End With Height = Height + 30 Next j
类模块
Public WithEvents btn As MSForms.CommandButton Dim WithEvents mCombobox As MSForms.comboBox Private combolist() As String '*******EDIT******** Public Indx As Long Property Let comboBox(cb As MSForms.comboBox) Set mCombobox = cb End Property '******************* 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 mCombobox_Click() '*******EDIT******** MsgBox "Combobox " & Indx & Chr(9) & mComboBox.Value '******************* End Sub
既然你需要事件的多对一的映射,我假设你在你的实际代码中有一个共同的callback,所以你也可以这样做…
在标准模块中
Public Sub cbCallBack(ocb As clsButton) MsgBox ocb.Indx End Sub
在clsButton(replace事件处理程序)
Private Sub mCombobox_Click() cbCallBack Me End Sub