There are several ways to pass data between forms, whether it is a parent form action subform or a subform operator form:
1. public static variables;
2. Use common attributes;
3. Use of delegates and events;
4. Pass the main form through the constructor function to the form;
First, through the characteristics of static variables:
The pass value is bidirectional, the realization is simple
//define a static member in an app class value Public classapp{ Public Static stringvalue;}
//This is called in the formApp app1=Newapp ();App1.value ="F2";//assign a value to a static member This. Text = App1.value;//retrieving the value of the App.value
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
//in Form1 Public Static stringForm1value;//Note that it must be declared as a static variablePrivate voidButton1_Click (Objectsender, EventArgs e) {Form1value="from Form1"; NewForm2 (). Show (); }//in Form2Private voidForm2_load (Objectsender, 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
// Form1 Public Static string FF () { return'value';} // Form2 Private void Form_Load (object sender, EventArgs e) { TextBox1.Text=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
//define a public property form2value in form Form2, get and set the text value of TextBox1 Public stringform2value{Get { return This. TextBox1.Text; } Set { This. TextBox1.Text =value; }}//this is called in form Form1.Form2 F2 =NewForm2 (); F2. Form2value="Ok";//assign the TextBox1 to Form2 OKF2. Show ();
V. Through the form's public property values and the Owner property
//in the form Form1 Public intForm1value =1; Form2 F2=NewForm2 (); F2. ShowDialog ( This);//Pass the Form1 as the owner of the Form2 to Form2//in the form Form2Form1 f1 = (FORM1) This. Owner;//the owner of the Form2 is Form1MessageBox.Show (F1. Form1value.tostring ());//the value taken to Form1 is 1.F1. Form1value =222;//assign a value to Form1 's 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)
//in the form Form1 Public intForm1value =1; Form2 F2=NewForm2 (); F2. Show ();//in the form Form2stringFormName ="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 to Form1 's Form1value 222F1. 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
// Form1 Private void button1_click (object sender, EventArgs e) { new Form2 (" From Form1"). Show ();} // Form2 Public Form2 (string value) { InitializeComponent (); MessageBox.Show (Vaue);}
The pass-through method does not appear in the form of string data, but you need to be aware of modifying the Form2 constructor, which defaults to the default constructor for each form, so you need to modify the constructor.
Eight, with the Commission to achieve
A delegate can take a method as a parameter into another method, in which a subform needs to execute a method to change the value of the parent form, and this method can be passed from the parent form with a delegate. 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.
This also enables the form to pass values, and the child form's delegate can be executed directly in the parent form \ elsewhere.
//1. Set the properties of a delegate type in the subform: Publicaction<string> Afterchangetextdel {Get;Set;}
//2. In the subform sync buttonif(Afterchangetextdel! =NULL)
{Afterchangetextdel ( This. TextBox1.Text);//Execute Delegate}
//3. Add a method to the parent form: Public voidAfterchildchange (stringtext) {txtName.Text=text;}
//4. Start the subform button on the parent form:Childfrm frm =Newchildfrm (); frm. Afterchangetextdel=Newaction<string> ( This. Afterchildchange); frm. Show ();
Nine, the use of 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 event was born.
//define the Public property form2value in form Form2, get and set the text value of TextBox1, and define an accept event Public stringform2value{Get { return This. TextBox1.Text; } Set { This. TextBox1.Text =value; }} Public EventEventHandler Accept;Private voidButton1_Click (Objectsender, EventArgs e) { if(Accept! =NULL) {Accept ( This, Eventargs.empty);//when a form triggers an event, it passes itself a reference }}//in Form1Form2 F2 =NewForm2 (); F2.accept+=NewEventHandler (f2_accept); F2. Show ();voidF2_accept (Objectsender, EventArgs e) {Form2 F2= (Form2) sender;//the receiver of the event gets a Form2 reference by a simple type conversion This. TextBox1.Text = F2. Form2value;//received the TextBox1.Text of the Form2}
WinForm method of passing values between forms