c#在程序中定義和使用自定義事件方法總結(jié)
C#在程序中定義和使用自定義事件可以分為以下幾個(gè)步驟:
步驟1:在類中定義事件
using System;
public class TestClass
{
  //....
  public event EventHandler TestEvent
}
步驟2:定義事件參數(shù)
注意:事件參數(shù)類TestEventArgs繼承自System.EventArgs
using System;
public class TestEventArgs : EventArgs
{
  public TestEventArgs() : base() { }
 
  public string Message { get; set; }
}
步驟3:在TestClass 引發(fā)事件
public class TestClass
{
  // 這個(gè)方法引發(fā)事件
  public void RaiseTestEvent(string message)
  {
    if (TestEvent == null) return;
    TestEvent(this, new TestEventArgs { Message = message });
  }
  public event EventHandler TestEvent; 
}
步驟4:使用事件
class Program
{
  static void Main(string[] args)
  {
 
    TestClass tc = new TestClass();
    // 掛接事件處理方法
    tc.TestEvent += Tc_TestEvent;
     
    Console.WriteLine("按任意鍵引發(fā)事件");
    Console.ReadKey();    
    // 引發(fā)事件
    tc.RaiseTestEvent("通過事件參數(shù)傳遞的字符串");
     
    Console.WriteLine("按任意鍵退出");
    Console.ReadKey();
  }
  private static void Tc_TestEvent(object sender, EventArgs e)
  {
    // 將事件參數(shù)強(qiáng)制轉(zhuǎn)換為TestEventArgs
    TestEventArgs te = (TestEventArgs)e;
    // 顯示事件參數(shù)中的Message
    Console.WriteLine(te.Message);
  }
}
完整的程序如下
using System;
public class TestClass
{
  public void RaiseTestEvent(string message)
  {
    if (TestEvent == null) return;
    TestEvent(this, new TestEventArgs { Message = message });
  }
 
  public event EventHandler TestEvent; 
}
public class TestEventArgs : EventArgs
{
  public TestEventArgs() : base() { }
 
  public string Message { get; set; }
}
class Program
{
  static void Main(string[] args)
  {
 
    TestClass tc = new TestClass();
    tc.TestEvent += Tc_TestEvent;
    Console.WriteLine("按任意鍵引發(fā)事件");
    Console.ReadKey();
    tc.RaiseTestEvent("通過事件參數(shù)傳遞的字符串");
    Console.WriteLine("按任意鍵退出");
    Console.ReadKey();
  }
  private static void Tc_TestEvent(object sender, EventArgs e)
  {
    TestEventArgs te = (TestEventArgs)e;
    Console.WriteLine(te.Message);
  }
}
上一篇:C#網(wǎng)絡(luò)請(qǐng)求與JSON解析的示例代碼
欄 目:C#教程
下一篇:C#使用RSA加密解密文件
本文標(biāo)題:c#在程序中定義和使用自定義事件方法總結(jié)
本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/4820.html
您可能感興趣的文章
- 01-10C#實(shí)現(xiàn)將窗體固定在顯示器的左上角且不能移動(dòng)的方法
 - 01-10WinForm實(shí)現(xiàn)程序一段時(shí)間不運(yùn)行自動(dòng)關(guān)閉的方法
 - 01-10C#實(shí)現(xiàn)將程序鎖定到Win7任務(wù)欄的方法
 - 01-10C#使用windows服務(wù)開啟應(yīng)用程序的方法
 - 01-10C#實(shí)現(xiàn)在Form里面內(nèi)嵌dos窗體的方法
 - 01-10C#中查找Dictionary中的重復(fù)值的方法
 - 01-10C#一個(gè)簡單的定時(shí)小程序?qū)崿F(xiàn)代碼
 - 01-10C#實(shí)現(xiàn)在啟動(dòng)目錄創(chuàng)建快捷方式的方法
 - 01-10C#實(shí)現(xiàn)程序等待延遲執(zhí)行的方法
 - 01-10C#使用Mutex簡單實(shí)現(xiàn)程序單實(shí)例運(yùn)行的方法
 


閱讀排行
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
 - 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
 - 01-10WinForm限制窗體不能移到屏幕外的方法
 - 01-10WinForm繪制圓角的方法
 - 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
 - 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新
 - 01-10C#停止線程的方法
 - 01-10C#實(shí)現(xiàn)清空回收站的方法
 - 01-10C#通過重寫Panel改變邊框顏色與寬度的
 - 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
 
隨機(jī)閱讀
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
 - 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
 - 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
 - 01-11ajax實(shí)現(xiàn)頁面的局部加載
 - 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
 - 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
 - 04-02jquery與jsp,用jquery
 - 01-10delphi制作wav文件的方法
 - 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
 - 01-10C#中split用法實(shí)例總結(jié)
 


