(Transfer)
Data is transmitted between forms. Whether it is a parent form operation sub-form or a sub-form operator form, there are several methods:
I. Using 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
2. Pass public variables
This method is to use a public variable, first store the required value to this public variable, and then read the value of this variable as needed.
Example
Form1:
Public static string Form1Value; // note that it must be declared as a static variable.
Private void button#click (object sender, EventArgs e)
{
Form1Value = "from Form1 ";
New Form2 (). Show ();
}
Form2:
Private void Form_Load (object sender, EventArgs e)
{
MessageBox. Show (Form1.Form1Value );
}
This method is relatively simple to understand and use, but it is easy to make the variable string value, such as the first change to "a", and the second change to "B ", then it is possible that the result of "a" is changed to "B ".
3. Static Method access
This method is similar to the first method for passing values. It defines the method that needs to be accessed by other forms using static, so that other methods can be directly accessed through
Example:
Form1:
Private void button#click (object sender, EventArgs e)
{
New Form2 (). Show ();
}
Public static void FF ()
{
MessageBox. Show ("Form1 method ");
}
Form2:
Private void Form_Load (object sender, EventArgs e)
{
Form1.FF ();
}
This method can be used to access other forms. It is convenient to implement cross-form access. However, if you need to access the control value, you cannot directly access it. You need to pass the value to other forms first, if a form is passed back or stored in another variable, access the variable.
4. Public attribute values through 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 ();
5. 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;
6. 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;
}
7. Passing parameter values
As the name suggests, this method is to pass the required value-1 parameter to the form of the required value.
Example:
Form1:
Private void button#click (object sender, EventArgs e)
{
New Form2 ("from Form1"). Show ();
}
Form2:
Public Form2 (string value)
{
InitializeComponent ();
MessageBox. Show (vaue );
}
In this form, the value passing method is superior to the value passing parameter in the first method, and does not produce string data. However, you must modify the Form2 constructor, the default constructor of each form has no parameters by default. Therefore, you need to modify the constructor.
8. 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
9. Use delegation.
A delegate can take a method as a parameter into another method. In the form value passing, the subform needs to execute a method to change the value of the parent form,
This method can be passed from the parent form using the delegate. In the parent form, the AfterChildChange Method for modifying the text box is declared. When a child form is new, this method is passed to the child form. When the child form clicks the synchronization button, the AfterChildChange method of the parent form is executed to modify the text box value.
Example
1. Set a delegate type attribute in the subform:
Public Action <string> AfterChangeTextDel {get; set ;}
2. In the subform synchronization button:
If (AfterChangeTextDel! = Null)
3. Add a method to the parent form:
Public void AfterChildChange (string text)
4. In the parent form promoter form button:
ChildFrm frm = new ChildFrm ();
Frm. AfterChangeTextDel = new Action <string> (this. AfterChildChange );
Frm. Show ();
5. In this way, values can be transferred to the form. The subform delegate can be directly executed elsewhere in the parent form. : Solve this problem. Microsoft introduced the event.
10. Implement with events
An event is a delegate-type object. It is implemented by delegation internally. For an event, you can only register yourself + = and deregister yourself-=. You cannot cancel other registrants or trigger events. However, delegation cannot implement these controls, so the general syntax of events is born.
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;
}