vba中GotFocus事件的动态处理 [英] Dynamic Handling of GotFocus Event in vba

查看:382
本文介绍了vba中GotFocus事件的动态处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些vba代码,可以自动创建一些ActiveX组合框。为了处理它们的事件,我用CComboEvent对象(我编写的自定义类,请参见下文)填充一个全局Collection,每个组合框一个。 CComboEvent对象应处理事件。尽管以下代码中的cbx_Change()可以按预期工作,但是cbx_GotFocus()不会触发。

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?

谢谢

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


推荐答案

正确关闭此问题:原因 Cbx_GotFocus()永远不会触发,因为在这种情况下 MSForms.ComboBox 确实不可用。可用的事件可以像这样进行检查:

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. 打开vba编辑器窗口上方的左下拉列表,然后选择class元素您要响应

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

打开vba编辑器窗口上方的右下拉列表,查看哪些事件可用的

Open the right drop down list above the vba editor window to see which events are avalable

这是一个技巧,您可以在不同场合使用检查vba中的内置事件/功能。我没想到的遗憾。我最终使用 Cbx_KeyUp() Cbx_DropButtonClick()的组合来响应该特定项目中的用户交互

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.

这篇关于vba中GotFocus事件的动态处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆