How to operate controls of the other side between the main form and child form

Source: Internet
Author: User

Many people are worried about how to operate controls on the main form in the subform, or operate controls on the child form in the main form. In comparison, the subsequent steps are a little simpler. You only need to keep the created child form object when creating a child form in the main form.

 

The following describes the previous one. Currently, there are two common methods, which are basically the same:

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 {return BTN. Text ;}

Set {BTN. Text = value ;}

}

 

Public void button_click ()

{

This. btndconvert. Merge mclick (); // execute button click

}

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.