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

时间:2016-05-24 08:17:52

标签: vba excel-vba events checkbox userform

我动态定义了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

嗯,对于这些复选框,我需要一个" Click" -Event。例如:

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

我怎样才能动态?我不需要CommandButton。这意味着,当用户单击Checkbox时,事件处理程序就会启动。

2 个答案:

答案 0 :(得分:2)

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

使用一些复选框创建一个userform,并将以下代码放在其模块中:

' 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

答案 1 :(得分:0)

使用复选框的AfterUpdate事件

Private Sub MyCheckBox_AfterUpdate()

   ' If the checkbox is unchecked, do nothing
   If MyCheckbox = False Then Exit Sub  

   ' If the checkbox is checked, do whatever you want:
   If FilterOk = True Then
       VersNr = Mid(frmOne.Version1.tag, 1, InStr(frmOne.Version1.tag, "_") - 1)
       Call funcVersion
       Exit Sub
   End If
   ...
End Sub