检测用户窗体中动态对象上的鼠标按下事件
Detect mouse down event on dynamic objects in a userform
我在用户窗体的框架内有一个动态范围的选项按钮。 OptionButtons 的数量根据 table 中的列数而变化。每个按钮都根据列标签进行标记。当有人选择一个选项时,我需要一个列表框来填充在 table 列中找到的项目。
填充选项按钮和 ListBox 非常简单。我知道如何检测已知用户窗体对象上的鼠标按下事件。但是这些按钮仅通过编码而存在并且有所不同。如何在实际不存在的对象上检测 MouseDown?
我尝试了为 Frame
创建 MouseDown Class 的代码
您需要将控件包装在 class 模块中 - 例如,DynamicOptionButton
:
Option Explicit
Private WithEvents ControlEvents As MSForms.OptionButton
Public Sub Initialize(ByVal ctrl As MSForms.OptionButton)
If Not ControlEvents Is Nothing Then ThrowAlreadyInitialized
Set ControlEvents = ctrl
End Property
Private Property Get AsControl() As MSForms.Control
Set AsControl = ControlEvents
End Property
Private Sub ThrowAlreadyInitialized()
Err.Raise 5, TypeName(Me), "Invalid Operation: This control is already initialized."
End Sub
Private Sub ControlEvents_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim parentForm As UserForm1 'todo: use your actual UserForm subtype
Set parentForm = AsControl.Parent
'handle mousedown here.
End Sub
当您创建动态控件时,您需要一个 模块级别 Collection
来保存 DynamicOptionButton
个实例 - 否则它们会 在达到End Sub
时超出范围,你将永远无法处理他们的任何事件。
Private DynamicControls As Collection
Private Sub Class_Initialize()
Set DynamicControls = New Collection
'todo invoke CreateOptionButton
End Sub
Private Sub CreateOptionButton() 'todo parameterize
Dim ctrl As MSForms.OptionButton
Set ctrl = Me.Controls.Add(...)
Dim wrapper As DynamicOptionButton
Set wrapper = New DynamicOptionButton
wrapper.Initialize ctrl
DynamicControls.Add wrapper
End Sub
我在用户窗体的框架内有一个动态范围的选项按钮。 OptionButtons 的数量根据 table 中的列数而变化。每个按钮都根据列标签进行标记。当有人选择一个选项时,我需要一个列表框来填充在 table 列中找到的项目。 填充选项按钮和 ListBox 非常简单。我知道如何检测已知用户窗体对象上的鼠标按下事件。但是这些按钮仅通过编码而存在并且有所不同。如何在实际不存在的对象上检测 MouseDown? 我尝试了为 Frame
创建 MouseDown Class 的代码您需要将控件包装在 class 模块中 - 例如,DynamicOptionButton
:
Option Explicit
Private WithEvents ControlEvents As MSForms.OptionButton
Public Sub Initialize(ByVal ctrl As MSForms.OptionButton)
If Not ControlEvents Is Nothing Then ThrowAlreadyInitialized
Set ControlEvents = ctrl
End Property
Private Property Get AsControl() As MSForms.Control
Set AsControl = ControlEvents
End Property
Private Sub ThrowAlreadyInitialized()
Err.Raise 5, TypeName(Me), "Invalid Operation: This control is already initialized."
End Sub
Private Sub ControlEvents_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim parentForm As UserForm1 'todo: use your actual UserForm subtype
Set parentForm = AsControl.Parent
'handle mousedown here.
End Sub
当您创建动态控件时,您需要一个 模块级别 Collection
来保存 DynamicOptionButton
个实例 - 否则它们会 在达到End Sub
时超出范围,你将永远无法处理他们的任何事件。
Private DynamicControls As Collection
Private Sub Class_Initialize()
Set DynamicControls = New Collection
'todo invoke CreateOptionButton
End Sub
Private Sub CreateOptionButton() 'todo parameterize
Dim ctrl As MSForms.OptionButton
Set ctrl = Me.Controls.Add(...)
Dim wrapper As DynamicOptionButton
Set wrapper = New DynamicOptionButton
wrapper.Initialize ctrl
DynamicControls.Add wrapper
End Sub