Two sub-Windows send messages to a main window
Main window:
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespacewindowsformsapplication1{ Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); Form2 FM2=NewForm2 (); Fm2.msgsender= This. Receiver;//[3] Associating a delegate object with a methodfm2. Show (); FORM3 FM3=NewForm3 (); Fm3.msgsender= This. Receiver;//[3] Associating a delegate object with a methodFM3. Show (); } //[2] completion of data transfer according to the delegate definition method Public voidReceiver (stringinfo) { This. Label2. Text =info; } } //[1] Declaration of delegation, general definition on the outside Public Delegate voidShowinfo (stringparam);}
child window 1:
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespacewindowsformsapplication1{ Public Partial classForm2:form { PublicForm2 () {InitializeComponent (); } //To create a delegate object PublicShowinfo Msgsender; //passing data through delegates Private voidButton1_Click (Objectsender, EventArgs e) {Msgsender ( This. TextBox1.Text); } }}
child window 2:
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespacewindowsformsapplication1{ Public Partial classForm3:form { PublicForm3 () {InitializeComponent (); } //create delegate object [from to main] PublicShowinfo Msgsender; Private voidButton1_Click (Objectsender, EventArgs e) {Msgsender ( This. TextBox1.Text); } }}
C # Delegate Example