Window Form类有很多的属性/方法和事件,其中事件属于一种发布订阅模式 。订阅发布模式定义了一种一对多的依赖关系,让多个订阅者对象同时监听某一个主体对象。这个主体对象在自身状态变化时,会通知所有订阅者对象,使它们能够自动更新自己的状态。 当一个对象的改变需要同时改变其他对象,而且无需关心具体有多少对象需要改变时,就特别适合用此种模式。本文将演示如何在窗体上自定义一个事件(custom event) :
1 自定义CustomEventArgs类
一般自定义的事件都有一个参数,继承自EventArgs.此处我们自定一个CustomEventArgs,类中通过自定义字段来存储参数的值,示例代码如下:
usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; namespaceCustomEventsDemo{ publicclassCustomEventArgs:EventArgs { //自定义字段用于存储值publicobjectTag; publicstringMessage; publicCustomEventArgs() { } publicCustomEventArgs(stringmessage, objecttag) { Message=message; Tag=tag; } } }
2 自定义事件
接下来我们创建一个FormPublisher窗体,然后用 event EventHandler<CustomEventArgs> customEvent;event EventHandler<CustomEventArgs> customSecondEvent和来自定义两个custom Event事件,它们的事件参数为CustomEventArgs.在窗体加载事件中我们触发两个事件(这个顺序会影响在窗体加载时订阅者的事件响应顺序.如果我们创建一个窗体继承此FormPublisher的话,我们会在此窗体的事件面板中看到下图:
而窗体代码如下:
usingSystem; usingSystem.Collections.Generic; usingSystem.ComponentModel; usingSystem.Data; usingSystem.Drawing; usingSystem.Linq; usingSystem.Text; usingSystem.Windows.Forms; namespaceCustomEventsDemo{ publicpartialclassFormPublisher : Form { //定义两个事件publiceventEventHandler<CustomEventArgs>customEvent; publiceventEventHandler<CustomEventArgs>customSecondEvent; publicFormPublisher() { InitializeComponent(); } privatevoidFormWithCutomEvent_Load(objectsender, EventArgse) { //确定自定义事件的执行顺序,继承此窗体的子类窗体加载时的默认顺序if (customEvent!=null) { CustomEventArgscustomEventArgs=newCustomEventArgs(this.textBox1.Text, "customEvent"); customEvent(this, customEventArgs); } if (customSecondEvent!=null) { CustomEventArgscustomEventArgs=newCustomEventArgs(this.textBox1.Text, "customSecondEvent"); customSecondEvent(this, customEventArgs); } } privatevoidbutton1_Click(objectsender, EventArgse) { this.textBox2.AppendText(this.textBox1.Text+"\r\n"); //this.textBox1.Text = "";if (customSecondEvent!=null) { CustomEventArgscustomEventArgs=newCustomEventArgs(this.textBox1.Text, "customSecondEvent"); //触发事件customSecondEvent(this, customEventArgs); } if (customEvent!=null) { CustomEventArgscustomEventArgs=newCustomEventArgs(this.textBox1.Text, "customEvent"); //触发事件customEvent(this, customEventArgs); } } } }
3 订阅事件
下面定义一个FormSubscriber窗体来订阅自定义事件,我们要定义另一个窗体的事件,必须要先实例化那个窗体,否则会调用失败。示例代码如下:
usingSystem; usingSystem.Collections.Generic; usingSystem.ComponentModel; usingSystem.Data; usingSystem.Drawing; usingSystem.Linq; usingSystem.Text; usingSystem.Windows.Forms; namespaceCustomEventsDemo{ publicpartialclassFormSubscriber : Form { FormPublisherform=null; publicFormSubscriber() { InitializeComponent(); //启动2个窗体form=newFormPublisher(); form.Visible=true; //订阅事件form.customSecondEvent+=form_customSecondEvent; //订阅事件form.customEvent+=form_customEvent; //把发布窗体实例化后传入第二个订阅窗体中,否则不能订阅FormSubScriber2from2=newFormSubScriber2(form); from2.Visible=true; } voidform_customSecondEvent(objectsender, CustomEventArgse) { this.textBox1.AppendText("Message from Publisher "+e.Message+" from "+e.Tag+"\r\n"); } voidform_customEvent(objectsender, CustomEventArgse) { this.textBox1.AppendText("Message from Publisher "+e.Message+" from "+e.Tag+"\r\n"); } privatevoidFormSubscriber_Load(objectsender, EventArgse) { } } }
执行示例代码,窗体自定义事件订阅效果如下: