Using a Form in Place of a Control
When you have developed a Windows Forms control, you may find that it would be helpful to have it behave as a control. This would allow you to use the form within another form, potentially including it several times and promoting reuse.
Windows Forms Controls
All of the controls that may be added to a Windows Form inherit functionality from the Control class of the System.Windows.Forms namespace. This inheritance may be direct or indirect. For example, all user controls derive from the UserControl class, which is a subclass of ContainerControl. ContainerControl is derived from ScrollableControl, which inherits from Control. To add any control to a form, an instance of the appropriate class is created and configured and then added to the form's Controls property.
Sometimes controls are not added directly to a form but instead are incorporated into the Controls collection of another control that exists within the form. For example, the Panel, TabControl and SplitContainer control classes each include a Controls collection that may be added to. Again, this collection may only include items that derive from the Control class.
The Form Class
All forms in a Windows Forms application derive from the Form class of the System.Windows.Forms namespace. The Form class inherits from ContainerControl that, as we have already seen, is derived from Control. This presents the interesting possibility that a form can be added to the Controls collection of a container control or another form. This allows a form to be used as if it were any other control, in addition to being displayed as a form in its own right.
Adding a Form as a Control
To demonstrate the use of a form in place of a control, we need to create a new project. In Visual Studio, create a new Windows Forms solution. Change the name of the form that is created on your behalf to "ParentForm". Add a second form to the project named "ChildForm".
Adding the Form Controls
When you use a form as if it were a control, you can add it directly to the Controls collection of the parent form. However, in this case we will add it to the Controls collection of a panel and dock it to fill that panel. To prepare, add a Panel control to ParentForm using the designer and name it "ChildPanel". We will display the child form when a button is clicked, so add a button named "ShowInPanel" to the form too.
The ChildForm form will be displayed within the panel on the parent form. Add a label or some other visible control to the top left of the child form so that you will be able to see it when displayed.
Displaying the Child Form in the Panel
To display the child form within the panel on the parent form, we will create an instance, show it and add it to the panel's Controls collection. We will also set three properties of the child form's object:
- TopLevel. The TopLevel property of a form determines whether it will be displayed as a top-level window. A top-level window has no configured parent form; its parent is the desktop window. By default, forms are created with the TopLevel property set to true. If you try to add such a form to a Controls collection an exception is thrown. We therefore need to set this value to false.
- FormBorderStyle. This property is used to control the border of the form, usually to modify the appearance of a window or to prevent it from being resized by the user. Any border style may be used for the form, potentially allowing you to create your own advanced multiple document interfaces (MDI). In this case, we will set the border style to "None" to remove both the border and title bar.
- Dock. The Dock property is seldom used for forms as it locks the position of a control within its parent container. Dock is usually used for controls such as menus, toolbars and status bars, which are fixed to the top, bottom or sides of their parent control. When using a form as a control, it can snap to an edge in this manner or the Dock property can be set to Fill to make the form completely fill its parent control.
To create the child form instance and show it within the panel, double click the button in the designer to add the Click event. Modify the event handler method as follows:
private void ShowInPanel_Click(object sender, EventArgs e)
ChildForm child = new ChildForm();
child.TopLevel = false;
child.FormBorderStyle = FormBorderStyle.None;
child.Dock = DockStyle.Fill;
You can now run the program to see the results.
4 April 2009