程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 如何訂閱Form的自定義事件,訂閱form自定義

如何訂閱Form的自定義事件,訂閱form自定義

編輯:C#入門知識

如何訂閱Form的自定義事件,訂閱form自定義


   Window Form類有很多的屬性/方法和事件,其中事件屬於一種發布訂閱模式 。訂閱發布模式定義了一種一對多的依賴關系,讓多個訂閱者對象同時監聽某一個主體對象。這個主體對象在自身狀態變化時,會通知所有訂閱者對象,使它們能夠自動更新自己的狀態。 當一個對象的改變需要同時改變其他對象,而且無需關心具體有多少對象需要改變時,就特別適合用此種模式。本文將演示如何在窗體上自定義一個事件(custom event) : 

1 自定義一個CustomEventArgs類

  一般自定義的事件都有一個參數,繼承自EventArgs.此處我們自定一個CustomEventArgs,類中通過自定義字段來存儲參數的值:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace CustomEventsDemo
 7 {
 8     public class CustomEventArgs:EventArgs
 9     {
10         //自定義字段用於存儲值
11         public object Tag;
12         public string Message;
13         public CustomEventArgs()
14         {
15             
16         }
17         public CustomEventArgs(string message, object tag)
18         {
19             Message = message;
20             Tag = tag;
21         }
22     }
23 }

 2 自定義一個事件

   接下來我們創建一個FormPublisher窗體,然後用 event EventHandler<CustomEventArgs> customEvent;event EventHandler<CustomEventArgs> customSecondEvent和來自定義兩個custom Event事件,它們的事件參數為CustomEventArgs.在窗體加載事件中我們觸發兩個事件(這個順序會影響在窗體加載時訂閱者的事件響應順序.如果我們創建一個窗體繼承此FormPublisher的話,我們會在此窗體的事件面板中看到下圖:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Windows.Forms;
 9 
10 namespace CustomEventsDemo
11 {
12     public partial class FormPublisher : Form
13     {
14         //定義兩個事件
15         public event EventHandler<CustomEventArgs> customEvent;
16         public event EventHandler<CustomEventArgs> customSecondEvent;
17         public FormPublisher()
18         {
19             InitializeComponent();
20         }
21 
22         private void FormWithCutomEvent_Load(object sender, EventArgs e)
23         {
24             //確定自定義事件的執行順序,繼承此窗體的子類窗體加載時的默認順序
25             if (customEvent != null)
26             {
27                 CustomEventArgs customEventArgs = new CustomEventArgs(this.textBox1.Text, "customEvent");
28                 customEvent(this, customEventArgs);
29             }
30             if (customSecondEvent != null)
31             {
32 
33                 CustomEventArgs customEventArgs = new CustomEventArgs(this.textBox1.Text, "customSecondEvent");
34                 customSecondEvent(this, customEventArgs);
35             }
36 
37         }
38 
39         private void button1_Click(object sender, EventArgs e)
40         {
41             
42             this.textBox2.AppendText(this.textBox1.Text + "\r\n");
43             //this.textBox1.Text = "";
44             if (customSecondEvent != null)
45             {
46                 CustomEventArgs customEventArgs = new CustomEventArgs(this.textBox1.Text, "customSecondEvent");
47                 //觸發事件
48                 customSecondEvent(this, customEventArgs);
49             }
50             if (customEvent != null)
51             {
52                 CustomEventArgs customEventArgs = new CustomEventArgs(this.textBox1.Text, "customEvent");
53                 //觸發事件
54                 customEvent(this, customEventArgs);
55             }
56         }
57     }
58 }

3 訂閱事件

  下面定義一個FormSubscriber窗體來訂閱自定義事件,我們要定義另一個窗體的事件,必須要先實例化那個窗體,否則會調用失敗:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Windows.Forms;
 9 
10 namespace CustomEventsDemo
11 {
12     public partial class FormSubscriber : Form
13     {
14         FormPublisher form = null;
15        public FormSubscriber()
16         {
17             InitializeComponent();
18             //啟動2個窗體
19             form = new FormPublisher();
20             form.Visible = true;
21            //訂閱事件
22             form.customSecondEvent += form_customSecondEvent;
23            //訂閱事件
24             form.customEvent += form_customEvent;
25            //把發布窗體實例化後傳入第二個訂閱窗體中,否則不能訂閱
26             FormSubScriber2 from2 = new FormSubScriber2(form);
27             from2.Visible = true;
28         }
29 
30        void form_customSecondEvent(object sender, CustomEventArgs e)
31        {
32            this.textBox1.AppendText("Message from Publisher " + e.Message + " from " + e.Tag + "\r\n");
33        }
34 
35        void form_customEvent(object sender, CustomEventArgs e)
36        {
37            this.textBox1.AppendText("Message from Publisher " + e.Message + " from " + e.Tag + "\r\n");
38        }
39 
40         private void FormSubscriber_Load(object sender, EventArgs e)
41         {
42             
43         }
44     }
45 }

   另一個窗體也進行訂閱:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Windows.Forms;
 9 
10 namespace CustomEventsDemo
11 {
12     public partial class FormSubScriber2 : Form
13     {
14      
15         public FormSubScriber2()
16         {
17             InitializeComponent();
18         }
19         public FormSubScriber2(FormPublisher form)
20         {
21             InitializeComponent();
22             //訂閱form自定義事件
23             form.customEvent += form_customEvent;
24         }
25         void form_customEvent(object sender, CustomEventArgs e)
26         {
27             this.textBox1.AppendText("Message from Publisher " + e.Message + " from " + e.Tag + "\r\n");
28         }
29 
30         private void FormSubScriber2_Load(object sender, EventArgs e)
31         {
32 
33         }
34     }
35 }

 

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