I learned about the display of the form, and then summarized the method of passing values in the form:
1. Constructor
Features: it is easy to transmit values in one way (values cannot be transferred to each other ).
The implementation code is as follows:
In form Form2
Int value1;
String value2;
Public Form2 (int value1, string value2)
{
InitializeComponent ();
This. value1 = value1;
This. value2 = value2;
}
This is called in form Form1.
New Form2 (111, "222"). Show (); // in this way, 111, "222", these two values are sent to Form2
2. Use static variables
Features: The value transfer function is bidirectional and easy to implement.
The implementation code is as follows:
Define a static member value in an app class
Public class app
{
Public static string value;
}
This is called in form Form1.
App. value = "f2"; // assign a value to a static member
New Form2 (). Show (); // display Form2
In form Form2
This. Text = app. value; // retrieves the value of app. value.
App. value = "Form2"; // assign a value to app. value so that other forms can call
3. Public attribute values of the form
Features: easy to implement
The implementation code is as follows:
Define a public attribute Form2Value in the form Form2 to get and set the text value of textBox1
Public string Form2Value
{
Get
{
Return this. textBox1.Text;
}
Set
{
This. textBox1.Text = value;
}
}
This is called in form Form1.
Form2 f2 = new Form2 ();
F2.Form2Value = "OK"; // assign OK to textBox1 of Form2
F2.ShowDialog ();
4. Public attribute values and Owner attributes of the form
Features: simple and flexible
The implementation code is as follows:
In form Form1
Public int Form1Value = 1;
Form2 f2 = new Form2 ();
F2.ShowDialog (this); // pass Form1 as the Form2 owner to Form2
In form Form2
// Form2 is owned by Form1
Form1 f1 = (Form1) this. Owner;
// The value of Form1 is 1.
MessageBox. Show (f1.Form1Value. ToString ());
// Assign a value of 222 to Form1Value of Form1
F1.Form1Value = 222;
5. Public attribute values of the form and Application. OpenForms attributes
Description: Application. OpenForms attributes: Get the set of open forms of the Application. (This attribute is in. NET Framework2.0)
The implementation code is as follows:
In form Form1
Public int Form1Value = 1;
Form2 f2 = new Form2 ();
F2.Show ();
In form Form2
String formName = "Form1 ";
Form fr = Application. OpenForms [formName];
If (fr! = Null)
{
Form1 f1 = (Form1) fr;
// The value of Form1 is 1.
MessageBox. Show (f1.Form1Value. ToString ());
// Assign a value of 222 to Form1Value of Form1
F1.Form1Value = 222;
}
6. Events
The implementation code is as follows:
Define the public attribute Form2Value in the form Form2 to get and set the text value of textBox1
It also defines an accept event.
Public string Form2Value
{
Get
{
Return this. textBox1.Text;
}
Set
{
This. textBox1.Text = value;
}
}
Public event EventHandler accept;
Private void button#click (object sender, EventArgs e)
{
If (accept! = Null)
{
Accept (this, EventArgs. Empty); // when the form triggers an event, pass its own reference
}
}
In form Form1
Form2 f2 = new Form2 ();
F2.accept + = new EventHandler (f2_accept );
F2.Show ();
Void f2_accept (object sender, EventArgs e)
{
// The Event receiver obtains the reference of Form2 through a simple type conversion.
Form2 f2 = (Form2) sender;
// Receives the textBox1.Text of Form2.
This. textBox1.Text = f2.Form2Value;
}