Program
1using System;
2using System.Collections.Generic;
3using System.Text;
4
5namespace EventExample1
6{
7 class Program
8 {
9 static void Main(string[] args)
10 {
11 MyText myText = new MyText();
12 myText.Changed += new MyText.ChangedEventHandler(myText_Chenged);
13
14 string str = string.Empty;
15 while (str != "exit")
16 {
17 Console.Write("請輸入一個字符串:");
18 str = Console.ReadLine();
19 myText.Text = str;
20 }
21 }
22
23 //事件處理程序
24 private static void myText_Chenged(object sender, EventArgs e)
25 <%2
4.實例解說
現在我們需要設計一個電子郵件程序,當收到電子郵件時,希望將該消息轉發到傳真機(Fax)和手機 (CallPhone);
一.我們需要傳遞消息則需要定義事件傳遞的消息類吧,定義如下:
1namespace EventEmail
2{
3 //事件傳遞的消息定義
4 public class MailMsgEventArgs:EventArgs
5 {
6 public readonly string from, to, subject, body;
7
8 public MailMsgEventArgs(string from, string to, string subject, string body)
9 {
10 this.from = from;
11 this.to = to;
12 this.subject = subject;
13 this.body = body;
14 }
15 }
16}