一、简单的事件介绍 下面将直接在windows应用程序的一个窗体中说明事件的基本规则 using System; using System.Windows.Forms; namespace Forms { public partial class Event : Form { /// <summary> /// 窗体构造器 /// </summary> public Event() { InitializeComponent(); //使用+=即是把新方法添加到委托列表中--多播委托 btnOne.Click += new EventHandler(btn_Click); btnTwo.Click += new EventHandler(btn_Click); btnTwo.Click += new EventHandler(btnTwo_Click); //匿名委托 btnOne.Click += delegate(object sender, EventArgs e) { labText.Text += "Button one was pressed"; }; } /// <summary> /// 事件处理函数【没有返回值,命名规则为引发事件对象_事件名】 /// </summary> /// <param name="sender">引发事件对象</param> /// <param name="e">包含事件数据的对象</param> private void btnTwo_Click(object sender, EventArgs e) { MessageBox.Show("This only happens in Button 2 click event"); } /// <summary> /// 事件处理函数【没有返回值】 /// </summary> /// <param name="sender">引发事件对象</param> /// <param name="e">包含事件数据的对象</param> private void btn_Click(object sender,EventArgs e) { labText.Text = ((Button)sender).Text + " was pressed"; } } }二、生成事件 using System; using System.Windows.Forms; namespace Forms { //通过Button的Click事件激活自定义的事件 public partial class CreateEvent : Form { BusEntity _busEntity = new BusEntity(); //定义委托--使用自定义的包含事件数据的对象 public delegate void ActionEventHandler(object sender, ActionCancelEventArgs ev); //定义事件 public static event ActionEventHandler Action; public CreateEvent() { InitializeComponent(); btnRaise.Click += new EventHandler(btnRaise_Click); } private void btnRaise_Click(object sender, EventArgs e) { ActionCancelEventArgs cancelEvent = new ActionCancelEventArgs(); OnAction(this, cancelEvent); if (cancelEvent.Cancel) labTest.Text = cancelEvent.Message + "--i = " + _busEntity.I; else labTest.Text = _busEntity.TimeString + "--i = " + _busEntity.I; } protected void OnAction(object sender, ActionCancelEventArgs ev) { //添加判断是否为空事件或事件未绑定处理函数 if (Action != null) //触发事件 Action(sender, ev); } } #region 创建包含事件数据的对象 /// <summary> /// 创建包含事件数据的对象 /// </summary> public class ActionCancelEventArgs : System.ComponentModel.CancelEventArgs { string _msg = ""; public ActionCancelEventArgs() : base() { } public ActionCancelEventArgs(bool cancel) : base(cancel) { } public ActionCancelEventArgs(bool cancel, string message) : base(cancel) { _msg = message; } public string Message { get { return _msg; } set { _msg = value; } } } #endregion public class BusEntity { public BusEntity() { CreateEvent.Action += new CreateEvent.ActionEventHandler(EventManager_Action); } //委托调用的方法 private void EventManager_Action(object sender, ActionCancelEventArgs ev) { ev.Cancel = !DoActions(); if (ev.Cancel) ev.Message = "Wasn't the right time."; i++; } 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; } #region 属性 int i = 0; public int I { get { return i; } set { i = value; } } string _time = ""; public string TimeString { get { return _time; } } #endregion } } 来源:http://www.cnblogs.com/swollaws/archive/2009/07/07/1518453.html 标签 click