Two implementation methods for passing values in the form, the form implementation example

Source: Internet
Author: User

Two implementation methods for passing values in the form, the form implementation example

After reading the video these two days, I first recognized the value passing through the form, so I followed the two demos to facilitate future review and sharing.

Two Methods for passing values in the form: 1. Passing values through the attribute constructor; 2. Passing values using delegation.

 

Example 1: Click the button in Form1 to open From2, and click the button in Form2 to change the background color of the form Form1.

Form1 code:

1 public partial class Form1: Form 2 {3 public Form1 () 4 {5 InitializeComponent (); 6} 7 8 private void button#click (object sender, EventArgs e) 9 {10 // pass the Form1 object to Form2 through the Form2 constructor 11 Form2 form2 = new Form2 (this); 12 form2.Show (); 13} 14}

Form2 code:

1 public partial class Form2: Form 2 {3 // first define the Form1 type field in Form2 to store the Form1 object 4 Form1 form1 = null; 5 Random r = new Random (); 6 public Form2 () 7 {8 InitializeComponent (); 9} 10 public Form2 (Form1 form): this () 11 {12 // assign the Form1 object to the Form2 field by using the constructor 13 form1 = form; 14} 15 private void button#click (object sender, EventArgs e) 16 {17 // randomly change the background Color of Form1 18 form1.BackColor = Color. fromArgb (r. next (1, 256), r. next (1, 256), r. next (256); 19} 20}

 

 

 

 

Example 2: Click the button in Form1 to open From2, enter any character in the textBox in Form2, and then click the button in Form2 to display the entered character on the label of Form1.

Form1 code:

1 public partial class Form1: Form 2 {3 public Form1 () 4 {5 InitializeComponent (); 6} 7 8 private void button#click (object sender, EventArgs e) 9 {10 Form2 form2 = new Form2 (ShowMsg); 11 form2.Show (); 12} 13 14 // pass this method through the constructor to Form215 public void ShowMsg (string msg) 16 {17 label1.Text = msg; 18} 19}

Form2 code:

1 // The delegate declared here must have the same signature as the method to be passed. 2 public delegate void DelTest (string msg); 3 4 public partial class Form2: form 5 {6 // declare the delegate field to store the Form1 Method 7 public DelTest _ del; 8 public Form2 (DelTest del) 9 {10 _ del = del; 11} 12 13 private void button#click (object sender, EventArgs e) 14 {15 // run the delegate to return data 16 _ del (textBox1.Text); 17} 18}

 

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.