vb.net对事件源头一次性清除事件订阅列表
在VB.Net中,不能直接使用Event的名称 = nothing来清除所有事件订阅列表,需要使用事件实例,而事件实例是在事件名称+Event, 如eTestEvent = Nothing
Public Class Form1
Public Event eTest As EventHandler
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
RaiseEvent eTest(Me, EventArgs.Empty)
End Sub
Private _no As Integer = 0
Private Sub eTest_Run(sender As System.Object, e As System.EventArgs)
_no += 1
Debug.Print(_no)
TextBox1.Text = _no.ToString()
End Sub
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
AddHandler Me.eTest, AddressOf eTest_Run
AddHandler Me.eTest, AddressOf eTest_Run
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
eTestEvent = Nothing '在事件后面加上Event才是事件实例,只有事件实例才能通过设置为Nothing来清除所有事件列表
End Sub
End Class