问题
I have some vba code that automatically creates a few ActiveX comboboxes. In order to handle their events I fill a global Collection with CComboEvent objects (custom class that I wrote, see below), one for every combobox. The CComboEvent objects should handle the events. While cbx_Change() in the code below works as expected, cbx_GotFocus() does not fire.
I feel like I'm overseeing something, can anyone please help?
Thank you
Option Explicit
Public WithEvents Cbx As MSForms.ComboBox
Private Sub Cbx_Change()
' TODO: Filter data that is shown in ListFillRange
' For now just show that the event fires:
MsgBox Cbx.Value ' This works as expected on every key stroke
End Sub
Private Sub Cbx_GotFocus()
MsgBox "FOCUS!" ' Never shown
' Open the dropdown list
Cbx.ListFillRange = "A1:A11"
Cbx.DropDown
End Sub
回答1:
To close this question properly: the reason Cbx_GotFocus()
never fires is that it really isn't available to MSForms.ComboBox
in this context. The available events can be checked like this:
Open the left drop down list above the vba editor window and choose the class element you want to respond to events for
Open the right drop down list above the vba editor window to see which events are avalable
This is a trick that you can use at different occasions to check for built in events/functions in vba. A shame that I didn't think of it right away. I finally ended up using a combination of Cbx_KeyUp()
and Cbx_DropButtonClick()
to respond to user interactions in that particular project.
来源:https://stackoverflow.com/questions/30593980/dynamic-handling-of-gotfocus-event-in-vba