FrozenHeart
FrozenHeart

Reputation: 20746

How to deal with controls and form's stretching in WinForms

Suppose that I have the following form in Designer:

enter image description here

I want to give users the ability to stretch this form as they want and all controls should be located like in the picture, no matter how user changed the size of this form, so they should take the same amount of space and stick to the same controls and borders.

How can I do it in WinForms? I know that there are such things like Docks etc, but I didn't find the correct way to use them in this situation.

Upvotes: 2

Views: 3005

Answers (2)

Reza Aghaei
Reza Aghaei

Reputation: 125197

Just To Add some notes on good answer of stakx

  1. For Controls Like ListBox that have a limit to their height, setting anchor is not enough and you should set IntegralHeight of them to false.

  2. I reccomend to set MinimumSize of Form to prevent a user from sizing a window to an undesirable size.In your case Set it to a minimum acceptable size to prevent ugly small form with an unusable ListBox.

Upvotes: 2

You want the Anchor property in this case, not Dock. Anchoring means that a control will always keep the same distance to certain sides (top, left, right, and/or bottom) even if it means that the size must be changed; docking OTOH does not care about margins, it just fills up all available space on one or all sides.

Here's what you might want to do:

  • Anchor the two image buttons to the top and right.

  • Anchor the OK button to the right and bottom (I guess).

  • Anchor the large ListBox to all sides.

Upvotes: 9

Related Questions