问题描述
我使用TextChanged事件,我禁用它在这里我并不需要如下
object.Event - =新System.EventHandler(将myHandler);
//我的代码其不需要的事件处理程序
object.Event =新System.EventHandler(将myHandler);
我曾经很多次这样。但我有时需要2禁用这样的代码:
object.Event - =新System.EventHandler(将myHandler);
object.Event - =新System.EventHandler(将myHandler);
当然,我有2个完成它使代码
object.Event + =新System.EventHandler(将myHandler);
object.Event + =新System.EventHandler(将myHandler);
我还不知道为什么我需要2次移除事件处理程序,但它的工作太棒了。
但在1的情况下,我得到的问题。
它不与2个或更多禁用代码工作。
我的问题是,我怎么能看这个事件处理程序,如果只需要一个 - =代码或更多?
或如何管理呢?
I总是像这样工作,以确保我总是把事件处理程序作为第一次
object.Event - =新System.EventHandler(将myHandler);
//我的代码其不需要的事件处理程序
object.Event =新System.EventHandler(将myHandler);
我的建议是停止删除并重新添加事件处理程序,而是添加一个标志来抑制你需要的任何活动,这些代码段期间禁止事件处理程序本身。
您可以有一个单一的布尔标志,或者使用引用计数一些,这取决于你需要如何应对嵌套。
如果有一些原因,你不能改变现有的事件处理程序,怎么样创建一个新的事件处理程序,你重视事件,并呼吁从旧的?
i am using textchanged event and i disable it where i don't need as following
object.Event -= new System.EventHandler(myHandler);
//my code which doesn't need event handler
object.Event += new System.EventHandler(myHandler);
i used many times like this. but i needed sometimes 2 disable code like this:
object.Event -= new System.EventHandler(myHandler);
object.Event -= new System.EventHandler(myHandler);
of course i finished it with 2 enable code
object.Event += new System.EventHandler(myHandler);
object.Event += new System.EventHandler(myHandler);
i don't know yet why i needed 2 times remove event handler but it worked great.
but in 1 case i got problem.
it doesn't work with 2 or more disable code.
my question is, how can i watch this eventhandler if it needs just one -= code or more?or how can i manage it?i always worked like this, to make sure that i always leave event handler as first time
object.Event -= new System.EventHandler(myHandler);
//my code which doesn't need event handler
object.Event += new System.EventHandler(myHandler);
My advice would be to stop removing and re-adding the event handler, and instead add a flag to your event handler itself which inhibits whatever activities you need to inhibit during these sections of code.
You can either have a single boolean flag, or use some kind of reference count, depending on how you need to cope with nesting.
If there's some reason why you can't change the existing event handler, what about creating a new event hander which you attach to Event, and call the old one from that?
这篇关于C#禁用事件处理程序问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!