8.5. Docking and Anchoring Controls

It's not enough to design layouts that are nicely aligned according to the design-time dimensions. At runtime a user will likely resize the form, and ideally the controls on our form will resize automatically to fill the modified space. The control properties that have the most impact on this are Dock and Anchor. Figure 8-15 (left) and 8-15 (right) show how the controls on a Windows Form will properly resize once you have set the correct Dock and Anchor property values.

Figure 8.15. Figure 8-15

The Dock property controls which borders of the control are bound to the container. For example, in Figure 8-15 (left), the TreeView control Dock property has been set to fill the left panel of a SplitContainer, effectively docking it to all four borders. Therefore, no matter how large or small the left-hand side of the SplitContainer is made, the TreeView control will always resize itself to fill the available space.

The Anchor property defines the edges of the container to which the control is bound. In Figure 8-15 (left), the two button controls have been anchored to the bottom right of the form. When the form is resized, as shown in 8-15 (right), the button controls maintain the same distance to the bottom right of the form. Similarly, the TextBox control has been anchored to the left and right borders, which means that it will automatically grow or shrink as the form is resized.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.77.158