Control interoperability between parent and child windows in Visual C #

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.