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 #