views:

51

answers:

2

I am working on a largish c# project with a lot of winforms that, even though you can resize the form, the elements in the form don't scale.

How can I make the form elements (such as the datagridview, text area's, etc) scale when the user changes the size of the form.

Nearly all the forms subclass from one specific form so if there's something I can do in the base class, that'd be great.

+6  A: 

You should set the Anchor and Dock properties on the controls in the forms.

The Anchor property controls which edges of a control are "bound" or "tied" to the corresponding edges of its form.
For example, if you set Anchor to Bottom, the distance between the control's bottom edge and the bottom of its parent will not change, so the control will move down as you resize the form.
If you set Anchor to Top | Bottom, the control will resize vertically as you resize the form.

To make a control resize with the form, set the Anchor to all four sides, or set Dock to Fill.

SLaks
It's fairly intuitive once you start using it. For example, if you have OK/Cancel buttons in the lower right of your dialog, set the `Anchor` property to Bottom and Right to have them drag properly on the form.
Matt Davis
So I have to do it on a per form basis?
Malfist
@Malfist: Yes; per-form and per-control. .Net will not magically figure out which controls are supposed to stretch and which controls to align to which sides.
SLaks
+1  A: 

Use the Anchor and Dock properties.

Anchor allows you to pin specific sides of the control to the sides of the parent control.

Dock will bind the whole control to a side of the parent control or it can be set to fill the contents of the parent control.

You usually just need to set the Anchor to the bottom and right of the parent control but gets more difficult when you have controls side by side, then you need to manually resize the controls on the forms OnResize event to get them to scale naturally together.

w69rdy