19

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
bLAZ
  • 1,689
  • 4
  • 19
  • 31

7 Answers7

43

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);
}
Arrow
  • 2,784
  • 8
  • 38
  • 61
  • 2
    and if you want to recenter the form after the resize just `this.CenterToScreen();` – dialex Jun 11 '14 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 Nov 12 '14 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 Mar 04 '15 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"). – Peter Mortensen May 11 '17 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.

Community
  • 1
  • 1
ChrisF
  • 134,786
  • 31
  • 255
  • 325
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);
    }
}
MrFox
  • 4,852
  • 7
  • 45
  • 81
  • 1
    This is not changing form's size for me. – Mox Shah Apr 11 '18 at 09: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 Apr 11 '18 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 Apr 11 '18 at 19:13
  • Is the border style fixed? https://stackoverflow.com/questions/5416380/how-do-i-disable-form-resizing-for-users I've never worked with MetroForm, so I can't help you any further. – MrFox Apr 17 '18 at 09: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;
Penge
  • 31
  • 1
2

Try this.ClientSize instead of this.Size:

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

Worked for me.

CoderColor
  • 21
  • 1
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.

IndustProg
  • 627
  • 1
  • 13
  • 33
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);
}