Friday 18 January 2008

Winforms - Anchor and Dock properties

I met several difficulties while trying to set the anchor and dock properties of Winform container controls such as panels and group boxes. That's why I give here, a brief explanation of these properties.

Anchor
This property controls the distance between a Winform control (button, combobox, panel) and its container control (panel, group box, etc).

At runtime, when you resize your form, the distance between your control and the edges of its container control (to which it is anchored) remains proportional to the distance "drawn" between them in design mode.

By default, this option is set to "Top, Left". Other possible options are "Right" and "Bottom".

Dock
This option allows you to attach a control such as a panel to one of the edges of your form. Possible options for this property are:

Top:
The control will always be displayed and attached to the top edge of your window.

Left:
The control will always be displayed and attached to the left edge of your window.

Right:
The control will always be displayed and attached to the right edge of your window.

Bottom:
The control will always be displayed and attached to the bottom edge of your window.

Fill:
The control will stretch to fill its container or form.

Note that in the case where more than one control have the same dock property in the same container/form, the first drawn control will have priority. For example, if you draw 2 panels in your form and set their dock properties to bottom. The first drawn panel will be displayed as the bottommost panel while second will be displayed just on top of the first one.

2 comments:

Anonymous said...

Thanks! Exactly what i was looking for.

Anonymous said...

Dock WinForms Control