如何订阅Form的自定义事件

王朝学院·作者佚名  2016-08-28
窄屏简体版  字體: 小  |  中  |  大  |  超大  

Window Form类有很多的属性/方法和事件,其中事件属于一种发布订阅模式 。订阅发布模式定义了一种一对多的依赖关系,让多个订阅者对象同时监听某一个主体对象。这个主体对象在自身状态变化时,会通知所有订阅者对象,使它们能够自动更新自己的状态。 当一个对象的改变需要同时改变其他对象,而且无需关心具体有多少对象需要改变时,就特别适合用此种模式。本文将演示如何在窗体上自定义一个事件(custom event) :

1 自定义一个CustomEventArgs类一般自定义的事件都有一个参数,继承自EventArgs.此处我们自定一个CustomEventArgs,类中通过自定义字段来存储参数的值:

1usingSystem;2usingSystem.Collections.Generic;3usingSystem.Linq;4usingSystem.Text;56namespaceCustomEventsDemo7{8publicclassCustomEventArgs:EventArgs9{10//自定义字段用于存储值11publicobjectTag;12publicstringMessage;13publicCustomEventArgs()14{1516}17publicCustomEventArgs(stringmessage,objecttag)18{19Message =message;20Tag =tag;21}22}23}

2 自定义一个事件 接下来我们创建一个FormPublisher窗体,然后用 event EventHandler<CustomEventArgs> customEvent;event EventHandler<CustomEventArgs> customSecondEvent和来自定义两个custom Event事件,它们的事件参数为CustomEventArgs.在窗体加载事件中我们触发两个事件(这个顺序会影响在窗体加载时订阅者的事件响应顺序.如果我们创建一个窗体继承此FormPublisher的话,我们会在此窗体的事件面板中看到下图:

[flash=500,400]http://s1.knowsky.com/20160211/unz5owyuss025.jpg[/flash]

1usingSystem;2usingSystem.Collections.Generic;3usingSystem.ComponentModel;4usingSystem.Data;5usingSystem.Drawing;6usingSystem.Linq;7usingSystem.Text;8usingSystem.Windows.Forms;910namespaceCustomEventsDemo11{12publicpartialclassFormPublisher : Form13{14//定义两个事件15publiceventEventHandler<CustomEventArgs>customEvent;16publiceventEventHandler<CustomEventArgs>customSecondEvent;17publicFormPublisher()18{19InitializeComponent();20}2122PRivatevoidFormWithCutomEvent_Load(objectsender, EventArgs e)23{24//确定自定义事件的执行顺序,继承此窗体的子类窗体加载时的默认顺序25if(customEvent !=null)26{27CustomEventArgs customEventArgs =newCustomEventArgs(this.textBox1.Text,"customEvent");28customEvent(this, customEventArgs);29}30if(customSecondEvent !=null)31{3233CustomEventArgs customEventArgs =newCustomEventArgs(this.textBox1.Text,"customSecondEvent");34customSecondEvent(this, customEventArgs);35}3637}3839privatevoidbutton1_Click(objectsender, EventArgs e)40{4142this.textBox2.AppendText(this.textBox1.Text +"\r\n");43//this.textBox1.Text ="";44if(customSecondEvent !=null)45{46CustomEventArgs customEventArgs =newCustomEventArgs(this.textBox1.Text,"customSecondEvent");47//触发事件48customSecondEvent(this, customEventArgs);49}50if(customEvent !=null)51{52CustomEventArgs customEventArgs =newCustomEventArgs(this.textBox1.Text,"customEvent");53//触发事件54customEvent(this, customEventArgs);55}56}57}58}

3 订阅事件下面定义一个FormSubscriber窗体来订阅自定义事件,我们要定义另一个窗体的事件,必须要先实例化那个窗体,否则会调用失败:

1usingSystem;2usingSystem.Collections.Generic;3usingSystem.ComponentModel;4usingSystem.Data;5usingSystem.Drawing;6usingSystem.Linq;7usingSystem.Text;8usingSystem.Windows.Forms;910namespaceCustomEventsDemo11{12publicpartialclassFormSubscriber : Form13{14FormPublisher form =null;15publicFormSubscriber()16{17InitializeComponent();18//启动2个窗体19form =newFormPublisher();20form.Visible =true;21//订阅事件22form.customSecondEvent +=form_customSecondEvent;23//订阅事件24form.customEvent +=form_customEvent;25//把发布窗体实例化后传入第二个订阅窗体中,否则不能订阅26FormSubScriber2 from2 =newFormSubScriber2(form);27from2.Visible =true;28}2930voidform_customSecondEvent(objectsender, CustomEventArgs e)31{32this.textBox1.AppendText("Message from Publisher"+ e.Message +"from"+ e.Tag +"\r\n");33}3435voidform_customEvent(objectsender, CustomEventArgs e)36{37this.textBox1.AppendText("Message from Publisher"+ e.Message +"from"+ e.Tag +"\r\n");38}3940privatevoidFormSubscriber_Load(objectsender, EventArgs e)41{4243}44}45}

另一个窗体也进行订阅:

1usingSystem;2usingSystem.Collections.Generic;3usingSystem.ComponentModel;4usingSystem.Data;5usingSystem.Drawing;6usingSystem.Linq;7usingSystem.Text;8usingSystem.Windows.Forms;910namespaceCustomEventsDemo11{12publicpartialclassFormSubScriber2 : Form13{1415publicFormSubScriber2()16{17InitializeComponent();18}19publicFormSubScriber2(FormPublisher form)20{21InitializeComponent();22//订阅form自定义事件23form.customEvent +=form_customEvent;24}25voidform_customEvent(objectsender, CustomEventArgs e)26{27this.textBox1.AppendText("Message from Publisher"+ e.Message +"from"+ e.Tag +"\r\n");28}2930privatevoidFormSubScriber2_Load(objectsender, EventArgs e)31{3233}34}35}

[flash=500,400]http://s1.knowsky.com/20160211/er1fzzfwahc25.png[/flash]

[flash=500,400]http://s1.knowsky.com/20160211/5wzgqdge1ve25.gif[/flash]

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航