vba - 在 VBA 中动态定义复选框的事件

标签 vba excel events checkbox userform

我已经为我的用户窗体动态定义了复选框。
见代码:

If rs.EOF = False Then
    i = 1
    Do Until rs.EOF Or i = 6
        With frmOne.Controls("Version" & i)
            .Visible = True
            .Caption = rs!versNo & "#" & rs!Vers_From
            .tag = rs!versNo & "_" & rs!noID & ".pdf"
        End With
        i = i + 1
        rs.MoveNext
    Loop
End If

好吧,对于这些复选框,我需要一个“点击”事件。例如:
Private Sub Version1_Click()
   If FilterOk = True Then
       VersNr = Mid(frmOne.Version1.tag, 1, InStr(frmOne.Version1.tag, "_") - 1)
       Call funcVersion
       Exit Sub
   End If
   ...
End Sub

我怎样才能使动态?我不需要命令按钮。这意味着,当用户单击复选框时,事件处理程序就会启动。

最佳答案

Rory 认为这是一个副本是正确的,但是由于我已经写了一个小片段,所以我会在这里发布。我希望它不违反规则。您需要使用将处理事件的启用事件的对象创建一个自定义类。然后,您可以将您的复选框的引用分配给这些对象。

一个简单的演示示例:

创建一个名为 CheckBoxEventHandler 的类模块并将以下代码放入类模块中。

' This will store a reference to a checkbox and enable handling its events.
Private WithEvents m_chckBox As MSForms.CheckBox

' Method to assign a reference to a checkbox to your event handler
Public Sub AssignCheckBox(c As MSForms.CheckBox)
    Set m_chckBox = c
End Sub

' Private sub to execute something on the event
Private Sub m_chckBox_Click()
    MsgBox "Checkbox" + m_chckBox.Caption + "clicked"
End Sub

创建一个带有一些复选框的用户表单,并将以下代码放入其模块中:
' Define a collection to store your event handlers while the userform is active.
Private eventHandlerCollection As New Collection

Private Sub UserForm_Initialize()
    Dim chckBoxEventHandler As CheckBoxEventHandler, c As Control

    For Each c In UserForm1.Controls
        If TypeName(c) = "CheckBox" Then
            'Create event handler instance
            Set chckBoxEventHandler = New CheckBoxEventHandler
            'Assign it reference to a checkbox
            chckBoxEventHandler.AssignCheckBox c
            'Store the event handler in the userform's collection,
            eventHandlerCollection.Add chckBoxEventHandler
        End If
    Next
End Sub


这是在您的情况下实现此功能的一种可能方法

(我没有你的确切代码,所以我无法测试它,但我相信它应该给你一个大致的想法。)

1. 创建一个名为CheckboxEventHandler 的新类
Public WithEvents chckBox As MSForms.CheckBox

Private Sub chckBox_Click()
    Debug.Print "Checkbox" + chckBox.Caption + "clicked"
    ' Do your click-handler logic here.
    ' If you need private variables that are defined elsewhere, you can define the function
    ' whereever you need it and use the eventhandler only to call it and pass it a reference to the clicked checkbox:
    Call somefunction(chckBox)
    ' Or you could define the function as a public method in frmOne and call it from here like this:
    Call frmOne.somefunction(chckBox)
End Sub

2. 在您的frmOne 中的代码开头添加以下内容用户表单:
' Define a collection to store event handlers.
Private eventHandlerCollection As New Collection

' Method for adding clickhandlers to checkBoxes dynamically
Public Sub createClickHandler(c As MSForms.CheckBox)
    Dim eventHandler As New CheckBoxEventHandler
    eventHandler.chckBox = c
    Call eventHandlerCollection.Add(eventHandler)
End Sub

3. 将事件处理程序附加到复选框
If rs.EOF = False Then
    i = 1
    Do Until rs.EOF Or i = 6

        With frmOne.Controls("Version" & i)
            .Visible = True
            .Caption = rs!versNo & "#" & rs!Vers_From
            .Tag = rs!versNo & "_" & rs!noID & ".pdf"
        End With

        'register event listener
        frmOne.createClickHandler (frmOne.Controls("Version" & i))
        i = i + 1
        rs.MoveNext
    Loop
End If

关于vba - 在 VBA 中动态定义复选框的事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37407974/

相关文章:

vba - 对没有唯一 ID 的名字和姓氏进行 VLOOKUP

excel - 如何在 Xpages 中制作像 Excel 一样工作的表格

javascript - 是否可以在 Angularjs 中的 div 滚动中触发事件?

php - 在新电子邮件上触发 PHP 脚本 (Google Apps/Gmail)

vba - 使用 VBA 设置带有千位分隔符、2 位小数和负数减号的标准数字格式?

excel - 从VBA调用Powershell脚本(带参数)

sql - Access VBA : Run-time error '94' : Invalid use of Null

vba - 在 protected 工作表上的未锁定单元格中禁用复制/粘贴?

excel - 如何将 .txt 文件(带分隔符)中排列不一致的数据重新排列为 Excel 中显示的格式

c# - 这是通用委托(delegate)和事件的正确语法吗?