gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 21:36:15 26 4
gpt4 key购买 nike

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

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com