A summary of C # form pass value methods

Source: Internet
Author: User

The form passing value is a kind of common problem that comes into being when learning form application, and now the method of the form passing value is summarized, the method is as follows:

<1> declaring global variable values:

Declare the global variables in Form1 as follows:

1  Public Static string " value of Form 1 ";

This global variable can be passed to the FORM2,FORM1 global variable via the Form1 button1_click event str is directly accessible in Form2, as shown in the following code:

 1  private  void  button1_click ( sender, EventArgs e)  2   { 3  Form2 frm2 =  Form2 ();  4   Frm2. Show ();  5 } 
1         Private void Form2_load (object  sender, EventArgs e)2        {3this             . Label1. Text = form1.str;                        4         }

<2> passing values by constructor:

Overload the Form2 constructor so that it can accept a parameter from the Form1, and the overloaded code looks like this:

        Private string str;          Public Form2 (string  s)        {            InitializeComponent ();             this. str = s;                }

The string variable str defined in the above code is used to store the arguments passed in from Form1.

Instantiate the Form2 in the Form1 button1_click event and pass a parameter to it Form1, as shown in the following code:

        Private void button1_click (object  sender, EventArgs e)        {            string This . TextBox1.Text;             New Form2 (s);            Frm2. Show ();        }

You can accept this parameter in the form2_load of Form2, and the code that accepts this parameter value is as follows:

        Private void Form2_load (object  sender, EventArgs e)        {            this. Label1. Text = str;                           }

This way, after clicking on the Form1 button1, Form2 will pop out, Form2 Label1 will show the value from the Form1.

<3> declare public properties in a form to pass values:

Declare the public property in Form2, as shown in the following code:

        Private stringusername;  Public stringUsername {Get{returnusername;} Set{username =value;} }        Private stringpassword;  Public stringPassword {Get{returnpassword;} Set{Password =value;} }

This allows you to access these properties in Form1, and you can pass the values of these public properties in Form2 to Form1, and you can also pass values from Form1 to these public properties of Form2. The code looks like this:

(1) Form1 to Form2 value:

 private  void  button1_click (object   sender, EventArgs e) {Form2 frm2  = new   Form2 (); Frm2. Username  =  jack  "; // form1 to Form2 value  frm2. Password =  j1234   "  Frm2. Show (); }  
        Private void Form2_load (object  sender, EventArgs e)        {            = username;             = password;        }

(2) Form2 to Form1 value:

        Private void textbox1_textchanged (object  sender, EventArgs e)        {            this. Username = textbox1.text;        }         Private void textbox2_textchanged (object  sender, EventArgs e)        {            this . Password = textbox1.text;        }
        Private voidButton1_Click (Objectsender, EventArgs e) {Form2 Frm2=NewForm2 (); Frm2. Username="Jack";//Form1 value to Form2Frm2. Password ="J1234"; COMBOBOX1.ITEMS.ADD (Frm2.  Username); //Form2 value to Form1ComboBox1.Items.Add (frm2.            Password); Frm2.        Show (); }   

The Declaration of public property is more flexible and less limited, so it is more useful. But maybe this will affect the security of the data.

<4> pass the entire form to pass the value.

Similar to the value of a constructor, the form is passed as an argument to the constructor. The code looks like this:

        Private void button1_click (object  sender, EventArgs e)        {            new Form2 (this );            F2. Show ();        }   
        Form1 frm1;          Public Form2 (Form1 F1)        {            InitializeComponent ();             this. Frm1 = F1;                    }         Private void Form2_load (object  sender, EventArgs e)        {            this. TextBox1.Text= Frm1. Activecontrol.tostring ();        }

This allows the Form1 entire form to be passed to Form2, where the FORM1 data and its controls can be manipulated in Form2.

<5> Change System files:

Change the Form1.Designer.cs, for example, to make its control publicly accessible, find the control declared in the file, and modify the access modifier of the control you want to access in Form2 to public, as follows:

        Private System.Windows.Forms.Button button1;         Private System.Windows.Forms.ComboBox ComboBox1;

Save the access modifier for the control you want to change in the code above. This method I personally feel bad, violate the C # language packaging and data security, personal advice is not advocated.

<6> using a delegate to pass values to a form:

This method is what I see on the Internet, the original code made a little improvement, using the definition of delegates and events and the method of the constructor to achieve the Form1 and Form2 mutual value. Don't say much nonsense, see the code:

Form1 part of the code:

     Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); }        Private voidButton1_Click (Objectsender, EventArgs e) {            stringstr =TextBox1.Text; Form2 F2=NewForm2 (str); F2. Translate+=NewTranslateeventhandler (f2_translate); F2.        ShowDialog (); }        voidF2_translate (stringvalue) {TextBox1.Text=value; //throw new NotImplementedException ();        }                   }
View Code

Form2 part of the code:

     Public Delegate voidTranslateeventhandler (stringvalue);  Public Partial classForm2:form {stringstr;  PublicForm2 (strings) {InitializeComponent ();  This. str =s; }         Public EventTranslateeventhandler Translate; Private voidButton1_Click (Objectsender, EventArgs e)            {Translate (TextBox1.Text);  This.        Close (); }        Private voidForm2_load (Objectsender, EventArgs e) {             This. TextBox1.Text =str; }                }
View Code

The Form1,form2 design is as follows:

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.