Udostępnij za pośrednictwem


How to: Resize Windows Forms

You can specify the size of your Windows Form in several ways. You can change both the height and the width of the form programmatically by setting a new value for the Size property, or adjust the Height or Width properties individually. If you are using Visual Studio, you can change the size using the Windows Forms Designer. How to: Resize Windows Forms Using the Designer
How to: Resize Windows Forms Using the Designer
How to: Resize Windows Forms Using the Designer
How to: Resize Windows Forms Using the Designer

To resize a form programmatically

  • Define the size of a form at run time by setting the Size property of the form.

    The following code example shows the form size set to 100 × 100 pixels.

    Form1.Size = New System.Drawing.Size(100, 100)
    
    Form1.Size = new System.Drawing.Size(100, 100);
    
    Form1.set_Size(new System.Drawing.Size(100, 100));
    
    Form1->Size = System::Drawing::Size(100, 100);
    

To change form width and height programmatically

  • After the Size is defined, change either the form height or width by using the Width or Height properties.

    The following code example shows the width of the form set to 300 pixels from the left edge of the form, whereas the height stays constant.

    Form1.Width = 300
    
    Form1.Width = 300;
    
    Form1.set_Width(300);
    
    Form1->Width = 300;
    

    -or-

    Change Width or Height by setting the Size property.

    However, as the following code example shows, this approach is more cumbersome than just setting Width or Height properties.

    Form1.Size = New Size(300, Form1.Size.Height)
    
    Form1.Size = new Size(300, Form1.Size.Height);
    
    Form1.set_Size(new Size(300, Form1.get_Size().get_Height()));
    
    Form1->Size = System::Drawing::Size(300, Form1->Size.Height);
    

To change form size by increments programmatically

  • To increment the size of the form, set the Width and Height properties.

    The following code example shows the width of the form set to 200 pixels wider than the current setting.

    Form1.Width += 200
    
    Form1.Width += 200;
    
    Form1.set_Width(Form1.get_Width() + 200);
    
    Form1->Width += 200;
    

    Warning

    Always use the Height or Width property to change a dimension of a form, unless you are setting both height and width dimensions at the same time by setting the Size property to a new Size structure. The Size property returns a Size structure, which is a value type. You cannot assign a new value to the property of a value type. Therefore, the following code example will not compile.

    ' NOTE: CODE WILL NOT COMPILE
    Dim f As New Form()
    f.Size.Width += 100 
    
    // NOTE: CODE WILL NOT COMPILE
    Form f = new Form();
    f.Size.Width += 100;
    
    // NOTE: CODE WILL NOT COMPILE
    Form f = new Form();
    f.get_Size().set_X(f.get_Size().get_X() + 100);
    
    // NOTE: CODE WILL NOT COMPILE
    Form^ f = gcnew Form();
    f->Size->X += 100;
    

See Also

Other Resources

Getting Started with Windows Forms

Enhancing Windows Forms Applications