Database Reference
In-Depth Information
Anchor Property
When a user resizes a form, the controls maintain a constant distance from the edges of their parent
form with the help of the Anchor property. The default value for the Anchor property for any control is
set to Top, Left, which means that this control will maintain a constant distance from the top and left
edges of the form. The Anchor property can be set by using the provided graphical interface in the
Properties window, as shown in Figure 9-12.
Because of the default setting of Anchor property to Top, Left, if you try to resize a form by
stretching it toward the right side, you will see that its controls are still positioned on the left rather than
shifting to the center of the form to adjust to the size of the form after resizing is done.
If opposite edges, for example, Left and Right, are both set in the Anchor property, the control will
stretch when the form is resized. However, if neither of the opposite edges is set in the Anchor property,
the control will float when the parent is resized.
Figure 9-12. Setting the Anchor property
Try It: Working with the Dock and Anchor Properties
In this exercise, you will use the existing Windows Forms application named WinApp, which you created
previously in the chapter. You will see how to modify this application in such a way that when you resize
the form, its controls behave accordingly and keep the application presentable for the user.
Go to Solution Explorer and open the WinApp project. Open the WinApp form in the Design view.
1. Select the form by clicking its title bar; you will see handles around form's
border, which allow you to resize the form's height and width.
2. Place the cursor on the handle of the right border, and when the mouse
pointer becomes double-headed, click and stretch the form toward the right
side. You will see that the form's width increases, but the controls are still
attached to the left corner of the form.
3. Similarly, grab the handle located on the bottom of the form and try to
increase the height of the form. You will notice that the controls are still
attached to the top side of the form.
 
Search WWH ::




Custom Search