I have a userform with programmatically created comboboxes, which I need to run events on. As per the advise here, I created a wrapper class which I put around each such combobox ("event listener").
This is the rough content of the clsEvntListnr class module
Public WithEvents cb As MSForms.ComboBox
Public frm As UserForm
Private Sub cb_Change()
CollectGarbage
End Sub
Private Sub cb_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
'stuff
End Sub
Private Sub cb_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
'stuff
End Sub
Private Sub cb_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
'stuff
End Sub
Private Sub cb_DropButtonClick()
'stuff
End Sub
Private Sub cb_Enter()
'stuff
End Sub
Private Sub cb_Exit()
'stuff
End Sub
Private Sub cb_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
'stuff
End Sub
Private Sub cb_Click()
'stuff
End Sub
Private Sub cb_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
'stuff
End Sub
Private Sub cb_AfterUpdate()
'stuff
End Sub
This is how the comboboxes are created (as a part of an event for another combobox). The C_COMBOS at the end is a globally declared collection.
Private Sub cbTransaction_Change()
Dim oEvtListnr As clsEventListener
'other stuff and declarations
For i = LBound(var) To UBound(var)
Set ctrl = Me.Controls.Add("forms.combobox.1", "ctrlTb" & i, True)
Set oEvtListnr = New clsEventListener
Set oEvtListnr.cb = ctrl
Set oEvtListnr.frm = Me
C_COMBOS.Add oEvtListnr
next i
End sub
Now the behaviour is mostly as expected with the exceptions that certain event just will not fire. From the events I defined in the class module, the following do fire:
cb_KeyDown, cb_KeyPress, cb_KeyUp, cb_DropButtonClick, cb_DblClick, cb_MouseUp
while these do not:
cb_Change, cb_Click, cb_Enter, cb_Exit, cb_AfterUpdate
I have made the obvious tests by putting in breaks into these events and indeed they simply do not fire up. Any idea what may be the issue?