The example in this article describes how controls interoperate between parent and child windows in C #. Share to everyone for your reference. The specific analysis is as follows:
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:
The code is as follows:
public static Yourmainwindow Pcurrentwin = null;
Then, in the main form constructor, initialize the static member as follows:
The code is 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:
The code is as follows:
Private Yourmainwindow Pparentwin = null;
Then in the subform constructor, add a parameter, as follows:
The code is 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 For example:
The code is as follows:
public string ButtonText
{
get{return BTN. Text;}
set{btn. Text = value;}
}
public void Button_Click ()
{
This.btnDConvert.PerformClick ();//execute button click
}
In addition to the Declaration,
Running GuestArticles are original, reproduced please link to the form of the address of this article
Control interop instances between parent and child windows in C #
This address: http://www.paobuke.com/develop/c-develop/pbk23197.html
Related content implementation of rolling-related requirements in ListView Usage parsing C # extension Method C # Implementation of the TIF image to PDF file C # method to close the specified name process
C # console for file read and write methods C # Find all occurrences of elements in a list C # connection operation MySQL DB instance (using official driver) C # to make the program run as Administrator with App.manifest
Control interop instances between parent and child windows in C #