本文介绍了如何实现在C#中取消事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道,在C#中,有几个建在传递一个参数(取消)事件,如果将它设置为真将停止进一步执行中引发该事件的对象。
I know that in C#, there are several built in events that pass a parameter ("Cancel") which if set to true will stop further execution in the object that raised the event.
你将如何实现一个事件在其募集对象是能够跟踪的属性在EventArgs的?
How would you implement an event where the raising object was able to keep track of a property in the EventArgs?
下面是什么,我试图做一个WinForms例如:
http://msdn.microsoft.com/en-us/library/system.componentmodel.canceleventargs.cancel.aspx
Here is a WinForms example of what I am trying to do:
http://msdn.microsoft.com/en-us/library/system.componentmodel.canceleventargs.cancel.aspx
感谢你。
推荐答案
这真的很容易。
private event _myEvent;
// ...
// Create the event args
CancelEventArgs args = new CancelEventArgs();
// Fire the event
_myEvent.DynamicInvoke(new object[] { this, args });
// Check the result when the event handler returns
if (args.Cancel)
{
// ...
}
这篇关于如何实现在C#中取消事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!