事件(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);
}
}
}