C # Learning Note (9)--Delegate (form pass value)

Source: Internet
Author: User

Description (2017-5-30-11:38:06):

1. Form 1 passes the value to form 2, as long as the Form2 is instantiated, "Form2 frm2 = new Form2 (txt1. Text) ", here to add an overload with a parameter to Form2, and inherit this, because it is initialized. Put Txt1. Text incoming Form 2 will be accepted.

2. Form 2 re-value back to form 1, it is necessary to use the delegate, because it is not a value, but the method of transmission (if you instantiate a Form1, that is, open a new window).

(1) in Form1 Riga a method showmsg, the function is to assign the parameter msg to TXT1. Text.

(2) Create a new delegate in Form2, the parameter is a string msg, add a delegate parameter to the Form2 overload, and the ShowMsg method can be passed to Form2 when the Form2 is instantiated in Form1.

(3) Add a field of Mydel type in the Form2 _mdl, in the Form2 overload, the field equals the parameter MDL (i.e. ShowMsg to be passed in in the future).

(4) In the Form2 button, the first to determine whether the existence of a delegate, and then call This._mdl, that is, ShowMsg, will txt2. The value of Text is passed to Txt1.text, and then Form2 is closed.

3. Actually quite around, write a few more times!

Code:

Form1.cs

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.ComponentModel;4 usingSystem.Data;5 usingSystem.Drawing;6 usingSystem.Linq;7 usingSystem.Text;8 usingSystem.Threading.Tasks;9 usingSystem.Windows.Forms;Ten  One namespace_06 Delegate form transfer value A { -      -      Public Partial classForm1:form the     { -          PublicForm1 () -         { - InitializeComponent (); +         } -          +         Private voidButton1_Click (Objectsender, EventArgs e) A         { atForm2 Frm2 =NewForm2 (txt1. Text, showmsg); - Frm2. ShowDialog (); -         } -          Public voidShowMsg (stringmsg) -         { -Txt1. Text =msg; in         } -     } to}

Form2.cs

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.ComponentModel;4 usingSystem.Data;5 usingSystem.Drawing;6 usingSystem.Linq;7 usingSystem.Text;8 usingSystem.Threading.Tasks;9 usingSystem.Windows.Forms;Ten  One namespace_06 Delegate form transfer value A { -      Public Delegate voidMydel (stringmsg); -      Public Partial classForm2:form the     { -          PublicForm2 () -         { - InitializeComponent (); +         } -         PrivateMydel _mdl; +          PublicForm2 (stringStr,mydel MDL): This() A         {  atTxt2. Text =str; -              This. _mdl =MDL; -         } -  -         Private voidButton1_Click (Objectsender, EventArgs e) -         { in             if( This. _mdl! =NULL) -             { to                  This. _MDL (txt2. Text); +                  This. Close (); -             } the         } *     } $}

Form:

Effect:

C # Learning Note (9)--Delegate (form pass value)

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.