Window Form類有很多的屬性/方法和事件,其中事件屬于一種釋出訂閱模式 。訂閱釋出模式定義了一種一對多的依賴關系,讓多個訂閱者對象同時監聽某一個主體對象。這個主體對象在自身狀态變化時,會通知所有訂閱者對象,使它們能夠自動更新自己的狀态。 當一個對象的改變需要同時改變其他對象,而且無需關心具體有多少對象需要改變時,就特别适合用此種模式。本文将示範如何在窗體上自定義一個事件(custom event) :
1 自定義CustomEventArgs類
一般自定義的事件都有一個參數,繼承自EventArgs.此處我們自定一個CustomEventArgs,類中通過自定義字段來存儲參數的值,示例代碼如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CustomEventsDemo
{
public class CustomEventArgs:EventArgs
{
//自定義字段用于存儲值
public object Tag;
public string Message;
public CustomEventArgs()
{
}
public CustomEventArgs(string message, object tag)
{
Message = message;
Tag = tag;
}
}
}
2 自定義事件
接下來我們建立一個FormPublisher窗體,然後用 event EventHandler<CustomEventArgs> customEvent;event EventHandler<CustomEventArgs> customSecondEvent和來自定義兩個custom Event事件,它們的事件參數為CustomEventArgs.在窗體加載事件中我們觸發兩個事件(這個順序會影響在窗體加載時訂閱者的事件響應順序.如果我們建立一個窗體繼承此FormPublisher的話,我們會在此窗體的事件面闆中看到下圖:

而窗體代碼如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace CustomEventsDemo
{
public partial class FormPublisher : Form
{
//定義兩個事件
public event EventHandler<CustomEventArgs> customEvent;
public event EventHandler<CustomEventArgs> customSecondEvent;
public FormPublisher()
{
InitializeComponent();
}
private void FormWithCutomEvent_Load(object sender, EventArgs e)
{
//确定自定義事件的執行順序,繼承此窗體的子類窗體加載時的預設順序
if (customEvent != null)
{
CustomEventArgs customEventArgs = new CustomEventArgs(this.textBox1.Text, "customEvent");
customEvent(this, customEventArgs);
}
if (customSecondEvent != null)
{
CustomEventArgs customEventArgs = new CustomEventArgs(this.textBox1.Text, "customSecondEvent");
customSecondEvent(this, customEventArgs);
}
}
private void button1_Click(object sender, EventArgs e)
{
this.textBox2.AppendText(this.textBox1.Text + "\r\n");
//this.textBox1.Text = "";
if (customSecondEvent != null)
{
CustomEventArgs customEventArgs = new CustomEventArgs(this.textBox1.Text, "customSecondEvent");
//觸發事件
customSecondEvent(this, customEventArgs);
}
if (customEvent != null)
{
CustomEventArgs customEventArgs = new CustomEventArgs(this.textBox1.Text, "customEvent");
//觸發事件
customEvent(this, customEventArgs);
}
}
}
}
3 訂閱事件
下面定義一個FormSubscriber窗體來訂閱自定義事件,我們要定義另一個窗體的事件,必須要先執行個體化那個窗體,否則會調用失敗。示例代碼如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace CustomEventsDemo
{
public partial class FormSubscriber : Form
{
FormPublisher form = null;
public FormSubscriber()
{
InitializeComponent();
//啟動2個窗體
form = new FormPublisher();
form.Visible = true;
//訂閱事件
form.customSecondEvent += form_customSecondEvent;
//訂閱事件
form.customEvent += form_customEvent;
//把釋出窗體執行個體化後傳入第二個訂閱窗體中,否則不能訂閱
FormSubScriber2 from2 = new FormSubScriber2(form);
from2.Visible = true;
}
void form_customSecondEvent(object sender, CustomEventArgs e)
{
this.textBox1.AppendText("Message from Publisher " + e.Message + " from " + e.Tag + "\r\n");
}
void form_customEvent(object sender, CustomEventArgs e)
{
this.textBox1.AppendText("Message from Publisher " + e.Message + " from " + e.Tag + "\r\n");
}
private void FormSubscriber_Load(object sender, EventArgs e)
{
}
}
}
執行示例代碼,窗體自定義事件訂閱效果如下: