2013-03-22 227 views
8

我在用戶窗體上有4個以上的組合框。當他們射擊時,他們射擊同一事件。我想要做的是找出哪個ComboBox觸發了事件。組合框的創建取決於組件的數量。代碼生成的組合框如下所示:Excel VBA組合框標識

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 

這工作得很好,我可以讓用戶選擇的價值,但我無法找到已使用該組合框。下面這段代碼是它火災(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 

這段代碼上面是好心的工作由道格Glancy得到的代碼工作的事件產生的組合框。

如何獲取觸發事件的組合框?即名稱或某種其他形式的標識。

回答

4

在類.Name將不會出現在智能感知列表中組合框爲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 
+0

感謝這個工作,我也找到了一個解決方案,經過幾個小時的搜索,我現在把它們結合起來。謝謝你的幫助! – NoLiver92 2013-03-22 11:45:34

2

也許再次參考btn.Combobox?類似於您如何分配,組合框按鈕擺在首位,但隨後反轉:

set combobox = btn.Combobox 
+0

即時通訊不知道你來自哪裏。但是當我做combobox.whatever沒有選項的名稱或任何類型的編號,所以我不知道該怎麼做。我已經看了100多個網站,這些網站都告訴我如何添加和設置值,但不知道如何找出哪個盒子引發了事件 – NoLiver92 2013-03-22 10:56:14

+0

啊,你的原始郵件沒有說你想要的(名稱),只是你想「得到組合框「,因此我認爲你想知道該對象,而不是你想要的對象的名稱...... 感謝您澄清 – 2013-03-22 11:05:10

+1

確定生病編輯主帖 – NoLiver92 2013-03-22 11:06:44

5

我設法終於回答我的問題搜索超過500個網頁後(用了很長時間)

這是我用它工作和火災被點擊的某些組合框時:

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 

希望這可以用於誰需要其他人。

0

是否有一個原因,你不只是添加一個屬性到你的自定義類,並設置該屬性,當你在集合中註冊?

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 

既然你需要很多的事件中的一個映射,我假設你有一個共同的回調在實際的代碼,所以你也可以做到這一點?

在標準模塊

Public Sub cbCallBack(ocb As clsButton) 
    MsgBox ocb.Indx 
End Sub 

在clsButton(替換事件處理程序)

Private Sub mCombobox_Click() 
    cbCallBack Me 
End Sub