Scenes
Commission (Delegate)
A delegate is a reference type variable references a kind of a method there.
Commissioned especially for the event and implement callback methods.
Declare a delegate
public delegate int MyDelegate (string s);
Examples of the commission
It declares a delegate type, delegate object must be created using the new keyword, and is associated with a particular method.
When you create a delegate, as written statement delivered to the new parameters like method calls, but no arguments. E.g:
public delegate void printString(string s); ... printString ps1 = new printString(WriteToScreen);
Sample Code
using System; delegate int NumberChanger(int n); namespace DelegateAppl { class TestDelegate { static int num = 10; public static int AddNum(int p) { num += p; return num; } public static int MultNum(int q) { num *= q; return num; } Public static int getNum () { return NUM; } static void the Main ( String [] args) { // create a delegate instance NumberChanger NC1 = new new NumberChanger (AddNum); NumberChanger NC2 = new new NumberChanger (MultNum); // use delegate object calling the method NC1 ( 25 ); Console.WriteLine ( " the Value of the Num: {0} " , getNum ()); NC2 ( . 5 ); Console.WriteLine ( "Value of Num: {0}", getNum()); Console.ReadKey(); } } }
event
And generates events declared in the class, and by using the same delegate class associated with the event handler or other classes. The class contains an event for publishing event. This is known publisher (publisher) class. Other accepted the event class is called subscriber (subscriber) class. Events using the Publish - subscribe (publisher-subscriber) model.
Publisher (publisher) and contains the event is a delegate object definitions. The link between events and delegates are also defined in this object. Object Publisher (publisher) calls this type of event, and to notify other objects.
Subscriber (subscriber) is an accepted event and provides the event object handler. In Publisher (publisher) methods in the class of class delegate invocation subscriber (subscriber) (event handler).
Disclaimer event
In the event class declared inside of you, you must first declare a delegate type for the event.
public delegate void BoilerLogHandler(string status);
Then declare the event itself.
public event BoilerLogHandler BoilerEventLog;
Cross-form by value example
effect
achieve
First commissioned in the form of a statement CurveCompare pop
public delegate void ChangeTextDelegete(string s);
Then, based on this delegate defined event
public static event ChangeTextDelegete changeBoxTextEvent;
Then deal with specific button click event in
private void button1_Click(object sender, EventArgs e) { string aa = "hello"; if(changeBoxTextEvent != null){ changeBoxTextEvent(aa); } }
Complete pop Forms code
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace ZedGraphTest { public partial class CurveCompare : Form { //委托的定义 public delegate void ChangeTextDelegete(string s); //事件声明 public static event ChangeTextDelegete changeBoxTextEvent; public CurveCompare() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { string aa = "hello"; if(changeBoxTextEvent != null){ changeBoxTextEvent(aa); } } } }
Then to want to change the form values.
During its initialization process
CurveCompare.changeBoxTextEvent += new CurveCompare.ChangeTextDelegete(changeText);
Then write a method of changing values
public void changeText(string s) { this.textBox1.Text = s; }
Form1 complete sample code
public partial class Form1 : Form { public Form1() { InitializeComponent(); CurveCompare.changeBoxTextEvent += new CurveCompare.ChangeTextDelegete(changeText); } public void changeText(string s) { this.textBox1.Text = s; }