怎样将多个动态创建的按钮的click事件绑定到同一个事件?解决思路
怎样将多个动态创建的按钮的click事件绑定到同一个事件?
比如,有如下代码动态创建了2个按钮,如果我程序中有一个btnClick()事件,
怎么样处理,才能让这两个按钮能够在被点击的时候,都能执行btnClick过程?
不要告诉我先为每个按钮绑定一个事件,然后在这两个不同的事件中再调
用btnClick!!!!!!!——需要直接到btnClick的执行
Set btnObj = Form1.Controls.Add( "VB.CommandButton ", "cmdObj1 ")
With Form1!cmdobj1
.Left = 100
.Visible = True
.Width = 2000
.Caption = "Dynamic Button "
End With
Set btnObj = Form1.Controls.Add( "VB.CommandButton ", "cmdObj2 ")
With Form1!cmdobj2
.Left = 3000
.Visible = True
.Width = 2000
.Caption = "Dynamic Button "
End With
------解决方案--------------------
不是很明白你的意思
为什么不定义两个
private WithEvents btnObj1 As CommandButton
private WithEvents btnObj2 As CommandButton
在分别
Set btnObj1 = Form1.Controls.Add( "VB.CommandButton ", "cmdObj1 ")
Set btnObj2 = Form1.Controls.Add( "VB.CommandButton ", "cmdObj2 ")
------解决方案--------------------
定义一个类,每次创建按钮的时候把新按钮绑定到这个类,由这个类去调用btnClick()
我这里调用的是Command1_Click(),注意要把这个事件改成Public
类模块:clsButton
Public WithEvents cmdButton As CommandButton
Private Buttons() As New clsButton
Private nCount As Long
Public Sub Bind(button As CommandButton)
nCount = nCount + 1
ReDim Preserve Buttons(nCount) As New clsButton
Set Buttons(nCount).cmdButton = button
End Sub
Private Sub cmdButton_Click()
Call Form1.Command1_Click
End Sub
=====================================================================
FORM1:
Private btn As New clsButton
Public Sub Command1_Click()
Debug.Print Now
End Sub
Private Sub Command2_Click()
Dim btnobj As CommandButton
Set btnobj = Form1.Controls.Add( "VB.CommandButton ", "cmdObj1 ")
With Form1!cmdobj1
.Left = 100
.Visible = True
.Width = 2000
.Caption = "Dynamic Button "
End With
btn.Bind btnobj
Set btnobj = Form1.Controls.Add( "VB.CommandButton ", "cmdObj2 ")
With Form1!cmdobj2
.Left = 3000
.Visible = True
比如,有如下代码动态创建了2个按钮,如果我程序中有一个btnClick()事件,
怎么样处理,才能让这两个按钮能够在被点击的时候,都能执行btnClick过程?
不要告诉我先为每个按钮绑定一个事件,然后在这两个不同的事件中再调
用btnClick!!!!!!!——需要直接到btnClick的执行
Set btnObj = Form1.Controls.Add( "VB.CommandButton ", "cmdObj1 ")
With Form1!cmdobj1
.Left = 100
.Visible = True
.Width = 2000
.Caption = "Dynamic Button "
End With
Set btnObj = Form1.Controls.Add( "VB.CommandButton ", "cmdObj2 ")
With Form1!cmdobj2
.Left = 3000
.Visible = True
.Width = 2000
.Caption = "Dynamic Button "
End With
------解决方案--------------------
不是很明白你的意思
为什么不定义两个
private WithEvents btnObj1 As CommandButton
private WithEvents btnObj2 As CommandButton
在分别
Set btnObj1 = Form1.Controls.Add( "VB.CommandButton ", "cmdObj1 ")
Set btnObj2 = Form1.Controls.Add( "VB.CommandButton ", "cmdObj2 ")
------解决方案--------------------
定义一个类,每次创建按钮的时候把新按钮绑定到这个类,由这个类去调用btnClick()
我这里调用的是Command1_Click(),注意要把这个事件改成Public
类模块:clsButton
Public WithEvents cmdButton As CommandButton
Private Buttons() As New clsButton
Private nCount As Long
Public Sub Bind(button As CommandButton)
nCount = nCount + 1
ReDim Preserve Buttons(nCount) As New clsButton
Set Buttons(nCount).cmdButton = button
End Sub
Private Sub cmdButton_Click()
Call Form1.Command1_Click
End Sub
=====================================================================
FORM1:
Private btn As New clsButton
Public Sub Command1_Click()
Debug.Print Now
End Sub
Private Sub Command2_Click()
Dim btnobj As CommandButton
Set btnobj = Form1.Controls.Add( "VB.CommandButton ", "cmdObj1 ")
With Form1!cmdobj1
.Left = 100
.Visible = True
.Width = 2000
.Caption = "Dynamic Button "
End With
btn.Bind btnobj
Set btnobj = Form1.Controls.Add( "VB.CommandButton ", "cmdObj2 ")
With Form1!cmdobj2
.Left = 3000
.Visible = True