September 20, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Changing Layout Dynamically in Windows Forms

  • April 16, 2007
  • By Bipin Joshi
  • Send Email »
  • More Articles »

Docking

Suppose that you want to develop a form as shown below:

The form has a Label control at the bottom that displays some status message to the user. Try resizing the form. You will find that, as the form is resized, the Label doesn't change its position and size. This is certainly undesirable (see 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.

Anchoring

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



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel