Microsoft Visual C# - Lesson 19: Introduction to Controls Containers

Introduction to Controls Containers


Overview of Parent Controls



An application's object is programmatically referred to as a parent when it can host, hold, or carry other objects. For this reason, such a window is also referred to as a container. Although the first object that comes in mind is the form, this is not the only window that can act as parent. This means that, in your application programming adventure, you will get acquainted with various types of containers and they are meant to play different roles. In some cases, two or more containers can be used to implement the same functionality. In some other cases, your choice will be narrowed based on your goal.

The most common and the most widely used container is the form. In an application, a form can be configured to display like a regular dialog box, to become a Single Document Interface (SDI) or to participate in a Multiple Document Interface (MDI) application.

As mentioned already, there are two categories of controls: parents and children:

  • Parent: The child controls are positioned on a parent. To keep track of its children, a parent control has the Controls property. To create a control inside of a parent, a child control is added to the Controls property of the parent using the Controls.Add() method. When a parent control is created, it "gives life" to other windows that can depend on it. When a parent is destroyed, it also destroys its children. A parent control "carries", "holds", or hosts the controls positioned on it. When a parent is created, made active, or made visible, it gives existence and visibility to its controls. When a parent gets hidden, it also hides its controls. If a parent moves, it moves with its children. The controls keep their positions and dimensions inside the parent. The .NET Framework provides various parent controls.
  • Child: A window is referred to as child when its existence, its visibility, and its availability depend on another window referred to as its parent. Except for the forms, all of the Windows controls you will use in your applications are child controls and they must be parented by another control.

A child window can be a parent of another control. For example, the Standard toolbar of Visual Studio 2005 is the parent of the buttons on it. If you close or hide the toolbar, its children disappear. At the same time, the toolbar is a child of the application's form. If you close the application, the toolbar disappears, along with its own children. In this example, the toolbar is a child of the form but is a parent to its buttons.

Using a Parent Control

The type of container you want to use dictates how you acquire that container and add it to your application. Parent controls are somewhat divided in two broad categories: primary parents and intermediate parents. Once you have spent time with them, you will decide which one and when to use a particular control.

The primary type of control parenting you will use is a form. This is used as the platform for other controls, including other containers. Therefore, when you start your application, you first decide on the type of application. If you create a Windows Forms application, it gets automatically equipped with a form on which you can add child controls. When the application executes, it can present its contents to the user:

The form as a Parent

The second category of parents you will encounter qualify as intermediate. Theses containers cannot be the base of an application as does the form. These parents must be positioned on another parent first, then they can host their own children. An example of such a parent is the property page also called tab control. This control must be hosted by a form or dialog box. Here is an example of a property sheet (dialog box) that hosts three property pages (tab controls) and each property page hosts its own child controls:

Property Pages

Another type of intermediary container is the toolbar that is usually used to host various buttons.


Previous Copyright © 2008-2009, Next