如何在C#中实现取消事件 [英] How to implement a cancel event in C#

查看:303
本文介绍了如何在C#中实现取消事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道在C#中,有几个内置的事件传递一个参数(Cancel),如果设置为 true 将会在引发事件的对象中停止进一步的执行。 p>

如何实现一个事件,其中提升对象能够跟踪EventArgs中的属性?



以下是我正在尝试做的WinForms示例: http://msdn.microsoft.com /en-us/library/system.componentmodel.canceleventargs.cancel.aspx



谢谢。

解决方案

真的很容易。

 私人活动_myEvent; 

// ...

//创建事件args
CancelEventArgs args = new CancelEventArgs();

//触发事件
_myEvent.DynamicInvoke(new object [] {this,args});

//当事件处理程序返回
时检查结果if(args.Cancel)
{
// ...
}


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.

How would you implement an event where the raising object was able to keep track of a property in the EventArgs?

Here is a WinForms example of what I am trying to do:
http://msdn.microsoft.com/en-us/library/system.componentmodel.canceleventargs.cancel.aspx

Thank you.

解决方案

It's really easy.

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#中实现取消事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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