前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >自定义CancelEventArgs类,封装事件参数信息,实现e.Cancle=true取消机制。

自定义CancelEventArgs类,封装事件参数信息,实现e.Cancle=true取消机制。

作者头像
跟着阿笨一起玩NET
发布2018-09-18 15:01:58
6800
发布2018-09-18 15:01:58
举报
文章被收录于专栏:跟着阿笨一起玩NET

参考文章:http://blog.csdn.net/lastinglate/article/details/5753113

如果您正在设计可取消的事件,请使用CancelEventArgs(而非是EventArgs)作为事件数据对象e的基类。

使用场景:订阅者可以通过e.Cancle来控制发布者引发事件的方法是否被执行!类似窗体的Form_Closing事件机制。

CancelEventArgsDemo

顺便有关静态事件链的实现,可以参考:https://cloud.tencent.com/developer/article/1341529

 /1.自定义EventArgs类,封装事件参数信息  

代码语言:javascript
复制
public class ActionCancelEventArgs:System.ComponentModel.CancelEventArgs  
    {  
        private string message;  
        public ActionCancelEventArgs() : this(false) { }  
        public ActionCancelEventArgs(bool cancel) : this(false, String.Empty) { }  
        public ActionCancelEventArgs(bool cancel, string message) : base(cancel) {  
            this.message = message;  
        }  
        public string Message { get; set; }  
    }  
[c-sharp] view plaincopyprint?
//2.定义委托  
       public delegate void ActionEventHandler(object sender, ActionCancelEventArgs ev);  
       //3.声明事件(用到委托类型)  
       public static event ActionEventHandler Action;  
        //4.定义事件处理函数  
       protected void OnAction(object sender, ActionCancelEventArgs ev)  
       {  
           if (Action != null)  
           {  
               Action(sender, ev);  
           }  
       }  
//注册事件处理程序  
public class BusEntity  
    {  
        string time = String.Empty;  
        public BusEntity() {  
            //将事件处理程序添加到事件  
            Form1.Action += new Form1.ActionEventHandler(Form1_Action);  
        }  
        private void Form1_Action(object sender, ActionCancelEventArgs e) {  
            e.Cancel = !DoActions();  
            if (e.Cancel)  
            {  
                e.Message = "wasn't the right time.";  
            }  
        }  
        private bool DoActions() {  
            bool retVal = false;  
            DateTime tm = DateTime.Now;  
            if (tm.Second<30)  
            {  
                time = "The time is "+DateTime.Now.ToLongTimeString();  
                retVal = true;  
            }  
            else  
            {  
                time = "";  
            }  
            return retVal;  
        }  
        public string TimeString {  
            get { return time; }  
        }  
    }  
[c-sharp] view plaincopyprint?
private void btnRaise_Click(object sender, EventArgs e)  
      {  
          ActionCancelEventArgs cancelEvent = new ActionCancelEventArgs();  
          OnAction(this, cancelEvent);  
          //抛出事件后,将根据询问订阅者是否取消事件的接受e.Cancle=False/True,来执行后续的代码。
          if (cancelEvent.Cancel)  
          {  
              lblInfo.Text = cancelEvent.Message;  
          }  
          else  
          {  
              lblInfo.Text = busEntity.TimeString;  
          }  
      }
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2012-05-13 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档