C # Delegate Example

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.