C#委托事件

public partial class DelegateTest : Form
{
public DelegateTest()
{
InitializeComponent();
}
public delegate void GetMessageHandler();//声明委托
private void DelegateEvent_Click(object sender, EventArgs e)
{
//DelegateEvent delegateEvent = new DelegateEvent();//类实例化
//delegateEvent.OnShow += new DelegateEvent.ShowMessageHandler(Message.SendMessage);//注册事件
//delegateEvent.issue();//触发事件


GetMessageHandler gm=new GetMessageHandler(Delegates.SendMessage);//实例委托并注册方法
gm += Delegates.ReciveMessage;//继续注册方法
gm();//执行委托调用
}
}
//class DelegateEvent
//{
// public delegate void ShowMessageHandler();//声明委托
// public event ShowMessageHandler OnShow;//声明事件
// public void issue()
// {
// if (OnShow != null)//判断事件是否注册
// {
// MessageBox.Show("This is newest");
// OnShow();//事件调用
// }
// }

//}
//class Message
//{
// public static void SendMessage()
// {
// MessageBox.Show("WOW,I have a message!");
// }
//}

class Delegates
{
public static void SendMessage()
{
MessageBox.Show("I'll tell you something");
}
public static void ReciveMessage()
{
MessageBox.Show("I konw something");
}

}

posted @ 2014-03-05 16:18  BicycleBoy  阅读(150)  评论(0编辑  收藏  举报