我已经通过谷歌和不同的网站,以获得添加处理程序的答案,删除处理程序。 根据Msdn上的文档,为了处理在不同场景中触发的事件,我们使用AddHandle将方法与click事件链接。(可能我的方法错了)。 让我有一个按钮点击事件,我想用方法链接
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
End Sub
和方法是
Sub EventHandler()
' Handle the event.
MsgBox("EventHandler caught event.")
End Sub
当上述两个人在同一个班级或不同班级时,我该如何处理它们。 我可以这样做吗。
AddHandler Button1.Click, AddressOf EventHandler()
RemoveHandler Button1.Click, AddressOf EventHandler()
,请以正确的方式指导我。
答案 0 :(得分:0)
可以通过两种方式订阅活动:
直接处理(当您的控件,示例中的按钮被声明为WithEvents时),它将始终触发:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
' Some action
End Sub
动态订阅/取消订阅事件,例如:
Sub test()
' Subscribe the handlers based on some criteria
If True Then
AddHandler Button1.Click, AddressOf EventHandler1
Else
AddHandler Button1.Click, AddressOf EventHandler2
End If
End Sub
Sub EventHandler1(sender As Object, e As EventArgs)
' some action
' if you wish to unsubscribe the event, do below
RemoveHandler Button1.Click, AddressOf EventHandler1
End Sub
Sub EventHandler2(sender As Object, e As EventArgs)
' some other action
' if you wish to unsbuscribe:
RemoveHandler Button1.Click, AddressOf EventHandler2
End Sub
注意:
您可以将事件订阅到多个处理程序
您的处理程序必须与Handler(Sender as object, e as EventArgs)