如何在类库(DLL)上声明事件并在表单上捕获它们?

Sei*_*aft 2 vb.net

我需要在类库上设置事件并在表单中捕获它们.

例如,我在DLL文件中运行一个sub ,我需要"接收"该类中的sub正在运行的事件.

DLL文件是由我在Visual Basic中创建的,但是如何在其上引发事件以便在表单中捕获?

请举个例子.

Wal*_*ter 7

dll中的代码

 Public Event MySpecialEvent ()

 Private Sub Test 
   RaiseEvent MySpecialEvent
 End Sub
Run Code Online (Sandbox Code Playgroud)

表格中的代码

 Private _MyDll as MyDLL

 Public Sub Main
   _MyDLL = New MyDLL
   AddHandler _MyDLL.MySpecialEvent, AddressOf MySpecialEventHandler
 End Sub

 Private Sub MySpecialEventHandler
   'Put your code here to act upon the handled event
 End Sub  
Run Code Online (Sandbox Code Playgroud)

您还需要在表单生命周期的某个时刻删除事件处理程序

RemoveHandler _MyDLL.MySpecialEvent, AddressOf MySpecialEventHandler
Run Code Online (Sandbox Code Playgroud)