VBA:在用户窗体上使用 WithEvents

发布于 2024-07-26 08:09:04 字数 116 浏览 5 评论 0原文

我有一个 Word 用户窗体,其中包含 60 多个不同类型的控件。 我想在每次触发 control_change 事件时评估表单并更改表单提交按钮的启用状态。 但是,我真的不想编写和维护 60 个更改事件处理程序。

I have a Word userform with 60+ controls of varying types. I would like to evaluate the form every time a control_change event is triggered and change the enabled state of the form's submit button. However, I really don't want to write and maintain 60 on change event handlers.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你的呼吸 2024-08-02 08:09:04

您可以创建一个事件接收器类,其中包含特定类型的所有控件的事件处理代码。

例如,创建一个名为 TextBoxEventHandler 的类,如下所示:

Private WithEvents m_oTextBox as MSForms.TextBox

Public Property Set TextBox(ByVal oTextBox as MSForms.TextBox)
    Set m_oTextBox = oTextBox
End Property

Private Sub m_oTextBox_Change()
    ' Do something
End Sub

现在您需要创建 & 为表单上适当类型的每个控件连接该类的一个实例:

Private m_oCollectionOfEventHandlers As Collection

Private Sub UserForm_Initialise()

    Set m_oCollectionOfEventHandlers = New Collection

    Dim oControl As Control
    For Each oControl In Me.Controls

        If TypeName(oControl) = "TextBox" Then

            Dim oEventHandler As TextBoxEventHandler
            Set oEventHandler = New TextBoxEventHandler

            Set oEventHandler.TextBox = oControl

            m_oCollectionOfEventHandlers.Add oEventHandler

        End If

    Next oControl

End Sub

请注意,您需要将事件处理程序实例添加到集合中的原因只是为了确保它们保持引用状态,从而不会被垃圾收集器在你完成它们之前。

显然,这种技术可以扩展到处理其他类型的控制。 您可以为每种类型使用单独的事件处理程序类,也可以为需要处理的每种控件类型使用具有成员变量(以及关联的属性和事件处理程序)的单个类。

You can create an event-sink class that will contain the event-handling code for all of your controls of a particular type.

For example, create the a class called TextBoxEventHandler as follows:

Private WithEvents m_oTextBox as MSForms.TextBox

Public Property Set TextBox(ByVal oTextBox as MSForms.TextBox)
    Set m_oTextBox = oTextBox
End Property

Private Sub m_oTextBox_Change()
    ' Do something
End Sub

Now you need to create & hook up an instance of that class for each control of the appropriate type on your form:

Private m_oCollectionOfEventHandlers As Collection

Private Sub UserForm_Initialise()

    Set m_oCollectionOfEventHandlers = New Collection

    Dim oControl As Control
    For Each oControl In Me.Controls

        If TypeName(oControl) = "TextBox" Then

            Dim oEventHandler As TextBoxEventHandler
            Set oEventHandler = New TextBoxEventHandler

            Set oEventHandler.TextBox = oControl

            m_oCollectionOfEventHandlers.Add oEventHandler

        End If

    Next oControl

End Sub

Note that the reason you need to add the event handler instances to a collection is simply to ensure that they remain referenced and thus don't get discarded by the garbage collector before you're finished with them.

Clearly this technique can be extended to deal with other types of control. You could either have separate event handler classes for each type, or you could use a single class that has a member variable (and associated property & event handler) for each of the control types you need to handle.

撑一把青伞 2024-08-02 08:09:04

在这种情况下,您几乎没有选择,因为事件处理程序无法在 VBA/VB6 中共享

选项 1: 使用从每个事件处理程序调用的中央处理函数。

Sub Control1_ChangeEvent()
  CommonChangeEvent // Just call the common handler, parameters as needed
End Sub

Sub Control2_ChangeEvent()
  CommonChangeEvent
End Sub
...
Sub CommonChangeEvent(/* Add necessary parameters */)
  //Do the heavy lifting here
End Sub

选项 2:在控件数组中组织控件。

Sub TextBox_ChangeEvent(Index As Integer)
  CommonChangeEvent
End Sub

Sub OtherControlType_ChangeEvent(Index As Integer)
  CommonChangeEvent
End Sub

将这两个选项结合起来,您的事件处理程序总数将大大减少,而其余的处理程序只是一个真正的事件处理程序的无脑存根。

In that case you have few options, because event handlers cannot be shared in VBA/VB6

Option 1: Use a central handling function which is called from every event handler.

Sub Control1_ChangeEvent()
  CommonChangeEvent // Just call the common handler, parameters as needed
End Sub

Sub Control2_ChangeEvent()
  CommonChangeEvent
End Sub
...
Sub CommonChangeEvent(/* Add necessary parameters */)
  //Do the heavy lifting here
End Sub

Option 2: Organize your controls in control arrays.

Sub TextBox_ChangeEvent(Index As Integer)
  CommonChangeEvent
End Sub

Sub OtherControlType_ChangeEvent(Index As Integer)
  CommonChangeEvent
End Sub

Combining both options your total event handler count will shrink considerably and the remaining handlers are just brainless stubs for the one true event handler.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文