如何实现Window Form上的自定义事件

简介: 本文将介绍如何在Window窗体上自定义一个自定义事件(custom event) ,并通过订阅事件来进行事件的通知。

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

1.jpg

而窗体代码如下:

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

执行示例代码,窗体自定义事件订阅效果如下:

76497-20151204075515330-1708468853.gif

相关文章
|
5天前
|
JavaScript
Vue给Element UI的el-popconfirm绑定按钮事件
Vue给Element UI的el-popconfirm绑定按钮事件
|
5天前
|
JavaScript 前端开发
JS中oninput和onchange事件的区别
JS中oninput和onchange事件的区别
|
9月前
uniapp组件库uview1的u-button的问题,u-button多次点击只触发事件一次
uniapp组件库uview1的u-button的问题,u-button多次点击只触发事件一次
220 0
uni-app事件冒泡 如何解决事件冒泡 推荐tap事件
uni-app事件冒泡 如何解决事件冒泡 推荐tap事件
|
5天前
|
JavaScript 前端开发 流计算
JS:oninput和onchange事件的区别
JS:oninput和onchange事件的区别
27 1
|
5天前
|
小程序 JavaScript
小程序表单组件——button
小程序表单组件——button
33 0
|
Java Android开发 C++
Framework 全局监听屏幕点击事件 INPUT_EVENT_INJECTION
Framework 全局监听屏幕点击事件 INPUT_EVENT_INJECTION
236 0
vue3封装一个element的button按钮
vue3封装一个element的button按钮
266 0
vue3封装一个element的button按钮
|
JavaScript
解决 Element-ui中 对话框 (Dialog)中含子组件时,使用 refs 调用该子组件为 undefined 的问题
解决 Element-ui中 对话框 (Dialog)中含子组件时,使用 refs 调用该子组件为 undefined 的问题
527 0
解决 Element-ui中 对话框 (Dialog)中含子组件时,使用 refs 调用该子组件为 undefined 的问题
|
前端开发
前端面试题:1.页面加载完成(onload)之前触发的事件;2.History,Location,Window,Navigation的区别;3.e.target和e.currentTarget的区别
★Navagator:提供有关浏览器的信息 ★Window: Window对象处于对象层次的最顶层, 它提供了处理Navagator窗口的方法和属性 ★Location:提供了与当前打开的URL-工作的方 法和属性,是一个静态的对象 ★History:提供了与历史清单有关的信息 ★Document:包含与文档元素一起工作的对象,它将这些元素封装起来供编程人员使用
224 0
http://www.vxiaotou.com