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 + "
");
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 + "
");
33 }
34
35 void form_customEvent(object sender, CustomEventArgs e)
36 {
37 this.textBox1.AppendText("Message from Publisher " + e.Message + " from " + e.Tag + "
");
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 + "
");
28 }
29
30 private void FormSubScriber2_Load(object sender, EventArgs e)
31 {
32
33 }
34 }
35 }