C# – How to automatically move and resize buttons and labels when a form is expanded

cvisual-studio-2015winforms

So I am using a form application that has buttons and labels in it (Picture) and I want to know how to make them re-size properly when I maximize the form. The anchor or/and dock property as in these questions: C# form auto resize window or Automatic resizing of the Windows Forms controls does not help because it expands only 1 item.

I need it to move and resize without going out of the original proportions. So like let's say I have 2 buttons above each other. I want them to stay always on the right side and they are each 10*12 pixels and 2 pixels from edge. When I expand the original window size three times, each button will still be 2 pixels from the edge but 30*36 pixels big.

Best Answer

You need the anchor property set on each control on the form. Note that if you anchor two opposite sides, the control will be stretched/squashed, but if you only anchor to one side, the control will instead move.

Note that it's not normal behaviour for a Winforms app to have all controls on a form resize proportionally - you choose the controls that need to stretch (panels, treeviews, images, listboxes etc), and then the standard controls (inputs, buttons, dropdowns etc) should keep the same size but move around (i.e. anchor to top and/or right).

If you're trying to do some sort of "kiosk mode" for your app, then you'd typically run at a lower resolution and/or higher DPI to get the proportional resizing effect, which wouldn't require changes to your app as long as you've applied the idiomatic style of anchoring in your forms.

Related Topic