为多个动态控件创建事件处理程序 [英] Create event handlers for multiple dynamic controls
问题描述
.name
属性,这意味着我无法创建事件处理正确。由于这个问题,我无法创建事件处理程序。下面显示了创建动态控件的代码,以及我为事件处理程序编写的代码(无法正常工作)。 Option Explicit
Public WithEvents cButton As MSForms.CommandButton
私有Sub TextBox1_Change()
如果TextBox1<> vbNullString然后
对于i = 1到TextBox1.Value
设置cButton = Me.Controls.Add(Forms.CommandButton.1)
带有cButton
.Name =CommandButton & i
.Left = 150
.Top = buttonStartPosition
.Width = 300
.Height = 140
结束
下一步i
结束如果
End sub
Private Sub cButton_Click()
如果cButton.Name =CommandButton1然后
MsgBoxButton1
ElseIf cButton.Name = CommandButton2然后
MsgBoxButton2
End If
End Sub
一旦这个代码执行并且两个按钮在屏幕上,我按第一个按钮( button1
),没有任何反应,但是当我按第二个按钮( button2
)我收到消息Button2。那么我怎么不能访问第一个按钮?
我得到了多个按钮的事件来帮助。 。 JWalk Excel技巧
以下是根据您的代码和提供的链接的修改。
创建一个名为Class1的类模块
将修改的代码添加到UserForm1 ..
Option Explicit
Dim Buttons()As New Class1
私有Sub TextBox1_Change()
Dim i As Integer
Dim buttonStartPosition As Integer
Dim cButton As CommandButton
buttonStartPosition = 30
如果TextBox1<> vbNullString然后
对于i = 1到TextBox1.Value
设置cButton = Me.Controls.Add(Forms.CommandButton.1)
带有cButton
.Name =CommandButton & i
.Left = 15
.Top = buttonStartPosition
.Width = 30
.Height = 14
结束
ReDim保存按钮(1 To i)
设置按钮(i).ButtonGroup = cButton
buttonStartPosition = buttonStartPosition + 14
Next i
End If
End Sub
I have a userform that creates two dynamic control buttons but I am having difficulty accessing the .name
property of the dynamic control, which means I can't create the event handler correctly. Due to this problem I am unable to create event handlers. Below shows the code that creates the dynamic controls and also the code that I have written for the event handlers (which isn't functioning correctly)
Option Explicit
Public WithEvents cButton As MSForms.CommandButton
Private Sub TextBox1_Change()
If TextBox1 <> vbNullString Then
For i = 1 To TextBox1.Value
Set cButton = Me.Controls.Add("Forms.CommandButton.1")
With cButton
.Name = "CommandButton" & i
.Left = 150
.Top = buttonStartPosition
.Width = 300
.Height = 140
End With
Next i
End If
End sub
Private Sub cButton_Click()
If cButton.Name = "CommandButton1" Then
MsgBox "Button1"
ElseIf cButton.Name = "CommandButton2" Then
MsgBox "Button2"
End If
End Sub
Once this code is executed and the two buttons are on the screen, I press the first button (button1
) and nothing happens but when I press the second button (button2
) I receive the message "Button2". So how come I can't access the first button?
I got the events for multiple buttons to work with help from .. JWalk Excel Tips
Below is the modification based on your code and the link provided.
Create a Class module called "Class1"
Add modified code to UserForm1..
Option Explicit
Dim Buttons() As New Class1
Private Sub TextBox1_Change()
Dim i As Integer
Dim buttonStartPosition As Integer
Dim cButton As CommandButton
buttonStartPosition = 30
If TextBox1 <> vbNullString Then
For i = 1 To TextBox1.Value
Set cButton = Me.Controls.Add("Forms.CommandButton.1")
With cButton
.Name = "CommandButton" & i
.Left = 15
.Top = buttonStartPosition
.Width = 30
.Height = 14
End With
ReDim Preserve Buttons(1 To i)
Set Buttons(i).ButtonGroup = cButton
buttonStartPosition = buttonStartPosition + 14
Next i
End If
End Sub
这篇关于为多个动态控件创建事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!