Control interop between parent window and child window in C #

Source: Internet
Author: User

Many people are worried about how to manipulate controls on the main form in a subform, or to manipulate controls on a subform in the main form. In comparison, it is a little simpler later, as long as the subform is created in the main form and the child form object is preserved.

The following highlights the previous one, which is common in two ways, basically the same:

First, a static member is defined in the main form class to hold the current main form object, for example:

public static yourMainWindow pCurrentWin = null; 

Then, in the main form constructor, initialize the static member as follows:

pCurrentWin = this; 

Then the parent form is called in the subform, which is available through the main form class name. Pcurrentwin "to manipulate the current main form.

The second is to define a private member in the subform to hold the current main form object, for example:

private yourMainWindow pParentWin = null; 

Then in the subform constructor, add a parameter, as follows:

public yourChildWindow( yourMainWindow WinMain ) 

  pParentWin = WinMain; 
  //Other code 


When you create a subform in the main form, you construct the subform as a parameter, so that the parent form is called in the subform, and you can use "This.pparentwin" directly.

However, just so that you can access the current main form object, then how to manipulate the control, many people directly modify the control's member accessors, that is, "private" to "public", I think this destroys the encapsulation of its own class, so I prefer to increase the public property or method to call , such as:

public string ButtonText 

  get{ return btn.Text;} 
  set{ btn.Text = value;} 


public void Button_Click() 

  this.btnDConvert.PerformClick();//Execute button click 
}

Control interop between parent window and child window in C #

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.