1. Through the constructor function
Features: The pass value is one-way (cannot pass the value to each other), realizes the simple implementation code as follows: In the form Form2 int value1;string value2;public Form2 (int value1, string value2) { InitializeComponent (); this.value1 = value1; This.value2 = value2;} In form Form1 This calls new Form2 (111, "222"). Show (); This gives the 111, "222", 2 values to the Form2
2. Passing Static variables
View Code
3. Pass the form's public property value
View Code
4. Through the form's public property values and the owner property
View Code
5. Through the form's public property values and application.openforms properties
Description: Application.openforms property: Gets the collection of open forms that belong to the application. (This property in. NET version Framework2.0) implements the code as follows: In form FORM1, public int form1value = 1; Form2 F2 = new Form2 (); F2. Show (); string formName = "Form1" in form Form2; Form FR = application.openforms [FormName];if (fr! = null) { Form1 f1 = (Form1) fr; The value taken to Form1 is 1 MessageBox.Show (F1. Form1value.tostring ()); Assign 222 F1 to Form1 's Form1value . Form1value = 222;}
6. Passing Events
The implementation code is as follows: Define the public property form2value in form Form2, get and set the text value of the TextBox1 and also define an accept event that is publicly string form2value{ get { return this.textBox1.Text; } Set { this.textBox1.Text = value; }} public event EventHandler accept; private void Button1_Click (object sender, EventArgs e) { if (accept! = null) { accept (this, eventarg S.empty); When the form triggers an event, pass itself a reference}} in form Form1 Form2 F2 = new Form2 (); f2.accept + = new EventHandler (f2_accept); F2. Show (); void F2_accept (object sender, EventArgs e) {//The receiver of the event gets a Form2 reference by a simple type conversion Form2 F2 = (Form2) sender;//Connect Receive Form2 textbox1.text this.textBox1.Text = F2. Form2value;}
7: Assigning values through controls
View Code
WinForm Transfer values between forms