委托、事件--事件介绍篇

一、简单的事件介绍

      下面将直接在windows应用程序的一个窗体中说明事件的基本规则

  1. using System;
  2. using System.Windows.Forms;
  3.  
  4. namespace Forms
  5. {
  6.     public partial class Event : Form
  7.     {
  8.         /// <summary>
  9.         /// 窗体构造器
  10.         /// </summary>
  11.         public Event()
  12.         {
  13.             InitializeComponent();
  14.             // 使用+=即是把新方法添加到委托列表中--多播委托
  15.             btnOne.Click += new EventHandler(btn_Click);
  16.             btnTwo.Click += new EventHandler(btn_Click);
  17.             btnTwo.Click += new EventHandler(btnTwo_Click);
  18.             // 匿名委托
  19.             btnOne.Click += delegate(object sender, EventArgs e) { labText.Text += "Button one was pressed"; };
  20.         }
  21.  
  22.         /// <summary>
  23.         /// 事件处理函数【没有返回值,命名规则为引发事件对象_事件名】
  24.         /// </summary>
  25.         /// <param name="sender"> 引发事件对象 </param>
  26.         /// <param name="e"> 包含事件数据的对象 </param>
  27.         private void btnTwo_Click(object sender, EventArgs e)
  28.         {
  29.             MessageBox.Show("This only happens in Button 2 click event");
  30.         }
  31.  
  32.         /// <summary>
  33.         /// 事件处理函数【没有返回值】
  34.         /// </summary>
  35.         /// <param name="sender"> 引发事件对象 </param>
  36.         /// <param name="e"> 包含事件数据的对象 </param>
  37.         private void btn_Click(object sender,EventArgs e)
  38.         {
  39.             labText.Text = ((Button)sender).Text + " was pressed";
  40.         }
  41.     }
  42. }

二、生成事件

  1. using System;
  2. using System.Windows.Forms;
  3.  
  4. namespace Forms
  5. {
  6.     // 通过Button的Click事件激活自定义的事件
  7.     public partial class CreateEvent : Form
  8.     {
  9.         BusEntity _busEntity = new BusEntity();
  10.  
  11.         // 定义委托--使用自定义的包含事件数据的对象
  12.         public delegate void ActionEventHandler(object sender, ActionCancelEventArgs ev);
  13.         // 定义事件
  14.         public static event ActionEventHandler Action;
  15.  
  16.         public CreateEvent()
  17.         {
  18.             InitializeComponent();
  19.             btnRaise.Click += new EventHandler(btnRaise_Click);
  20.         }
  21.  
  22.         private void btnRaise_Click(object sender, EventArgs e)
  23.         {
  24.             ActionCancelEventArgs cancelEvent = new ActionCancelEventArgs();
  25.             OnAction(this, cancelEvent);
  26.             if (cancelEvent.Cancel)
  27.                 labTest.Text = cancelEvent.Message + "--i = " + _busEntity.I;
  28.             else
  29.                 labTest.Text = _busEntity.TimeString + "--i = " + _busEntity.I;
  30.         }
  31.        
  32.         protected void OnAction(object sender, ActionCancelEventArgs ev)
  33.         {
  34.             // 添加判断是否为空事件或事件未绑定处理函数
  35.             if (Action != null)
  36.                 // 触发事件
  37.                 Action(sender, ev);
  38.         }
  39.     }
  40.  
  41.     #region 创建包含事件数据的对象
  42.     /// <summary>
  43.     /// 创建包含事件数据的对象
  44.     /// </summary>
  45.     public class ActionCancelEventArgs : System.ComponentModel.CancelEventArgs
  46.     {
  47.         string _msg = "";
  48.  
  49.         public ActionCancelEventArgs() : base() { }
  50.  
  51.         public ActionCancelEventArgs(bool cancel) : base(cancel) { }
  52.  
  53.         public ActionCancelEventArgs(bool cancel, string message)
  54.             : base(cancel)
  55.         {
  56.             _msg = message;
  57.         }
  58.  
  59.         public string Message
  60.         {
  61.             get { return _msg; }
  62.             set { _msg = value; }
  63.         }
  64.     }
  65.     #endregion
  66.  
  67.     public class BusEntity
  68.     {
  69.         public BusEntity()
  70.         {
  71.             CreateEvent.Action += new CreateEvent.ActionEventHandler(EventManager_Action);
  72.         }
  73.  
  74.         // 委托调用的方法
  75.         private void EventManager_Action(object sender, ActionCancelEventArgs ev)
  76.         {
  77.             ev.Cancel = !DoActions();
  78.             if (ev.Cancel)
  79.                 ev.Message = "Wasn't the right time.";
  80.  
  81.             i++;
  82.         }
  83.  
  84.         private bool DoActions()
  85.         {
  86.             bool retVal = false;
  87.             DateTime tm = DateTime.Now;
  88.  
  89.             if (tm.Second < 30)
  90.             {
  91.                 _time = "The time is " + DateTime.Now.ToLongTimeString();
  92.                 retVal = true;
  93.             }
  94.             else
  95.                 _time = "";
  96.  
  97.             return retVal;
  98.         }
  99.  
  100.         #region 属性
  101.         int i = 0;
  102.         public int I
  103.         {
  104.             get { return i; }
  105.             set { i = value; }
  106.         }
  107.         string _time = "";
  108.         public string TimeString
  109.         {
  110.             get { return _time; }
  111.         }
  112.         #endregion
  113.     }
  114. }

      

你可能感兴趣的:(事件)