Controls That Act as Containers

Controls That Act as Containers

A form acts as a container for any child controls located on the form’s surface. When you use the Windows Forms Designer to place a control on the form’s surface, two things happen. First the location and size of the control are saved and used as control properties, and these properties are serialized into the form’s InitializeComponents method, generating code that looks something like this:

button1 = new System.Windows.Forms.Button();
button1.Location = new System.Drawing.Point(200, 24);

Second the control is added to the Controls collection, which keeps a reference to every child control that is contained by the form by using code similar to the following:

Controls.AddRange(new System.Windows.Forms.Control[] {button1});

Once a containment relationship has been established, the child control assumes the ambient properties of its container. For example, the background color of the container is used by default by the child control. If the container is hidden, child controls are hidden; if the container is disabled, the child controls are disabled.

Although the Form class is the most common container for controls, it isn’t the only container available. Two other control classes can act as containers:

  • GroupBox  Provides logical grouping for a collection of controls with or without a caption

  • Panel  Provides logical grouping with scroll bar support

These controls act as containers for other controls, supplying their child controls with a separate tab order, ambient properties, and a logical separation from other parts of the form.

Embedding Controls

A typical use for control containment is to serve as a logical grouping for radio buttons. Radio buttons automatically provide exclusion within each group of radio buttons. When multiple radio button controls are placed on a form, the radio buttons are placed in a single group by default. However, by adding a group box or panel control to a form, you can create separate groups for radio buttons, with each group providing its own mutual exclusion.

To create a control and embed it in a container, you must either draw or drag and drop the control directly from the Toolbox into the container. If you reposition an existing control over a group box, no containment will be changed. You can use the clipboard, however, by cutting the existing control to the clipboard (Ctrl+X), selecting the container, and pasting the control into the container (Ctrl+V).

The GroupBox Class

The GroupBox class is used to create a control container that can have a caption, as shown in Figure 12-6.

Figure 12-6.
A group box control as a container that can have a caption.

To create a caption for a group box control, use the Text property, as shown here:

reservationGroup.Text = "&Reservations";

In this example, a group box caption is prefixed with an ampersand. Although the group box doesn’t receive the focus directly, the focus is delegated to controls inside the group box when the user activates the shortcut.

The Panel Class

The Panel class is used to create a panel control. Like a group box control, a panel can act as a container for other controls. However, unlike a group box, a panel never has a caption. Also unlike a group box, a panel control can be associated with a scroll bar. For this reason, the Panel class is often used when child controls must be visibly set apart, either by providing a different BackColor property or by supplying a scroll bar to enable multiple controls to be placed in a constrained space.

By default, a panel control is drawn with no border. To create a visible boundary for the control, set its BorderStyle property to one of the values from the BorderStyle enumeration listed earlier in this chapter, in Table 12-5. The following code uses the BorderStyle property to create a three-dimensional border around a panel control:

panel.BorderStyle = BorderStyle.Fixed3D;

Automatic scrolling is disabled by default for panel controls. To enable scrolling for a panel, set its AutoScroll property to true, as shown here:

panel.AutoScroll = true;

Part III: Programming Windows Forms