20

How can I change window form size at runtime?

I saw examples, but every one requires Form.Size property. This property can be set like here: http://msdn.microsoft.com/en-us/library/25w4thew.aspx#Y456, but I've created my application form in a visual tool and the form is created like this:

static void Main()
{
    Application.Run(new Form());
}

How do I set that Size property now and then change it by the Form.Height and Form.Width methods?

1
  • 2
    Do you want to do it on some event, like button click?
    – Maheep
    Commented Oct 25, 2012 at 8:17

7 Answers 7

45

You cannot change the Width and Height properties of the Form as they are readonly. You can change the form's size like this:

button1_Click(object sender, EventArgs e)
{
    // This will change the Form's Width and Height, respectively.
    this.Size = new Size(420, 200);
}
4
  • 3
    and if you want to recenter the form after the resize just this.CenterToScreen();
    – dialex
    Commented Jun 11, 2014 at 10:37
  • Thank you very much, it's enlighting. Though I still don't understand, why didn't Microsoft® changing of Window properties as easy as it does with a controls…
    – Hi-Angel
    Commented Nov 12, 2014 at 18:23
  • I found that if I had the MinimumSize and MaximumSize properties set on the form in the designer, then setting the Size property simply wouldn't change anything...so make sure those two properties aren't set.
    – JTech
    Commented Mar 4, 2015 at 22:23
  • 2
    Can you qualify this answer? E.g. what about other dependencies that may or may not prevent this from having any effect, like properties MaximumSize, MinimumSize, AutoSizeMode (that e.g. can be set to "GrowOnly"), AutoSize, FormBorderStyle (that e.g. can be set to "FixedDialog") and WindowState (that e.g. can be set to "Maximized"). Commented May 11, 2017 at 11:05
12

If you want to manipulate the form programmatically the simplest solution is to keep a reference to it:

static Form myForm;

static void Main()
{
    myForm = new Form();
    Application.Run(myForm);
}

You can then use that to change the size (or what ever else you want to do) at run time. Though as Arrow points out you can't set the Width and Height directly but have to set the Size property.

4

In order to call this you will have to store a reference to your form and pass the reference to the run method. Then you can call this in an actionhandler.

public partial class Form1 : Form
{
    public void ChangeSize(int width, int height)
    {
        this.Size = new Size(width, height);
    }
}
4
  • 1
    This is not changing form's size for me.
    – Mox Shah
    Commented Apr 11, 2018 at 9:57
  • @MoxShah You need to be more specific, maybe ask your own question. Check with the debugger, does it reach the method call? What happens when you step inside? Maybe you need to also do a redraw on the form. Haven't used this stuff for a long time.
    – MrFox
    Commented Apr 11, 2018 at 12:47
  • @MeFox, I am using MetroForm control, on its resize event I'm changing its size, but its not working. When I say its not working means its executing that particular statement but its value is not getting changed.
    – Mox Shah
    Commented Apr 11, 2018 at 19:13
  • Is the border style fixed? stackoverflow.com/questions/5416380/… I've never worked with MetroForm, so I can't help you any further.
    – MrFox
    Commented Apr 17, 2018 at 9:38
3

You can change the height of a form by doing the following where you want to change the size (substitute '10' for your size):

this.Height = 10;

This can be done with the width as well:

this.Width = 10;
0
3

Try this.ClientSize instead of this.Size:

private void Form1_Load(object sender, EventArgs e)
{
   this.ClientSize = new Size(mywidth, myheight);
}

Worked for me.

0

Something like this works fine for me:

public partial class Form1 : Form
{
    Form mainFormHandler;
...
}

private void Form1_Load(object sender, EventArgs e){
    mainFormHandler = Application.OpenForms[0];
   //or instead use this one:
   //mainFormHandler = Application.OpenForms["Form1"];
}

Then you can change the size as below:

mainFormHandler.Width = 600;
mainFormHandler.Height= 400;

or

mainFormHandler.Size = new Size(600, 400);

Another useful point is that if you want to change the size of mainForm from another Form, you can simply use Property to set the size.

0

As a complement to the answers given above; do not forget about Form MinimumSize Property, in case you require to create smaller Forms.

Example Bellow:

private void SetDefaultWindowSize()
{
   int sizeW, sizeH;
   sizeW = 180;
   sizeH = 100;

   var size = new Size(sizeW, sizeH);

   Size = size;
   MinimumSize = size;
}

private void SetNewSize()
{
   Size = new Size(Width, 10);
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.