关于一个很有用的类模块的教程的问题
我在网上看到一例非常有用的代码,原文如下……
窗体UserForm1上有5个CommandButton控件(名称分别为默认CommandButton 1- CommandButton 5)和1个TextBox控件(名称为TextBox1)。要求当各个CommandButton控件被点击时,它的按钮文字(Caption)会写入TextBox1。
如果不用类,我们需要为5个CommandButton控件分别写5个相同的Click事件代码。如:
Private Sub CommandButton 1_Click()
TextBox1 = CommandButton 1.Caption
End Sub
下面是用类的成员事件方法的代码:
‘类模块Cmds的代码
Option Explicit
Public WithEvents cmd As CommandButton
Private Sub cmd_Click()
UserForm1.TextBox1 = cmd.Caption
End Sub
‘窗体UserForm1的代码
Option Explicit
Dim co As New Collection
Private Sub UserForm_Initialize()
Dim i%
Dim myc As Cmds
For i = 1 To 5
Set myc = New Cmds
Set myc.cmd = Me.Controls("CommandButton" & i)
co.Add myc
Next i
Set myc = Nothing
End Sub
可我按照他所说得去做了,点任何按钮文字框中怎么都是commandbutton1啊?2,3,4,5号commandbutton呢?