我如何将带有事件的 buttons/textBoxes 添加到 Excel 表单
How can i add buttons/textBoxes with events to an Excel form
我正在尝试以编程方式生成帮助表单,其中包含用户可以单击以访问的超链接列表。
这是我目前使用的代码。手动生成按钮是不可能的,因为它们太多了,我在这个例子中只保留了 3 个(这段代码将创建 3 个按钮,但只有最后一个按钮附加了事件,我如何为每个按钮生成新事件新按钮?):
'form z_test - empty form with only code in the background
Option Explicit
Public tbPin As MSForms.textBox ' The textbox control.
Dim objMyEventClass As New C_events ' Create an object of the Class (where we declared the events).
Private Sub UserForm_Initialize()
Dim arrHyperlinks, Hi, topI
arrHyperlinks = Array("google.com", "bing.com", "facebook.com")
topI = 30
For Each Hi In arrHyperlinks
Dim btEx As MSForms.CommandButton
Set btEx = Me.Controls.add("Forms.CommandButton.1")
With btEx
.TOP = topI
.Left = 10
.Width = 130
.Height = 25
.Caption = Hi
End With
topI = topI + 30
Set objMyEventClass.btEvents = btEx ' Attach at event to the button.
Next
End Sub
'class c_events
Option Explicit
Public WithEvents tbEvents As MSForms.textBox
Public WithEvents btEvents As MSForms.CommandButton
Private Sub btEvents_click()
MsgBox btEvents.Caption
End Sub
亲切的问候,
丹尼尔
您的代码只有一个 class,其中包含 1 个按钮,并且您一直在循环内覆盖按钮。所以只有最后一个按钮与事件挂钩。解决这个问题的一种方法是收集 C_events classes。这是您为实现此想法而略微修改的原始代码:
Option Explicit
Public tbPin As MSForms.TextBox ' The textbox control.
Private ButtonEvents As Collection
Private objMyEventClass As C_events ' Create an object of the Class (where we declared the events).
Private Sub UserForm_Initialize()
Dim arrHyperlinks, Hi, topI
arrHyperlinks = Array("google.com", "bing.com", "facebook.com")
topI = 30
Set ButtonEvents = New Collection
For Each Hi In arrHyperlinks
Set objMyEventClass = New C_events
Dim btEx As MSForms.CommandButton
Set btEx = Me.Controls.Add("Forms.CommandButton.1")
With btEx
.Top = topI
.Left = 10
.Width = 130
.Height = 25
.Caption = Hi
End With
topI = topI + 30
Set objMyEventClass.btEvents = btEx ' Attach at event to the button.
ButtonEvents.Add objMyEventClass
Next
End Sub
我正在尝试以编程方式生成帮助表单,其中包含用户可以单击以访问的超链接列表。
这是我目前使用的代码。手动生成按钮是不可能的,因为它们太多了,我在这个例子中只保留了 3 个(这段代码将创建 3 个按钮,但只有最后一个按钮附加了事件,我如何为每个按钮生成新事件新按钮?):
'form z_test - empty form with only code in the background
Option Explicit
Public tbPin As MSForms.textBox ' The textbox control.
Dim objMyEventClass As New C_events ' Create an object of the Class (where we declared the events).
Private Sub UserForm_Initialize()
Dim arrHyperlinks, Hi, topI
arrHyperlinks = Array("google.com", "bing.com", "facebook.com")
topI = 30
For Each Hi In arrHyperlinks
Dim btEx As MSForms.CommandButton
Set btEx = Me.Controls.add("Forms.CommandButton.1")
With btEx
.TOP = topI
.Left = 10
.Width = 130
.Height = 25
.Caption = Hi
End With
topI = topI + 30
Set objMyEventClass.btEvents = btEx ' Attach at event to the button.
Next
End Sub
'class c_events
Option Explicit
Public WithEvents tbEvents As MSForms.textBox
Public WithEvents btEvents As MSForms.CommandButton
Private Sub btEvents_click()
MsgBox btEvents.Caption
End Sub
亲切的问候, 丹尼尔
您的代码只有一个 class,其中包含 1 个按钮,并且您一直在循环内覆盖按钮。所以只有最后一个按钮与事件挂钩。解决这个问题的一种方法是收集 C_events classes。这是您为实现此想法而略微修改的原始代码:
Option Explicit
Public tbPin As MSForms.TextBox ' The textbox control.
Private ButtonEvents As Collection
Private objMyEventClass As C_events ' Create an object of the Class (where we declared the events).
Private Sub UserForm_Initialize()
Dim arrHyperlinks, Hi, topI
arrHyperlinks = Array("google.com", "bing.com", "facebook.com")
topI = 30
Set ButtonEvents = New Collection
For Each Hi In arrHyperlinks
Set objMyEventClass = New C_events
Dim btEx As MSForms.CommandButton
Set btEx = Me.Controls.Add("Forms.CommandButton.1")
With btEx
.Top = topI
.Left = 10
.Width = 130
.Height = 25
.Caption = Hi
End With
topI = topI + 30
Set objMyEventClass.btEvents = btEx ' Attach at event to the button.
ButtonEvents.Add objMyEventClass
Next
End Sub