C# 事件 EventHanlder 博客分类: 编程技术 CC++C#
程序员文章站
2024-02-12 15:33:34
...
using System; public class CharEventArgs : EventArgs { public char CurrChar; public CharEventArgs(char CurrChar) { this.CurrChar = CurrChar; } } class CharChecker { public event EventHandler<CharEventArgs> CharTest; public CharEventArgs myeven; char curr_char; public char Curr_Char { get { return curr_char; } set { if (CharTest != null) { myeven = new CharEventArgs(value); curr_char = value; CharTest(this, myeven); } } } } class AppEvent { static void Main() { CharChecker chartester = new CharChecker(); chartester.CharTest += new EventHandler<CharEventArgs>(Charge_X); chartester.Curr_Char = 'a'; Console.WriteLine("event handler result: {0}", chartester.myeven.CurrChar); chartester.Curr_Char = 'x'; Console.WriteLine("event handler result: {0}", chartester.myeven.CurrChar); Console.WriteLine(); } static void Charge_X(object sender,CharEventArgs e) { CharChecker cc = (CharChecker)sender; if (e.CurrChar == 'x') { Console.Write("trigger char is x,"); e.CurrChar = '?'; } else { Console.Write("trigger char is not x,"); } } }