3

I finished developing my first windows forms application in c#, but when running it on a different Computer with a different screen size, the controls are not where they are supposed to be.

I used

this.WindowState = FormWindowState.Maximized;

to maximize the screen, but when doing this the form is just extended at the bottom and the controls stay at the same position.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • 1
    Yea the controls would never move unless they are docked somewhere, which they are not in your case probably. A workaround is to set `autoscalemode` to `None` , `StartPosition` to `CentreScreen` and `MinimizeBox ` to `false`. If you cant do the above for some reason you need to use dock and anchor controls from each other. – Rishav May 04 '18 at 05:22
  • Possible duplicate of [How to auto resize and adjust Form controls with change in resolution](https://stackoverflow.com/questions/4248637/how-to-auto-resize-and-adjust-form-controls-with-change-in-resolution) – default locale May 04 '18 at 05:32
  • take a look at this [question/answer](https://stackoverflow.com/questions/22735174/how-to-write-winforms-code-that-auto-scales-to-system-font-and-dpi-settings) it gives a good discussion auto scaling and winforms – Mark Hall May 04 '18 at 05:37
  • In WinForms using panels and the dock property is your friend. – GuidoG May 04 '18 at 07:39

2 Answers2

4

Use anchor and dock Property for the control you need.

Dock : The Dock property forces a control to stick to a certain edge of the parent form (or control) like glue.

For example :

If you have an Image Box and you set the dock to the bottom right, after you maximize your windows, the Image Box will stay on the bottom right.

You can also use "Fill" to your Dock Property to make the size also dynamic depends on the windows size.

1

HI For responsive design 1st create below class

 public class Resolution
    {
        float heightRatio = new float();
        float widthRatio = new float();
        int standardHeight, standardWidth;
        public void ResizeForm(Form objForm, int DesignerHeight, int DesignerWidth)
        {
            standardHeight = DesignerHeight;
            standardWidth = DesignerWidth;
            int presentHeight = Screen.PrimaryScreen.WorkingArea.Height;//.Bounds.Height;
            int presentWidth = Screen.PrimaryScreen.Bounds.Width;
            heightRatio = (float)((float)presentHeight / (float)standardHeight);
            widthRatio = (float)((float)presentWidth / (float)standardWidth);
            objForm.AutoScaleMode = AutoScaleMode.None;
            objForm.Scale(new SizeF(widthRatio, heightRatio));
            foreach (Control c in objForm.Controls)
            {
                if (c.HasChildren)
                {
                    ResizeControlStore(c);
                }
                else
                {
                    c.Font = new Font(c.Font.FontFamily, c.Font.Size * heightRatio, c.Font.Style, c.Font.Unit, ((byte)(0)));
                }
            }
            objForm.Font = new Font(objForm.Font.FontFamily, objForm.Font.Size * heightRatio, objForm.Font.Style, objForm.Font.Unit, ((byte)(0)));
        }

        private void ResizeControlStore(Control objCtl)
        {
            if (objCtl.HasChildren)
            {
                foreach (Control cChildren in objCtl.Controls)
                {
                    if (cChildren.HasChildren)
                    {
                        ResizeControlStore(cChildren);

                    }
                    else
                    {
                        cChildren.Font = new Font(cChildren.Font.FontFamily, cChildren.Font.Size * heightRatio, cChildren.Font.Style, cChildren.Font.Unit, ((byte)(0)));
                    }
                }
                objCtl.Font = new Font(objCtl.Font.FontFamily, objCtl.Font.Size * heightRatio, objCtl.Font.Style, objCtl.Font.Unit, ((byte)(0)));
            }
            else
            {
                objCtl.Font = new Font(objCtl.Font.FontFamily, objCtl.Font.Size * heightRatio, objCtl.Font.Style, objCtl.Font.Unit, ((byte)(0)));
            }
        }
    }

then when ever yo add any form the add panel control to form and dock it to form below
InitializeComponent();

Write below code

    this.WindowState = FormWindowState.Maximized;
    int screenWidth = Screen.PrimaryScreen.Bounds.Width;
    int screenHeight = Screen.PrimaryScreen.Bounds.Height;
    Resolution objFormResizer = new Resolution();
    objFormResizer.ResizeForm(this, screenHeight, screenWidth); 

this will make form responsive as much as possible as well as create system default font as well

Tanmay Nehete
  • 2,138
  • 4
  • 31
  • 42
  • Thank you, I combined the answers and now it works :) But when I use this code, the font cell style of datagridview in my forms is changed. –  May 04 '18 at 06:37