程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> C#編程利器之四:委托與事件(Delegate and event) (下)(5)

C#編程利器之四:委托與事件(Delegate and event) (下)(5)

編輯:關於C語言

1namespace EventEmail
2{
3  /**//// <summary>
4  /// 手機
5  /// </summary>
6  public class CallPhone
7  {
8    private TextBox _tBox;
9    public CallPhone(MailManager mm, TextBox tBox)
10    {
11      mm.MailMsg += new MailMsgEventHandler(CellPhoneMsg);
12      _tBox = tBox;
13    }
14
15    private void CellPhoneMsg(Object sender, MailMsgEventArgs e)
16    {
17      _tBox.Text += string.Format("消息到手機:{4}來自:{0}{4}發到:{1}{4}主題:{2} {4}內容:{3}{4}{4}", e.from, e.to, e.subject, e.body,Environment.NewLine);
18    }
19
20    public void Register(MailManager mm)
21    {
22      mm.MailMsg += new MailMsgEventHandler(CellPhoneMsg);
23    }
24    public void UnRegister(MailManager mm)
25    {
26      mm.MailMsg -= new MailMsgEventHandler(CellPhoneMsg);
27    }
28  }
29} 

四.客戶端調用

上面的邏輯處理完畢,下面來看看調用情況:

1namespace EventEmail
2{
3  public partial class Form1 : Form
4  {
5    private Fax fax = null;
6    private CallPhone cell = null;
7    private MailManager mm = null;
8    public Form1()
9    {
10      InitializeComponent();
11      mm = new MailManager();
12      fax = new Fax(mm, txtReceiver);
13      cell = new CallPhone(mm, txtReceiver);
14    }
15
16    private void Form1_Load(object sender, EventArgs e)
17    {
18
19    }
20
21    private void btnSend_Click(object sender, EventArgs e)
22    {
23      mm.SimulateArrivingMsg(txtFrom.Text, txtTo.Text, txtSubject.Text, txtBody.Text);
24    }
25
26    private void btnClear_Click(object sender, EventArgs e)
27    {
28      this.txtReceiver.Text = "";
29    }
30  }
31}

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