事件(Event)C#中的事件處理實際上是一種具有特殊簽名的delegate,一般象下面這個樣子:
public delegate void MyEventHandler(object sender, MyEventArgs e);
其中的兩個參數,sender代表事件發送者,e是事件參數類。MyEventArgs類用來包含與事件相關的數據,所有的事件參數類都必須從System.EventArgs類派生。當然,如果你的事件不含參數,那么可以直接用System.EventArgs類作為參數,或者可以不使用參數。
我們可以將自定義事件的實現歸結為以下幾步:
step1.定義delegate對象類型,它有兩個參數,第一個參數是事件發送者對象,第二個參數是事件參數類對象。
step2.定義事件參數類,此類應當從System.EventArgs類派生。如果事件不帶參數,這一步可以省略。
step3.用event關鍵字定義事件對象,它同時也是一個delegate對象。
step4.在需要觸發事件的地方用調用delegate的方式寫事件觸發方法。(delegate一般被封裝在一個方法內,與屬性操作類似)
step5.用+=操作符添加事件到事件隊列中(-=操作符能夠將事件從隊列中刪除)。
step6.定義事件處理方法,它應當與delegate對象具有相同的參數和返回值類型。
簡單的例子如下:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace myApp


{

/**//// <summary>
/// Form5 的摘要說明。
/// </summary>
public class Form5 : System.Windows.Forms.Form

{
private System.Windows.Forms.Button button1;

/**//// <summary>
/// 必需的設計器變量。
/// </summary>
private System.ComponentModel.Container components = null;

public Form5()

{
//
// Windows 窗體設計器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 調用后添加任何構造函數代碼
//
}


/**//// <summary>
/// 清理所有正在使用的資源。
/// </summary>
protected override void Dispose( bool disposing )

{
if( disposing )

{
if(components != null)

{
components.Dispose();
}
}
base.Dispose( disposing );
}


Windows 窗體設計器生成的代碼#region Windows 窗體設計器生成的代碼

/**//// <summary>
/// 設計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內容。
/// </summary>
private void InitializeComponent()

{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(80, 64);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form5
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.button1);
this.Name = "Form5";
this.Text = "Form5";
this.Load += new System.EventHandler(this.Form5_Load);
this.ResumeLayout(false);

}
#endregion

private shape trigon=new shape();

private void button1_Click(object sender, System.EventArgs e)

{
this.trigon.ColorValue=1;
}

private void Form5_Load(object sender, System.EventArgs e)

{
//步驟5 用+=操作符添加事件到事件隊列中
this.trigon.ColorChange +=new ColorChangeEvent(trigon_ColorChange); //把事件內容加入事件隊列
}

//步驟6 定義事件處理方法
private void trigon_ColorChange(object sender,MyEventArgs e)//事件內容

{
MessageBox.Show("Class: " + sender.ToString()+ "\nClass(x,y): ("+ e.X +","+ e.Y+")" );
}
}


//步驟1 定義Delegate
public delegate void ColorChangeEvent(object sender,MyEventArgs e);

//步驟2 定義參數類
public class MyEventArgs : EventArgs

{
private int _x;
private int _y;

public MyEventArgs(int x, int y)

{
_x = x;
_x = y;
}


public int X
{get
{return _x;}}

public int Y
{get
{return _y;}}
}

public class shape

{
private int cvalue; //成員變量
private int x;
private int y;
private MyEventArgs myEventArgs;

//步驟3 用event定義一個Delegate對象
public event ColorChangeEvent ColorChange; //注意public

public int ColorValue

{

get
{return cvalue;}
set

{
cvalue=value;
//步驟4 觸發事件
ColorChange(this,myEventArgs);
}
}
public shape()

{
cvalue=0;
x=0;
y=0;
myEventArgs=new MyEventArgs(x,y);
}
}
}