Dynamic Handling of GotFocus Event in vba

大城市里の小女人 提交于 2019-12-25 04:19:10

问题


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:

  1. Open the left drop down list above the vba editor window and choose the class element you want to respond to events for

  2. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!