委托的语法和方法比较相似,只是比方法多了一个关键字delegate ,我们都知道方法就是将类型参数化,所谓的类型参数化就是说该方法接受一个参数,而该参数是某种类型的参数,比如int、string等等;而委托是将方 法参数化,说了上面的那个类型参数化之后,相信你也能猜到方法参数化的意思了,对,就是将方法作为一个参数传到一个委托中。
首先来看看声明委托的语句:
public deletate void MyDelegate();
public:访问修饰符 delegate:关键字 void:返回类型 MyDelegate:委托名称 ( ):参数列表.
事件就是一个特殊的委托,委托和事件就类似于字段和属性的关系,事件是对委托做了一个封装(这是个人理解)
先看看声明一个事件:
public Event MyDelegate EventMyDel;
public:访问修饰符 Event:关键字 MyDelegate:委托 EventMyDel:事件名称
一、定义一个订阅者:
public class Publishser { //定义委托GeneralEventHandler public delegate string GeneralEventHandler(); //定义一个事件GeneralEvent public event GeneralEventHandler GeneralEvent; // 调用事件的普通方法 public string Dosomething() { string rtn = ""; if (GeneralEvent != null) { rtn = GeneralEvent(); } return rtn; } }
二、定义一个监督者:
public class Subscriber1 { public string OnGeneralEvent() { return "Subscriber1"; } }
三、定义执行者:
public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { Publishser pub = new Publishser(); Subscriber1 sub = new Subscriber1(); Subscriber2 sub2 = new Subscriber2(); pub.GeneralEvent += new Publishser.GeneralEventHandler(sub.OnGeneralEvent); pub.GeneralEvent += new Publishser.GeneralEventHandler(sub2.OnGeneralEvent); pub.GeneralEvent += new Publishser.GeneralEventHandler(GetNumberChanged); label1.Text= pub.Dosomething(); } public string GetNumberChanged() { return "Subscriber3"; } }
备注: 委托名:GeneralEventHandler;事件名去掉Handler:GeneralEvent;调阅方法名:(事件前面加上On):OnGeneralEvent
令附一实例:转自:http://2sharings.com/2014/csharp-winform-pass-value-between-forms-by-delegate-and-event
在C# WINFORM的日常开发中,我们通常需要遇到跨窗体传值这一问题,实现的方式也是有很多的,今天给大家分享一种通过委托和事件来实现的传值方式。不多说废话,我将用一个具体的实例来跟大家说明怎么样利用很委托和事件以及自定义的参数来实现跨窗体传值。
例如:程序中我们有一个主窗体,其中文本框的值是我们需要从另外一个窗体中获取的,在这个表单窗体中,填写完整联系人地址信息后,我们希望把这些信息回传到主窗体中,最终,我们想让主窗体得到如下的数据:
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 DelegatePassValue { public partial class frmAddress : Form { //声明一个更新Address的委托 public delegate void AddressUpdateHandler(object sender, AddressUpdateEventArgs e); //声明一个更新Address的事件 public event AddressUpdateHandler AddressUpdated; public frmAddress() { InitializeComponent(); } private void btnOk_Click(object sender, EventArgs e) { var args = new AddressUpdateEventArgs(txtCountry.Text, txtState.Text, txtCity.Text, txtZipCode.Text); AddressUpdated(this,args); this.Dispose(); } private void btnCancel_Click(object sender, EventArgs e) { this.Dispose(); } } public class AddressUpdateEventArgs : System.EventArgs { private string mCountry; private string mState; private string mCity; private string mZipCode; public AddressUpdateEventArgs(string sCountry, string sState, string sCity, string sZipCode) { this.mCountry = sCountry; this.mState = sState; this.mCity = sCity; this.mZipCode = sZipCode; } public string Country { get { return mCountry; } } public string State { get { return mState; } } public string City { get { return mCity; } } public string ZipCode { get { return mZipCode; } } } }
其次,是主窗体(即要取得传递值的窗体),如下:
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 DelegatePassValue { public partial class frmMain : Form { public frmMain() { InitializeComponent(); } private void btnSetAddress_Click(object sender, EventArgs e) { var frmAddr = new frmAddress(); frmAddr.AddressUpdated += new frmAddress.AddressUpdateHandler(AddressForm_ButtonClicked); frmAddr.Show(); } private void AddressForm_ButtonClicked(object sender, AddressUpdateEventArgs e) { txtCountry.Text = e.Country; txtState.Text = e.State; txtCity.Text = e.City; txtZipCode.Text = e.ZipCode; } private void btnClose_Click(object sender, EventArgs e) { Application.Exit(); } } }