程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> .NET實例教程 >> C# 中如何自定義事件?

C# 中如何自定義事件?

編輯:.NET實例教程
C# 中如何自定義事件?
Wason (zhuhai)
本次只用一個實例,來說明在C#中如何使用自定義事件。
寫一個類,此類包含了自定義事件: onSendMsg。
文件名:ChatServer.cs
public class ChatServer
{
public delegate void MyEventHandler(string msg);
public event MyEventHandler onSendMsg;
public void SendMsg()
{
onSendMsg("開始發送消息!");
}
}
文件名:Form1.cs
///////////////////////////////////
//類事件的處理代碼如下
private void SendMsgTest()
{
//創建ChatServer類的實例
Server f_Server;
f_Server = new Server();
f_Server.onSendMsg += new Server.MyEventHandler(this.OnSendMsg);
//調用 f_Server的SendMsg()函數
f_Server.SendMsg();
}
//處理 onSendMsg 事件代碼
protected void OnSendMsg(string msg)
{
//輸出調試信息
Console.WriteLine(msg);
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved