Changing Layout Dynamically in Windows Forms
Suppose that you want to develop a form as shown below:
The expected behavior would be that the Label always remains attached with the bottom edge on the form. This is where the docking feature comes into picture. Windows Forms controls have a property called Dock that can dock that control to left, right, top, or bottom edge of the form. You can also make the control fill the available area of the form. To see this property in action, set the Dock property of the Label control to Bottom and run the form again. This time, as the form is resized, the Label is also resized and always remains attached to the bottom of the form.
Assume that you are developing a resizable dialog box that has two buttons, OK and Cancel (see below).
If you resize the form at runtime, you will find that the buttons assume a fixed location.
You would expect them to maintain the same distance from the right and bottom edges of the form. Thanks to the Anchor property, that makes this possible. The Anchor property anchors a control at some fixed place with respect to form borders. By default, controls are anchored at a fixed point from the top and left edges. To see the Anchor property in action, set the Anchor property of the OK and Cancel buttons to Bottom-Right and run the form. This time, you will observe that, even if you resize the form, the controls maintain a fixed distance from the bottom and right border of the form.
Page 2 of 3