Visual Basic 2005 Cookbook: Solutions for VB 2005 Programmers (Cookbooks (OReilly))

Problem

You want a specific control, such as a toolbar, to always "stick" to one edge of the form, no matter how it is resized.

Solution

Use the control's Dock property to permanently affix the control to the selected side or other "dock region."

Discussion

Dock has six possible values:

None

The control performs no docking.

Top

The control attaches itself to the top of the form's client area and fills the entire width of the client area, if the control supports such resizing.

Bottom

The control attaches itself to the bottom of the form's client area and fills the entire width of the client area, if the control supports such resizing.

Left

The control attaches itself to the left edge of the form's client area and fills the entire height of the client area, if the control supports such resizing.

Right

The control attaches itself to the right edge of the form's client area and fills the entire height of the client area, if the control supports such resizing.

Fill

The control fills the entire client area of the form, if the control supports such resizing.

If multiple controls have Dock settings other than None, they are attached to the form edges according to their z-order settings, starting from the back-most control. To alter the z-order of a control, right-click on the control in the Form Designer and select either "Bring to Front" or "Send to Back" from the shortcut menu. Figures 4-9 and 4-10 show a form with two controls with different z-orders docked to its bottom edge: a MonthCalendar control (notice how it automatically fills the width of the form by adding months) and a StatusStrip control.

Figure 4-9. The form when the calendar's z-order is in front

Some controls are designed to dock along a specific edge of the form's client area. The most obvious example is the StatusStrip control, shown in this recipe's figures, which is designed to dock along the bottom edge of the form. Other controls, such as the CheckBox control, really aren't designed for docking. While you can still dock them, they may not look very nice.

Docking also applies to panels and other containers that can include subordinate controls. Figure 4-11 displays a Panel control with an included ComboBox control that is docked along the top edge of the panel.

Figure 4-10. The form when the calendar's z-order is in back

Figure 4-11. Docking within a container

See Also

Recipe 4.12 discusses the Anchor property, which can be used to attach a control to one, two, three, or four sides of the form. The Dock and Anchor properties cannot be used at the same time on the same control. The last one you set on that control is the one used.

Категории