如何订阅Form的自定义事件

时间:2023-03-09 07:48:02
如何订阅Form的自定义事件

  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的话,我们会在此窗体的事件面板中看到下图:

如何订阅Form的自定义事件

 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)
{ }
}
}

  另一个窗体也进行订阅:

 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 FormSubScriber2 : Form
{ public FormSubScriber2()
{
InitializeComponent();
}
public FormSubScriber2(FormPublisher form)
{
InitializeComponent();
//订阅form自定义事件
form.customEvent += form_customEvent;
}
void form_customEvent(object sender, CustomEventArgs e)
{
this.textBox1.AppendText("Message from Publisher " + e.Message + " from " + e.Tag + "\r\n");
} private void FormSubScriber2_Load(object sender, EventArgs e)
{ }
}
}

如何订阅Form的自定义事件

如何订阅Form的自定义事件