Creat a new class named Class1, it's codes like this:
Public Event MyEvent()
Public Sub RaiseTheEvent()
'some other codes here
RaiseEvent MyEvent
End Sub
Create a new form, draw a command on it named Command1, double the command to open the code.
It codes like this:
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.
MsgBox "class1's event occured."
End Sub
Start the application and Click the button will raise the event.

博客介绍了在VB中创建类和触发事件的示例。创建名为Class1的类,定义事件和触发事件的方法;在窗体上添加命令按钮,编写代码实例化类并触发事件。点击按钮即可触发类的事件并弹出消息框。
946

被折叠的 条评论
为什么被折叠?



