Recipe 4.12. Moving or Resizing Controls as a Form Resizes


Problem

You want a control to move or stretch in proportion to how the form is resized.

Solution

Use the control's Anchor property to attach it to one or more sides of the form.

Discussion

Setting the Anchor property of a control tells that control to permanently maintain a consistent distance relationship with one or more sides of the form or container. You can anchor a control to any or all of the four sides of a form. By default, controls are anchored to the left and top sides of the form. This means that as the form resizes, the controls remain the same distance from the form's left and top edges (i.e., they do not appear to move).

The available Anchor property choices include Left, Top, Right, and Bottom, and you can use them in any combination. The following list shows the types of combinations you can use with the Anchor property:


Anchored to one side

As the form is resized, the center point of the control along the anchored edge is matched to a position on that form edge relative to the changing size of the form. The size of the control does not change. For instance, if a control is anchored to the top of a form and the form is made wider, the control moves to the right in proportion to the size of the form, as shown in Figure 4-12.

Figure 4-12. The top-anchored control moves when the form is resized


Anchored to two adjacent sides

As the form is resized, the control maintains its distance from both anchor sides. In other words, it seems to be joined to the corner that is shared by the two anchor sides. By default, most controls anchor to the left and top sides of the form and do not appear to move when the right and bottom borders of the form are moved in a resize operation.


Anchored to two opposite sides

The anchor sides of the control remain a fixed distance from the anchor borders. For instance, if a control is anchored on the left and right, the control grows by the same number of pixels as the form is widened (see Figure 4-13). When the unanchored direction is resized, the control is moved to keep the portion of space between the unanchored sides and the control the same, but the control is not resized in that direction.

Figure 4-13. The left-and-right-anchored control stretches as the form widens


Anchored to three sides

The control resizes between the two anchor sides that are opposite to each other and remains the same distance from the single anchor border, as shown in Figure 4-14.

Figure 4-14. Anchored to top, left, and right, the edges of the control remain a fixed distance from all but the bottom edge of the form


Anchored to all four sides

The control is continually resized with the form. All its sides stay the same distance from all anchored form borders, as shown in Figure 4-15.

Figure 4-15. Anchored to top, left, bottom, and right, the proportions of the control change in concert with the form's proportions

Anchoring also applies to panels and other containers that can include subordinate controls.

See Also

See Recipe 4.11 for details on the Dock property, which you can use to attach a control to one side of a form's client area. 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.




Visual Basic 2005 Cookbook(c) Solutions for VB 2005 Programmers
Visual Basic 2005 Cookbook: Solutions for VB 2005 Programmers (Cookbooks (OReilly))
ISBN: 0596101775
EAN: 2147483647
Year: 2006
Pages: 400

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net