在vb.net中只删除一个特定的事件处理程序 [英] Remove only one specific event handler in vb.net

查看:118
本文介绍了在vb.net中只删除一个特定的事件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个使用Lua来控制它的小型2D引擎。在这里,我已经在Lua和VB.net之间设置了一些事情。这个问题,我已经意识到,那就是我似乎不能删除那些正在拧紧很多事情的事件(因为当他们被完成时被调用)。要解决这个问题,我想添加一些方法来删除特定的事件处理程序。



我知道如何添加事件处理程序:

  AddHandler Button1.Click,AddressOf Button1Clicked 





谢谢!



编辑



对不起,我没有很好地解释:



如果我有这个:

  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

(其他地方)

setupevent(someLuaFunction)
setupevent(someOtherLuaFunction)
setupevent(JustAnotherLuaFunction)

有没有办法即时删除这些事件连接之一?

解决方案

只需使用以下语句:

  RemoveHandler Button1.Click,AddressOf Button1Clicked 

请记住,可以多次注册EventHandler。如果是这样,您会经常注意事件的注册处理程序。因此,删除处理程序只会删除一个注册,您仍然会收到按钮事件。



编辑:

顺便说一句:您的编辑看起来不像标准的事件处理程序(发件人作为对象,作为EventArgs)

一般来说,您可以使用以下方法添加和删除句柄:

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

公共Sub SecondHandler(sender As object ,e As EventArgs)
'...
End Sub

某处: p>

  AddHandler Button1.Click,AddressOf FirstHandler 
AddHandler Button1.Click,AddressOf SecondHandler
RemoveHandler Button1.Click,AddressOf FirstHandler
'只有SecondHandler现在订阅

您正在使用lambda表达式作为EventHandler,所以你没有具体的地址来删除它。


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?

解决方案

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.

这篇关于在vb.net中只删除一个特定的事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆