ProblemYou want a specific control, such as a toolbar, to always "stick" to one edge of the form, no matter how it is resized. SolutionUse the control's Dock property to permanently affix the control to the selected side or other "dock region." DiscussionDock has six possible values:
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 frontSome 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 backFigure 4-11. Docking within a containerSee AlsoRecipe 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. |