Manipulating Forms with the Windows Forms Library - 4.11 Attaching a Control to the Edge of a Form (Page 2 of 4 )
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 haveDocksettings other thanNone, 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: aMonthCalendarcontrol (notice how it automatically fills the width of the form by adding months) and aStatusStrip 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 theStatusStrip control, shown in this recipe’s figures, which is designed to dock along the bottom edge of the form. Other controls, such as theCheckBoxcontrol, 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 aPanelcontrol with an includedComboBox control that is docked along the top edge of the panel.
Attaching a Control to the Edge of a Form

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 andAnchorproperties cannot be used at the same time on the same control. The last one you set on that control is the one used.
Next: 4.12 Moving or Resizing Controls as a Form Resizes >>
More Visual Basic.NET Articles
More By O'Reilly Media
|
This article is excerpted from chapter four of the Visual Basic 2005 Cookbook, written by Tim Patrick and John Clark Craig (O'Reilly; ISBN: 0596101775). Check it out today at your favorite bookstore. Buy this book now.
|
|