如何停止调用事件? [英] How to stop invoke events?

查看:83
本文介绍了如何停止调用事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当焦点放在某些控件上时,我想停止调用事件. (例如,如果文本框被选中,我不想捕获KeyPress事件.)

我希望这是可能的.

I want to stop invoking events when focus is on some controls. (Example I don'' want to catch KeyPress event if is selected text box).

I hope this is possible.

推荐答案

如果您不希望为事件添加处理程序吗?

如果您未指定处理程序,则不会发生任何异常情况……
Don''t add a handler for the event if you don''t want it?

If you don''t specify a handler, nothing unusual happens...


比剥离事件更好的是,设置标记以在每个事件处理程序中告诉您代码是否应运行或不是.在按键事件中,还可以通过在eventargs上设置Handled属性来拒绝事件.
Better than stripping events, is to set flags that tell you in each event handler, if the code should run or not. In key press events, you can also reject the event by setting the Handled property on the eventargs.


您也可以取消订阅事件,但是我更喜欢使用Graus提到的标志.

仅供参考:如果您已订阅活动,则可以这样取消订阅:
You could also unsubscribe the events as well, but I rather use flags as mentioned by Graus.

FYI: If you have subscribed the event, you unsubscribe it like this:
// Subscribe
this.button1.Click += new System.EventHandler(this.button1_Click);

// Unsubscribe
this.button1.Click -= new System.EventHandler(this.button1_Click);


这篇关于如何停止调用事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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