EventHandler委托与自定义委托

http://blog.csdn.net/uuxyz/article/details/7175248

 

EventHandler委托与自定义委托

自定义委托: 

//1、  
public delegate void UcSavedEventHanler(bool isSuccess);  
//2、  
public event UcSavedEventHanler OnUcSaved;  
//3、  
UcEditor1(用户控件).OnUcSaved+= new UcSavedEventHanler(UcEditor1_OnUcSaved);  
void UcEditor1_OnUcSaved(bool isSuccess)  
{  
    //todo:  
} 

  

EventHandler委托: 

//2.1、
public event EventHandler OnUcSaved;
//2.2、
public class SavedEventArgs: EventArgs
{
    public SavedEventArgs(bool isSuccess)
    {
this.isSuccess= isSuccess;   
    }
    public bool isSuccess;   
}//end of class SavedEventArgs
//3、  
UcEditor1(用户控件).OnUcSaved+= new EventHandler(UcEditor1_OnUcSaved);

void UcEditor1_OnUcSaved(object sender,EventArgs e)  
{  
    //todo:  
}  

  

1:EventHandler实际上就是一个特殊的委托,它是由.NET预定义好的一个委托,它的形式是固定的。EventHandler的定义原形类似于:
delegate void EventHandler(object sender,EventArgs e);
如Click事件 

btnVal0.Click += new EventHandler(but_Click);
void but_Click(object sender, EventArgs e)
{
    //todo
}

2:使用EventHandler时,处理函数的返回值必须是Void类型,而使用Deleagate则没有这个限制。

3:Delegate相当于一个函数的指针,用于绑定的函数返回值和参数列表要符合Delegate声明时的要求。