• event & delegate Demo(事件&委托示例)


      1using System;
      2using System.ComponentModel;
      3using System.Windows.Forms;
      4
      5namespace WindowsFormsApplication1
      6{
      7    public partial class Form1 : Form
      8    {
      9        public delegate void ActionEventHandler(object sender, ActionCancelEventArgs ev);//声明一个delegate
     10        public static event ActionEventHandler Action;//声明一个名为Action的事件
     11
     12        string _time = "";
     13
     14        public Form1()
     15        {
     16            InitializeComponent();
     17            Form1.Action += new ActionEventHandler(Form1_Action);//为事件Action增加处理程序(即通过ActionEventHandler这个delegate来调用Form1_Action)
     18        }

     19
     20        private void Form1_Action(object sender, ActionCancelEventArgs ev) //这里的方法签名必须与ActionEventHandler的声明签名相同
     21        {
     22            ev.Cancel = DoAction();//调用DoAction,根据当前时间是否超过30秒,决定是否取消事件(小于30秒取消,反之继续)
     23            if (ev.Cancel) 
     24            {
     25                ev.Message = "当前时间小于30秒,事件被取消"//如果取消,设置ev的Message属性  
     26            }

     27        }

     28
     29        /// <summary>
     30        /// 判断当前时间是否超过30秒
     31        /// </summary>
     32        /// <returns>小于30秒,返回true,反之返回false</returns>

     33        private bool DoAction() 
     34        {
     35            bool retVal = false;
     36            DateTime tm = DateTime.Now;
     37
     38            if (tm.Second < 30)
     39            {
     40                _time = "";
     41                retVal = true;
     42            }

     43            else 
     44            {
     45                _time = "事件被触发于 " + DateTime.Now.ToLongTimeString();
     46            }

     47
     48            return retVal;
     49        }

     50
     51        /// <summary>
     52        /// 声明一个当前时间的属性
     53        /// </summary>

     54        public string TimeString 
     55        {
     56            get return _time; }
     57        }

     58
     59        protected void OnAction(object sender, ActionCancelEventArgs ev) 
     60        {
     61            if (Action!=null)//如果有人订阅了Action事件
     62            {
     63                Action(sender, ev);//则事件触发
     64            }

     65        }

     66
     67        /// <summary>
     68        /// 通过按钮来激发事件
     69        /// </summary>
     70        /// <param name="sender"></param>
     71        /// <param name="e"></param>

     72        private void btnRaise_Click(object sender, EventArgs e)
     73        {
     74            ActionCancelEventArgs cancelEvent = new ActionCancelEventArgs();//生成一个ActionCancelEventArgs的实例
     75            OnAction(this, cancelEvent);//激发事件
     76            if (cancelEvent.Cancel)//如果事件被取消,则显示Message
     77            {
     78                lblInfo.Text = cancelEvent.Message;
     79            }

     80            else//反之显示当前时间
     81            {
     82                lblInfo.Text = this.TimeString;
     83            }

     84        }

     85    }

     86
     87    public class ActionCancelEventArgs: CancelEventArgs 
     88    {
     89        string _msg = "";
     90        
     91        //定义一个Message属性
     92        public string Message 
     93        {
     94            get return _msg; }
     95            set { _msg = value;}
     96        }

     97
     98    }

     99}

    100
    作者:菩提树下的杨过
    出处:http://yjmyzz.cnblogs.com
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    2021年7月国产数据库排行榜:openGauss成绩依旧亮眼,Kingbase向Top 10发起冲刺
    在SQL SERVER 中通过ADSI接口查询活动目录AD信息!
    PowerPivot作为多维数据源在PerformancePoint Services的使用!
    自助式微软BI工具PowerPivot入门篇(一)!
    一种在PowerPivot客户端中把SharePoint的PowerPivot作为数据源的方法!
    自助式微软BI工具PowerPivot使用介绍!
    利用开源SharePoint Permission Extension插件对SharePoint 的列表进行权限控制!
    使用SQL Server 集成服务中结合SharePoint的客户端模型进行SharePoint列表的同步实践!
    在SQL Server 集成服务中使用开源SharePoint List组件对SharePoint列表进行ETL操作!
    自助式微软BI工具PowerPivot入门篇(二)!
  • 原文地址:https://www.cnblogs.com/yjmyzz/p/992787.html
Copyright © 2020-2023  润新知