新建From1(窗体),新建Command1(按钮CommandButton),代码:
Option Explicit
Private WithEvents Myclass As Class1
Private Sub Command1_Click()
Set Myclass = New Class1
Call Myclass.RaiseTheEvent 'In this sub event is raised.
End Sub
Private Sub Myclass_MyEvent() 'this is the sub to dill with the event.
Debug.Print "Class1's event 发生了"
End Sub
新建类 Class1.cls,代码:
Public Event MyEvent()
Public Sub RaiseTheEvent()
'some other codes here
Debug.Print "在RaiseTheEvent中"
RaiseEvent MyEvent '如果注释掉,可以取消这个事件
Debug.Print "离开RaiseTheEvent"
End Sub
运行结果:
在RaiseTheEvent中
Class1's event 发生了
离开RaiseTheEvent
个人感觉_Click等有下划线的子程序就是这么生成的。然后我的From1代码就是:
Option Explicit
Private WithEvents Myclass As Class1
Private Sub Command1_Click()
Set Myclass = New Class1
Call Myclass.RaiseTheEvent 'In this sub event is raised.
End Sub
Private Sub Myclass_Click() 'this is the sub to dill with the event.
Debug.Print "Class1's event 发生了"
End Sub
类 Class1.cls,代码:
Public Event Click()
Public Sub RaiseTheEvent()
'some other codes here
Debug.Print "在RaiseTheEvent中"
RaiseEvent Click '如果注释掉,可以取消这个事件
Debug.Print "离开RaiseTheEvent"
End Sub
成功运行,和上面的结果一样!哈哈。