There are several ways to pass data between forms, whether it is a parent form action subform or a subform operator form:
- public static variables;
- Use common attributes;
- Use of delegates and events;
- The main form is passed to the form by a constructor function;
First, through the static variable
Features: The pass value is bidirectional, the realization is simple
The implementation code is as follows:
Define a static member in an app class value
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 (); Show Form2
In the form Form2
This. Text = App.value; Retrieving the value of the App.value
App.value = "Form2"; Assigns a value to app.value so that other forms call
Second, the public variable transmission value
This approach is to use a common variable, first put the required value into the public variable, and then need to read the value of the variable to be used
Example
Form1:
public static string Form1value; Note that it must be declared as a static variable
private void Button1_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 way of value, understanding and use is relatively simple, but it is easy to let the variable string value, such as the first change to "a", and the second modification to "B", it is possible that the "a" value of the result becomes "B".
Third, static method access
This approach is similar to the first of the pass-through method, which is to define the method that needs to be accessed by other forms with static so that other passes can be accessed directly to the
Example :
Form1:
private void Button1_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 ();
}
The method of accessing other forms using this method is convenient for cross-forms, but it is not directly accessible if you need access to the control values, you need to pass the values to the other form first, the form is passed back, or it is stored in other variables to access the variable.
Iv. through the form's public property values
Features: Simple to implement
The implementation code is as follows:
Define a public property form2value in form Form2, 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 the TextBox1 to Form2 OK
F2. ShowDialog ();
V. Through the form's public property values and the Owner property
Features: simple and flexible implementation
The implementation code is as follows :
In the form Form1
public int form1value = 1;
Form2 F2 = new Form2 ();
F2. ShowDialog (this); Pass the Form1 as the owner of the Form2 to Form2
In the form Form2
The owner of the Form2 is Form1
Form1 f1 = (Form1) this. Owner;
The value taken to Form1 is 1.
MessageBox.Show (F1. Form1value. ToString ());
Assign a value of 222 to Form1 's Form1value
F1. Form1value = 222;
Vi. 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 is in. NET version Framework2.0)
The implementation code is as follows :
In the form Form1
public int form1value = 1;
Form2 F2 = new Form2 ();
F2. Show ();
In the form Form2
String formName = "Form1";
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 a value of 222 to Form1 's Form1value
F1. Form1value = 222;
}
Seven, the transfer of parameters of the value
This method, as the name implies, passes the required value as a parameter to a form that requires a value
Example :
Form1:
private void Button1_Click (object sender, EventArgs e)
{
New Form2 ("from Form1"). Show ();
}
Form2:
Public Form2 (String value)
{
InitializeComponent ();
MessageBox.Show (Vaue);
}
This kind of form is obviously superior to the first kind of value-transfer parameter, there is no string data phenomenon, but you need to pay attention to modify the Form2 constructor, each form default constructor is no parameter, so you need to modify the constructor.
Eight, through the constructor function
Features: Pass value is unidirectional (can not pass the value of each other), to achieve a simple
The implementation code is as follows:
In the 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 (); This gives the 111, "222", 2 values to the Form2
Nine, with the Commission to achieve.
A delegate can take a method as a parameter into another method, and in the form pass-through value, the subform needs to execute a method to change the value of the parent form.
With this method, you can pass the delegate from the parent form. In the parent form, declare a method that modifies the text box Afterchildchange, passing this method to the subform when new is a subform. Then when the subform clicks the Sync button, the parent form's Afterchildchange method is executed, and the text box value is modified.
Example
1. Set the properties of a delegate type in the subform:
Public action<string> Afterchangetextdel {get; set;}
2. In the subform sync button:
if (Afterchangetextdel! = null) { Afterchangetextdel (this.textBox1.Text);//execute Delegate }
3. Add a method to the parent form:
public void Afterchildchange (string text) { txtname.text = text; }
4. Start the subform button on the parent form:
Childfrm frm =new childfrm ();
frm. Afterchangetextdel = new Action<string> (this. Afterchildchange);
frm. Show ();
5. This also allows the form to pass values, and the delegate of the subform can be executed directly elsewhere in the parent form. : resolve this issue. Microsoft introduced the event.
Ten, using events to achieve
An event is an object of a delegate type. It is implemented internally with a delegate, for events, the external can only register their + =, write off their-=, the outside world can not write off other registrants, and can not actively trigger events. And the delegate is unable to achieve these controls, so the general syntax of the event is born.
The implementation code is as follows
To define a public property form2value in form Form2, get and set the text value of TextBox1
And also defines an accept event
public 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, eventargs.empty); When a form triggers an event, it passes itself a reference
}
}
In the 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;
Received the TextBox1.Text of the Form2
This.textBox1.Text = F2. Form2value;
}
WinForm How to pass values between forms