Resizing Windows Forms
There are a number of ways to specify the size of your Windows Form, including manually within the Windows Forms Designer, using the Properties window, and in code.
To resize a form in the Windows Forms Designer
- In the Windows Forms Designer, click the form to select it.
- Click and drag one of the eight sizing handles that appear on the border of the form. The sizing handles look like small white boxes, and the mouse pointer turns into a double-headed arrow when you point at a handle.
Note Pressing the ARROW keys while holding down the SHIFT key allows you to resize the form more precisely.
To resize a form using the Properties window
- In the Properties window, click the Size property and enter values for the width and height, separated by a comma.
Note Expand the Size property to enter the Width and Height values individually.
Additionally, forms can be resized at run time. For example, if a form displays a bitmap, you may want to resize the form whenever the user selects a new bitmap.
To resize a form programmatically
- Define the size of a form at run time by setting the Size property of the form. The following example shows the form size set to 100 by 100 pixels:
' Visual Basic Form1.Size = New System.Drawing.Size(100, 100) // C# Form1.Size = new System.Drawing.Size(100, 100); // C++ Form1->Size = System::Drawing::Size(100, 100);
To change form width and height programmatically
- Once the Size object is defined, change either its Width or Height property. In the example below, the width of the form is set to 300 pixels from the left edge of the form, while the height stays constant.
' Visual Basic Form1.Width = 300 // C# Form1.Width = 300; // C++ Form1->Width = 300;
- Change Width or Height by setting the Size property. As the following code shows, however, this approach is more cumbersome than simply setting Width or Height properties.
' Visual Basic Form1.Size = New Size(300, Form1.Size.Height) // C# Form1.Size = new Size(300, Form1.Size.Height); // C++ 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. In the example below, the width of the form is set to 200 pixels wider than the current setting.
' Visual Basic Form1.Width += 200 // C# Form1.Width += 200; // C++ Form1->Width += 200;Caution Always use the Height or Width property to change the size of a form, unless you're setting both at the same time. The following code will not change the form size. The Size property returns a Size structure containing a copy of the form's height and width, and the X member of this structure is incremented by 100 — however, the copied and incremented structure is then discarded.
' Visual Basic ' NOTE: CODE WILL NOT COMPILE Dim f As New Form() f.Size.X += 100 // C# // NOTE: CODE WILL NOT COMPILE Form f = new Form(); f.Size.X += 100; // C++ // NOTE: CODE WILL NOT COMPILE Form * f = new Form(); f->Size->X += 100;