Home

The Form's Measures

 

The Form's Size

A form's size is the amount of space it is occupying on the screen. It is expressed as its width and its height. The width of a form is the distance from its left to its right borders. The height is the distance from the top to the bottom borders of a form:

When you create a form, it assumes a default size. To set or change the size of a form, at design time, first click it to select it. Then, position the mouse on its right, bottom, or bottom-right handles. This changes the mouse cursor in one of three shapes:

Resizing a form

 

With the mouse positioned, drag in the desired direction.

If you don't want to be able to resize a form and you want to prevent this by accident, at design time, set the form's Locked property to True. If you do this:

  • A small picture of a lock displays in the top-left corner of the form:
  • The form gets surrounded by a rectangle with a black border
  • The sizing handles of the form disappear
The Locked property of the form set to True
Because Locked is only a design characteristic, it is not an actual property of the Form class. Therefore, you cannot use it to lock a form at run time. In order words, you cannot use the Lock feature to prevent the user from resizing a form.

Besides resizing the form by dragging one of its handles, to change the size of a form, select it and, in the Properties window, click the + button of the Size field. Then type the desired value for the Width and the desired value for the Height. The values must be natural numbers.

To programmatically change the size of a form, assign the desired values to either or both its Width and Height properties. Here is an example:

public ref class CExercise : public Form
{
public:
    CExercise()
    {
	InitializeComponent();
    }

    void InitializeComponent()
    {
	Text = L"Windows Fundmentals - Programming";
        Icon = gcnew System::Drawing::Icon(L"C:\\Programs\\RedBook.ico");
	
	ControlBox = true;
	MinimizeBox = true;
	MaximizeBox = false;

	Width  = 425;
	Height = 308;
    }
};

Alternatively, you can assign a Size value to the Size property of the form. Here is an example:

public ref class CExercise : public Form
{
public:
    CExercise()
    {
	InitializeComponent();
    }

    void InitializeComponent()
    {
	Text = L"Windows Fundmentals - Programming";
        Icon = gcnew System::Drawing::Icon(L"C:\\Programs\\RedBook.ico");
	StartPosition = FormStartPosition::WindowsDefaultLocation;

        ControlBox = true;
        MinimizeBox = true;
        MaximizeBox = false;

	Size = System::Drawing::Size(425, 308);
    }
};

If you want the operating system to specify the size of the form, set its StartPosition property to WindowsDefaultBounds. In this case, a value called CW_USEDEFAULT would be assigned to both the Width and the Height properties.

Practical LearningPractical Learning: Setting the Form's Default Size

  1. Position the mouse to the small square on the right border of the form
  2. Click and drag in the right direction to make sure the caption in the title bar can appear completely
     
    Resizing a form
  3. Release the mouse

The Form Borders

A form can be made to look like a regular rectangular host made of a system icon and the system buttons. Depending on your goals, you can also make a form appear as a dialog box or a dockable window. The borders of a form are controlled by the FormBorderStyle property.

If you set both the MinimizeBox and the MaximizeBox properties to False, we saw that the form would have only the system Close button, but the form can still be resized. If you want the form to display only the system Close button and to prevent the user from resizing it, set its FormBorderStyle property to FixedDialog. Here is an example:

public ref class CExercise : public Form
{
public:
    CExercise()
    {
	InitializeComponent();
    }

    void InitializeComponent()
    {
	Text = L"Windows Fundmentals - Programming";
        Icon = gcnew System::Drawing::Icon(L"C:\\Programs\\RedBook.ico");
	StartPosition = FormStartPosition::WindowsDefaultLocation;

        ControlBox = true;
        MinimizeBox = true;
        MaximizeBox = false;

	Size = System::Drawing::Size(425, 308);

	FormBorderStyle = System::Windows::Forms::FormBorderStyle::FixedDialog;
    }
};

A tool window is a form equipped with a short title bar, no icon, and only a small system Close button. There are two types of tool windows.

  • A tool window is referred to as fixed if the user cannot resize it. In this case, the FormBorderStyle property of the form is set to FixedToolWindow:
     
    FormBorderStyle = FormBorderStyle::FixedToolWindow;
  • A tool window is referred to as sizable if it allows the user to resize it. To get such a form, set its FormBorderStyle property to SizableToolWindow:
     
    FormBorderStyle = FormBorderStyle::SizableToolWindow;

You can also create a form with no borders by assigning None to the FormBorderStyle property. If you do this, make sure you provide the user with a way to close the form; otherwise...

A Tool Window
 
 

Previous Copyright © 2007 FunctionX, Inc. Next