C#事件做用和用法

例若有下面的需求須要實現:程序主畫面中彈出一個子窗口。此時主畫面仍然能夠接收用戶的操做(子窗口是非模態的)。子窗口上進行某些操做,根據操做的結果要在主畫面上顯示不一樣的數據。html

即以下圖所示:ide

大多數咱們會這樣作:this

表單1窗口定義:spa

 1  public partial class Form1 : Form
 2     {
 3 
 4         public string LabelName
 5         {
 6             get { return this.label1.Text; }
 7             set { this.label1.Text = value; }
 8         }
 9 
10         public Form1()
11         {
12             InitializeComponent();
13             this.button1.Click += new EventHandler(button1_Click);
14         }
15 
16         /// <summary>
17         /// 按鈕觸發
18         /// </summary>
19         /// <param name="sender"></param>
20         /// <param name="e"></param>
21         private void button1_Click(object sender, EventArgs e)
22         {
23             Form2 form2 = new Form2();
24             form2.form1 = this;
25             form2.Show();
26         }
27     }
View Code

表單2窗口定義:code

 1     public partial class Form2 : Form
 2     {
 3         public Form1 form1;
 4         public Form2()
 5         {
 6             InitializeComponent();
 7             this.button1.Click += new EventHandler(button1_Click);
 8         }
 9 
10         /// <summary>
11         /// 表單2按鈕觸發
12         /// </summary>
13         /// <param name="sender"></param>
14         /// <param name="e"></param>
15         private void button1_Click(object sender, EventArgs e)
16         {
17             form1.LabelName = "改變以後";
18         }
19     }
View Code

這樣雖然能夠達到目的,可是各個模塊之間產生了很強的耦合。通常說來模塊之間的調用應該是單方向的:模塊A調用了模塊B,模塊B就不該該反向調用A,不然就破壞了程序的層次,增強了耦合程度,也使得功能的改變和追加變得很困難。orm

因此這裏就用到了事件:htm

下邊是表單2中的事件定義:blog

 1  public partial class Form2 : Form
 2     {
 3         public delegate void OutputMessage(object sender, System.EventArgs e);
 4         public event OutputMessage ButtonClick;
 5 
 6         public Form2()
 7         {
 8             InitializeComponent();

 9             this.button1.Click += new EventHandler(button1_Click);
10         }
11 
12         /// <summary>
13         /// 表單2按鈕觸發
14         /// </summary>
15         /// <param name="sender"></param>
16         /// <param name="e"></param>
17         private void button1_Click(object sender, EventArgs e)
18         {
19             ButtonClick(this, e);
20         }
21     }

而後在表單1中進行捕獲:事件

 1 public partial class Form1 : Form
 2     {
 3         public Form1()
 4         {
 5             InitializeComponent();
 6             this.button1.Click += new EventHandler(button1_Click);
 7         }
 8 
 9         /// <summary>
10         /// 按鈕觸發
11         /// </summary>
12         /// <param name="sender"></param>
13         /// <param name="e"></param>
14         private void button1_Click(object sender, EventArgs e)
15         {
16             Form2 form = new Form2();
17             form.Show();
18             form.ButtonClick += new Form2.OutputMessage(form_ClickButton);
19         }
20 
21         private void form_ClickButton(object sender, EventArgs e)
22         {
23             this.label1.Text = "改變以後";
24         }
25     }

這樣,各個模塊專心的作本身的事情,不須要過問其餘模塊的事情。get

不知道說的對不對,還請指教。。。

原文:http://www.cnblogs.com/cn-blogs/p/3413652.html

相關文章
相關標籤/搜索