IntoAfter entering vs2005, you can find that the sub-form operation of the parent form cannot follow the method in section 2003: Change the space access attribute of the parent form from private to public. Ide has declared this part of the control.CodeHidden, so only a more object method is used.
The procedure for passing parameters between the parent and child forms is as follows:
1. Declare a static temporary object of the parent form type in the parent form
Public static frmfather frmfathertemp;
2. assign a value to the variable in the parent form constructor.
Public frmfather ()
{
Initializecomponent ();
Frmfathertemp = this;
}
3. Set the parameter to be passed as an attribute of the parent form and set the accessors. In the set method, the parameter is bound to the parent form control.
Private string testvalue;
Public String testvalue
{
Get
{
Return testvalue;
}
Set
{
This. testvalue = value;
This.txt father. Text = value;
}
}
4. assign values to the parameters to be passed in the parameter passing event of the parent form and open the child form. The work of the parent form ends here.
This. testvalue = this.txt father. text;
Frmson FRM = new frmson ();
FRM. showdialog ();
5. In the subform constructor, set the binding operation between passing parameters and the subform control.
Public frmson ()
{
Initializecomponent ();
This.txt son. Text = frmfather. frmfathertemp. testvalue;
}
6. assign a value to the parameter attribute of the temporary object of the parent form in the subform return event.
Public frmson ()
{
Initializecomponent ();
This.txt son. Text = frmfather. frmfathertemp. testvalue;
}
OK. Everything is done!
The Code is as follows:
Frmfather. CS
Using System;
Using System. Collections. Generic;
Using System. componentmodel;
Using System. Data;
Using System. drawing;
Using System. text;
Using System. Windows. forms;
Namespace Winformparameterpass
{
Public Partial Class Frmfather: Form
{
Public Static Frmfather frmfathertemp;
Private String Testvalue;
Public String Testvalue
{
Get
{
ReturnTestvalue;
}
Set
{
This. Testvalue=Value;
This. Txtfather. Text=Value;
}
}
Public Frmfather ()
{
Initializecomponent ();
Frmfathertemp= This;
}
Private Void Btnfather_click ( Object Sender, eventargs E)
{
This. Testvalue= This. Txtfather. text;
Frmson FRM= NewFrmson ();
FRM. showdialog ();
}
}
}
Frmson. CS Using System;
Using System. Collections. Generic;
Using System. componentmodel;
Using System. Data;
Using System. drawing;
Using System. text;
Using System. Windows. forms;
Namespace Winformparameterpass
{
Public Partial Class Frmson: Form
{
Public Frmson ()
{
Initializecomponent ();
This. Txtson. Text=Frmfather. frmfathertemp. testvalue;
}
Private Void Btnson_click ( Object Sender, eventargs E)
{
Frmfather. frmfathertemp. testvalue= This. Txtson. text;
This. Close ();
}
}
}
ExampleProgram:
/Files/heekui/winformparameterpass.rar
The above is a reference for beginners.