Reputation: 32143
I'm writing a small 2D engine that uses Lua to control it. I've been setting events up in between the Lua and the VB.net for the past little while here. The problem, I've realized, is that is that I can't seem to remove the events which is screwing up quite a few things (because they are being called when they should be done with). To fix this, I wanted to add some kind of way to remove the specific event handler.
I know how to add an event handler:
AddHandler Button1.Click, AddressOf Button1Clicked
But with this in mind, is their a way to remove a specific event handler in the VB.net?
Thanks!
EDIT
Sorry, I didn't explain this very well:
If I have this:
public sub setupevent(byval func as LuaFunction)
AddHandler Me.event_mouseup, Sub(x As Integer, y As Integer)
func.Call(x, y)
End Sub
end sub
(Somewhere else)
setupevent(someLuaFunction)
setupevent(someOtherLuaFunction)
setupevent(JustAnotherLuaFunction)
Is there a way to remove just one of these event connections on the fly?
Upvotes: 4
Views: 10930
Reputation: 4577
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.
Upvotes: 9