The Form

 

Characteristics of a Form

 

Introduction

The Form is the most fundamentals objects used in an application. By itself, a form does nothing. Its main role is to host other objects that the user uses to interact with the computer:

There are two main ways you get a form to your application. If you create a Windows Forms Application, it creates a starting form for you. Otherwise, the other technique consists of explicitly adding a form to your application.

 
  1. Start Microsoft Visual Studio .NET
  2. On the Start Page, click New Project (alternatively, on the main menu, you can click File -> New -> Project...)
  3. On the New Project dialog box, in the Project Types tree list, click Visual C++ Projects
  4. In the Templates list, click Windows Forms Application (.Net)
  5. In the Name edit box, replace the <Enter name> content with WinForms1
  6. In the Location combo box, accept the suggestion or type your own. Otherwise, type C:\Programs\MSVC .NET
     
    The New Project dialog box
  7. Click OK
  8. Press Ctrl + F5 to test the program
  9. Close it and return to MSVC

The Title Bar

A form is made of various sections that allow its control as a Windows object and other aspects that play a valuable part as a host of other objects. The top section of a form is made of a long portion called the title bar.

On the left part of the title bar, the form displays a small picture called an icon or the system icon. Microsoft Visual C++ provides a default icon for all forms. If you want to use a different icon, while the form is selected, on the Properties window, you can click the Icon field and then click its ellipsis button . This would launch the Open dialog box from where you can locate an icon and open it.

To change the icon programmatically, declare a pointer to the Icon class of the System::Drawing namespace and initialize it with the name of an icon file using the new operator. After initializing the icon, assign it to the form's Icon property. Here is an example:
 

SimpleForm::SimpleForm()
{
	// Declare a pointer to the Icon class and use its constructor
	// to specify the name of the icon
	// (The Icon class is defined in the Drawing namespace)
	Drawing::Icon *IC = new Drawing::Icon("Designer.ico");
	// Let this form know that you have an icon and assign it
	// to the Icon property of this form
	this->Icon = IC;
}

 

  1. On the main menu, click Project -> Add Resource...
  2. Click Icon and click New
  3. On the main menu, click Image -> Show Colors Window
  4. Using the colors from the Colors palette and the tools from the Image -> Tools menu, design the icon as follows:
     
    Icon Design 32x32
  5. To continue with the icon design, on the main menu, click Image -> New Image Type... On the New Icon Image Type, make sure the first 16x16, 16 colors item is selected:
     
    New Icon Image Type
  6. Click OK.
  7. Design it like the other. To make it transparent, make sure you right-click the color that looks like a monitor and apply it to the external areas:
     
    Icon design 16 x 16
  8. In the Workspace, click the Resource View tab and expand everything. Right-click IDI_ICON1 and click Properties. In the Properties window, click ID, type IDI_TARGET and press Enter
  9. Display the form and, on the Properties window, click Icon and click its ellipsis button
  10. On the Open dialog box, located the folder in which you had saved the project and select the icon1 icon
     
    The Open dialog box
  11. Click Open
  12. Press Ctrl + F5 to test the program
  13. Close it and return to MSVC

 

The Form's Caption

On the right side of the system icon, there is a word or a group of words called the caption. By default, the caption displays the name of the form. If you want to change the caption, while the form is selected, on the Properties window, click the Text field and type anything you want. After typing the text, press Enter to display it on the form. At design time, the caption is made of text that you type "as is". At run time, you can change the caption to display a more complex text that could be a changing time, the result of a calculation, etc.

  1. While the form is selected, on the Properties window, click Text and type
    Form Implementation
  2. Press Enter

The System Buttons

The system buttons are located on the right side of a title bar. They are used to minimize, maximize, restore, or close a window.

On the right side of the caption, there are three small buttons called the system buttons, made of the Minimize Minimize, Maximize Maximize, and Close buttons Close. The Minimize Minimize and the Maximize Maximize buttons are each controlled by a Boolean property. Therefore, to disable either one of the them, set its MinimizeBox or MaximizeBox property to false. If you set one of them to false while the other is true, the one set to false would appear disabled. If you set both to false, both would be hidden and only the Close Close button would appear.

To change a system button programmatically, call the desired button's property and assign it a true or false value. Here is an example that makes sure the user cannot maximize the form:
SimpleForm::SimpleForm()
{
	this->Text = S"A Simple Form";
	Size = Drawing::Size(280, 420);
	Drawing::Icon *IC = new Drawing::Icon("Designer.ico");
	this->Icon = IC;

	this->MinimizeBox = true;
	this->MaximizeBox = false;
}

In the same way, to hide both the Maximize and the Minimize buttons programmatically, you change their values as follows:
 
SimpleForm::SimpleForm()
{
	this->Text = S"A Simple Form";
	Size = Drawing::Size(220, 320);
	Drawing::Icon *IC = new Drawing::Icon("Designer.ico");
	this->Icon = IC;

	this->MinimizeBox = false;
	this->MaximizeBox = false;
}
 

  1. To make sure that the user cannot maximize the form, on the Properties window, click the MaximizeBox field to reveal its combo box and select False
  2. Test the program
     
    Form Implementation

The Form Borders

A form can be made to look like a regular rectangular control 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 behave like a dialog box that has only the system Close button and cannot be resized, set its FormBorderStyle property to FixedDialog. Here is an example:

SimpleForm::SimpleForm()
{
	this->Text = S"A Simple Form";
	Size = Drawing::Size(220, 320);
	Drawing::Icon *IC = new Drawing::Icon("Designer.ico");
	this->Icon = IC;

	this->MinimizeBox = false;
	this->MaximizeBox = false;
	this->FormBorderStyle = 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. Here is an example:

SimpleForm::SimpleForm()
{
	this->Text = S"A Simple Form";
	Size = Drawing::Size(200, 320);

	this->FormBorderStyle = FormBorderStyle::FixedToolWindow;
}

A sizable tool window is a tool window that allows the user to resize it. It can be created as follows:

SimpleForm::SimpleForm()
{
	this->Text = S"A Simple Form";
	Size = Drawing::Size(200, 320);

	this->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

The Form's Body

The main area of the form can be referred to as its body but it is usually called the client area. It is made of a color a priori specified by the operating system. To change the color to anything you like, you can use the BackColor field of the Properties window. To programmatically change its color, assign a color from the Color structure to the BackColor property. Here is an example:

SimpleForm::SimpleForm()
{
	this->Text = S"A Sample Form";
	this->Size = Drawing::Size(420, 280);
	this->BackColor = Color::AliceBlue;
}

If you prefer to cover the client area with a picture, use the BackgroundImage property instead. If using the Properties window, you can easily locate and select a picture. To programmatically specify or change the picture used as background, declare and initialize a pointer to the Bitmap class. Then assign it to the BackgroundImage property. Here is an example:

SimpleForm::SimpleForm()
{
	this->Text = S"A Sample Form";
	this->Size = Drawing::Size(420, 280);

	Bitmap *Bg = new Bitmap("C:\\Programs\\E350.bmp");
	this->BackgroundImage = Bg;
}
Form Background

The Window State of a Form

When a form appears as it was designed, it is said to be "normal". You can configure a form to be minimized or maximized when the application is launched. This ability is controlled by the WindowState property. The default value of this property is Normal which means the form would appear in a normal fashion. If you want the form to be minimized or maximized at startup, in the Properties window, select the desired value for the WindowState property.

To control the window’s state programmatically, assign the Maximized or Minimized value, which are members of the FormWindowState enumerator, to the WindowState property. Here is an example:

int __stdcall WinMain()
{
	Form1 *Fm = new Form1;
    
	Fm->WindowState = FormWindowState::Maximized;

	Application::Run(Fm);

	return 0;
}

If you want to check the state of a window before taking action, simply use a conditional statement to compare its WindowState property with the Normal, the Maximized, or the Minimized values.

 

The Form's Taskbar Presence

When an application displays on the screen along with other applications, its form can be positioned on top or behind forms of other applications. This is allowed by multitasking assignments. When a form is hidden, the taskbar allows you to access it because the form would be represented by a button. This aspect of forms is controlled by the ShowInTaskbar Boolean property. Its default value is True, which indicates that the form would be represented on the taskbar by a button.

If you create an application made of various forms, you may not want to show all of its forms on the taskbar. Usually the first or main form would be enough. To prevent a button for a form to display on the taskbar, set its ShowInTaskbar property to False.

Methods to Manage a Form

 

Form Creation

The form in implemented by the Form class from Forms namespace of the Forms namespace of the Windows namespace of the System namespace. The Form class is equipped with a constructor that allows you to dynamically create it.

Form Closure

When the user has finished using a form, he or she must be able to close it. Closing a form is made possible by a simple call to the Close() method. Its syntax is:

void Close();

Although this method can be used to close any form of an application, if it is called by the main form, it also closes the application.

Form Activation

When two or more forms are running on the computer, only one can receive input from the user; that is, only one form can actually be directly used at one particular time. Such a window has a title bar with the color identified in Control Panel as Active Window. The other window(s), if any, display(s) its/their title bar with a color called Inactive Window.

To manage this setting, the windows are organized in a 3-dimensional coordinate system and they are incrementally positioned on the Z coordinate, which defines the (0, 0, 0) origin on the screen (on the top-left corner of your monitor) with Z coordinate coming from the screen towards you.

In order to use a form other than the one that is active, it must be activated. To do this, the Activated() event must be fired.

Form Deactivation

If there is more than one form or application on the screen, only one can be in front of the others and be able to receive input from the others. If a form is not active and you want to bring it to the top, you must activate it, which sends the Activated() event. When a form is being activated, the one that was on top would become deactivated. The form that was on top, as it looses focus, would fire the Deactivated() event.

 

Previous Copyright © 2003-2015, FunctionX, Inc. Next