Search code examples
c#winformsvisual-studioscaledesigner

Scale WindowsForm


Basically, I want everything (Controls etc) to scale with the WindowsForm when resizing it by dragging, so that the user can determine the size of the UI himself. The picture is not perfect, but I hope it explains it:

Resize approximation

The easiest way to do this would be to use a (Flow/Table)LayoutPanel and the appropriate Anchor/Drop properties, but I feel like that restricts my design, which currently looks like this:

My Design

My idea was to scale all the components when Resize() is called:

foreach(Control c in Controls){
            c.Scale(scaleFactor);
        }

My problems are: Locations aren't set properly and the rounding needed to determine the 'scaleFactor' leads to inconsistencies.

Is there a clean way to do all this? Do I have to use LayoutPanels to get a clean way?


Solution

  • Are you sure that you want to zoom those text boxes and labels, too? Text boxes have a fairly standard height, for example. What do you expect if the window is so small that the text is too large for the buttons/text boxes, etc.?

    The typical resizing logic of the contents of a resizable window is a little bit different. I would rather create a borderless panel for the groupboxes and stack/dock everything like this:

    before resize

    If I resize this window, that will look like this:

    after resize

    Btw, I do not like if buttons are resized like this. I would use only Anchor = Left, Right for them so they would preserve their height. And do not forget to set a proper MinimumSize for the form.