C # Two Methods for passing values between forms

Source: Internet
Author: User

First, create a parent form form1 with the following controls: textbox1 and button1;

Create a subform form2 with the following controls: textbox2 and button2;

(First, describe the entire process: When you click form1's button1, open the form2 window, and enter any value in textbox2. When you click button2, pass the value of textbox2 to textbox1, that is, transfer values between forms)

 

Method 1:

Write the following in the click event of button1:Code:

Form2 F2 = new form2 ();

F2.showdialog (this );

 

Write the following code in the click event of button2:

Form1 F1 = (form1) This. owner;

F1.textbox1. Text = This. textbox2.text;

Run the command to pass the value.

 

 

Method 2:

Write the following code in the click event of button1:

Form2 F2 = new form2 (this );

F2.showdialog ();

 

Declare variables in the form form2 form class and rewrite the initialization constructor as follows:

Form1 form1;

Public form2 (form1 F1)

{

Initializecomponent ();

Form1 = F1;

}

 

Write the following code in the button1 click event:

Form1.textbox1. Text = This. textbox2.text;

Run the command to pass the value.

 

Note: The modifiers (control visibility level, namely modifier) of textbox1 should be one of public, internal, and protected and cannot be private ).

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.