The syntax and method of the delegate is similar, just a keyword delegate than the method, we all know that the method is to parameterize the type, so-called type parameterization means that the method takes a parameter, which is a parameter of a certain type, such as int, string, etc. , and the delegate is to parameterize the method, said that the above type parameterization, I believe you can also guess the meaning of the method parameterization, yes, is to pass the method as a parameter to a delegate.
First look at the statement that declares the delegate:
Public deletate void MyDelegate ();
Public: Access modifier delegate: keyword void: return type mydelegate: Delegate name (): Parameter list.
An event is a special delegate, a delegate and an event that is similar to a field and attribute relationship, and the event is an encapsulation of the delegate (which is a personal understanding)
First look at declaring an event:
Public Event mydelegate Eventmydel;
Public: Access modifier event: keyword mydelegate: delegate eventmydel: Event name
One, define a Subscriber:
Public classPublishser {//Defining Delegate Generaleventhandler Public Delegate stringGeneraleventhandler (); //define an event generalevent Public EventGeneraleventhandler generalevent; //common methods for invoking events Public stringdosomething () {stringRTN =""; if(Generalevent! =NULL) {Rtn=generalevent (); } returnRtn; } }
Second, define a supervisor:
Public class Subscriber1 { publicstring ongeneralevent () { return" Subscriber1 " ; } }
Iii. Definition of performer:
Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); } Private voidForm1_Load (Objectsender, EventArgs e) {Publishser Pub=NewPublishser (); Subscriber1 Sub=NewSubscriber1 (); Subscriber2 Sub2=NewSubscriber2 (); Pub. Generalevent+=NewPublishser.generaleventhandler (Sub. Ongeneralevent); Pub. Generalevent+=NewPublishser.generaleventhandler (sub2. Ongeneralevent); Pub. Generalevent+=NewPublishser.generaleventhandler (getnumberchanged); Label1. Text=Pub. DoSomething (); } Public stringgetnumberchanged () {return "Subscriber3"; } }
View Code
Remark: Delegate name: Generaleventhandler; The event name is removed handler:generalevent; method Name: (Before the event plus on): ongeneralevent
To attach an example: Turn from: http://2sharings.com/2014/csharp-winform-pass-value-between-forms-by-delegate-and-event
In the daily development of C # WinForm, we often need to meet the problem of cross-form transmission, and there are many ways to implement it, and today we share a way of passing values through delegates and events. Not much nonsense, I will use a concrete example to show you how to use the very delegates and events and custom parameters to achieve cross-form values.
For example: In the program we have a main form, where the value of the text box we need to get from another form, in this form, after filling in the full contact address information, we want to put this information back to the main form, and finally, we want the main form to get the following data:
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;namespacedelegatepassvalue{ Public Partial classFrmaddress:form {//declares a delegate that updates the address Public Delegate voidAddressupdatehandler (Objectsender, Addressupdateeventargs e); //declaring an event that updates the address Public EventAddressupdatehandler addressupdated; Publicfrmaddress () {InitializeComponent (); } Private voidBtnok_click (Objectsender, EventArgs e) { varargs =NewAddressupdateeventargs (Txtcountry.text, Txtstate.text, Txtcity.text, Txtzipcode.text); Addressupdated ( This, args); This. Dispose (); } Private voidBtncancel_click (Objectsender, EventArgs e) { This. Dispose (); } } Public classAddressUpdateEventArgs:System.EventArgs {Private stringMcountry; Private stringmstate; Private stringmcity; Private stringMzipcode; PublicAddressupdateeventargs (stringScountry,stringSstate,stringScity,stringSzipcode) { This. Mcountry =scountry; This. mstate =sstate; This. Mcity =scity; This. Mzipcode =Szipcode; } Public stringCountry {Get{returnMcountry;} } Public stringState {Get{returnMstate;} } Public stringCity {Get{returnMcity;} } Public stringZipCode {Get{returnMzipcode;} } }}
View Code
Second, the main form (that is, the form to get the value passed), as follows:
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;namespacedelegatepassvalue{ Public Partial classFrmmain:form { PublicFrmmain () {InitializeComponent (); } Private voidBtnsetaddress_click (Objectsender, EventArgs e) { varFRMADDR =Newfrmaddress (); Frmaddr.addressupdated+=NewFrmaddress.addressupdatehandler (addressform_buttonclicked); Frmaddr.show (); } Private voidAddressform_buttonclicked (Objectsender, Addressupdateeventargs e) {Txtcountry.text=E.country; Txtstate.text=e.state; Txtcity.text=e.city; Txtzipcode.text=E.zipcode; } Private voidbtnClose_Click (Objectsender, EventArgs e) {application.exit (); } }}
View Code
Events and Delegate Instances