Remove only one specific event handler in vb.net

南楼画角 提交于 2019-12-04 14:49:02

Just use the following statement:

RemoveHandler Button1.Click, AddressOf Button1Clicked

Keep in mind, that it is possible to register the EventHandler more than once. If so, you'll reveive the event as often as you registered the handler. Therefor removing the handler will only remove one registration and you will still receive the button-event.

Edit:
by the way: Your example in your Edit does not look like a standard event handler (sender As object, e As EventArgs)
In general you can add and remove handle using this:

Public Sub FirstHandler(sender As object, e As EventArgs)
 ' …
End Sub

Public Sub SecondHandler(sender As object, e As EventArgs)
 ' …
End Sub

Somewhere:

AddHandler Button1.Click, AddressOf FirstHandler
AddHandler Button1.Click, AddressOf SecondHandler
RemoveHandler Button1.Click, AddressOf FirstHandler
' Only SecondHandler subscribed now

You’re using a lambda expression as EventHandler, so you lack on having its specific address to remove it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!