国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

如何訂閱Form的自定義事件

2019-11-14 13:42:41
字體:
來源:轉載
供稿:網友

   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 CustomEventsDemo11 {12     public partial class FormPublisher : Form13     {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 CustomEventsDemo11 {12     public partial class FormSubscriber : Form13     {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 CustomEventsDemo11 {12     public partial class FormSubScriber2 : Form13     {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 }

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 南澳县| 连江县| 青浦区| 宁波市| 上杭县| 柳河县| 长沙市| 孟村| 虎林市| 松潘县| 八宿县| 永靖县| 根河市| 龙川县| 华池县| 靖远县| 宜川县| 曲沃县| 密山市| 油尖旺区| 盐津县| 阳西县| 顺平县| 茶陵县| 西丰县| 龙江县| 临清市| 西乌珠穆沁旗| 宕昌县| 交口县| 浑源县| 海盐县| 武乡县| 察哈| 宜宾市| 商河县| 林西县| 隆回县| 乐都县| 盐城市| 福泉市|