Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change form size at runtime in C#

Tags:

c#

resize

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?

like image 316
bLAZ Avatar asked Oct 25 '12 08:10

bLAZ


People also ask

How do I change the form size?

Select the form, then find the Properties pane in Visual Studio. Scroll down to size and expand it. You can set the Width and Height manually.

How do I fix the size of a form in C#?

Right click on your form and go to properties. Then go to Layout options,see there are a property named Size. Change it as your need as width and length wise.

How do I stop winform from resizing?

Resizing of the form can be disabled by setting the FormBorderStyle property of the form to `FixedDialog`, `FixedSingle`, or `Fixed3D`.

How do I fit windows form to any resolution?

simply set Autoscroll = true for ur windows form..


2 Answers

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);
}
like image 93
Arrow Avatar answered Sep 21 '22 22:09

Arrow


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.

like image 36
ChrisF Avatar answered Sep 19 '22 22:09

ChrisF