查看: 84  |  回复: 0
VB6 RaiseEvent的用法
楼主
发表于 3月6日 22:24

新建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

成功运行,和上面的结果一样!哈哈。

您需要登录后才可以回帖 登录 | 立即注册
【本版规则】请勿发表违反国家法律的内容,否则会被冻结账号和删贴。
用户名: 立即注册
密码:
2020-2024 MaNongKu.com