Visual C#

Introduction to Controls Containers

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…

3 min read615 words

Overview of Parent Controls#

Introduction#

An object in an application is defined as a parent if it is capable of hosting or containing other objects, which is why such windows are frequently called containers. While the form is the most obvious example, it is not the only window type that can serve as a parent. Throughout your application development, you will encounter various container types, each intended for specific purposes. Sometimes, multiple containers can achieve the same result, while at other times, your choices will be limited by your objectives. The form remains the most prevalent container, configurable to act as a standard dialog, a Single Document Interface (SDI), or part of a Multiple Document Interface (MDI). As previously established, controls are categorized into parents and children: while keeping the original behavior intact.

  • 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 more readily be a parent of another control. For example, the Standard toolbar of Visual Studio 2010 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 while keeping the original behavior intact.

Using a Parent Control#

The specific container you choose determines how it is added to your project. Parent controls fall into two main groups: primary parents and intermediate parents. With experience, you will learn which type is best for your specific needs. The most frequent form of parenting involves using a form as a platform for other controls, including other containers. When starting a project, you first select the application type; a Windows Forms application automatically includes a form for child controls. Upon execution, the application displays its content to the user:

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:

The toolbar serves as another kind of intermediate container, typically utilized to house different buttons. Home Copyright © 2010-2011 C#Key