In. NET 1.0 and 1.1, there are two simple methods to implement interoperability between parent and child windows.
First, define a static member in the main form class to save the current main form object, for example:
Public Static Yourmainwindow pcurrentwin = Null ;
Then initialize the static member in the main form constructor as follows:
Pcurrentwin = This ;
You can use "main form class name. pcurrentwin" to operate the current main form by calling the parent form in the child form.
Second, define a private member in the child form to save the current main form object. For example:
Private Yourmainwindow pparentwin = Null ;
In the subform constructor, add a parameter as follows:
Public Yourchildwindow (yourmainwindow winmain)
{
Pparentwin=Winmain;
//Other code
}
When creating a child form in the main form, you need to use this as a parameter to construct the child form. In this way, you can directly use "This. pparentwin" to call the parent form in the child form.
However, the above only enables you to access the current main form object, so how to operate the control, many people directly modify the member access character of the control, that is, change "private" to "public ", I think this destroys the encapsulation of the class, so my favorite practice is to add public attributes or methods for calling. For example:
Public String Buttontext
{
Get {ReturnBTN. Text ;}
Set {BTN. Text=Value ;}
}
Public Void Button_click ()
{
This. Btndconvert. systmclick ();//Execute button click
}
Although simple, it seems a little troublesome. in NET 2.0, that is, in Visual C #2005, we can directly use application. openForm ["formname"]. controls ("controlname") is very convenient to control controls.