The Form

 

Overview of Forms

 

Introduction

A Form is a rectangular object used to host other controls that allow a person to interact with the computer.

There are two main ways you get a form to your application. If you create a Windows ( Forms) Application, the wizard generates and displays a starting form for you. Otherwise, you can explicitly adding a form to your application.

 

Characteristics of a Form

The Title Bar

Like any regular window, the top section of a form is made of a long section 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 Studio provides a default icon for all forms. If you want to use a different icon, you can supply it to the form. You can use an icon stored in a medium such as a hard disc, a floppy disc, a CD or CD, etc. When you install Microsoft Visual Studio, it also install many icons by default in C:\Program Files\Microsoft Visual Studio .Net 2003\Common7\Graphics\Icons. The icons are divided in various categories for convenience.

If you don't have or can't find an icon that fits your needs, you can design a new one. To do that, on the main menu, you can click Project -> Add Resource... From the Add Resource dialog box, you can click Icon and click New. Here is an example of a designed icon


 
Icon Design 32x32

An icon is usually used as a combination of a 32x32 pixel and a 16x16 pixel. To design the other icon, you don't have to create a new file. You can display the New Icon Image Type dialog box by clicking Image -> New Image Type... from the main menu. On the New Icon Image Type, make sure the first 16x16, 16 colors item is selected:
 
New Icon Image Type

Here is an example of the 16x16 pixel version of the above icon:
 
Icon design 16 x 16

After design the icon, you must save it. This is automatically done for you.

Once the icon is ready, you can set it as the icon of the form. To use an icon either from a medium or one that you designed, 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:

The Open dialog box

 

To change the icon programmatically, you can 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;
}

 

 

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, in the Properties window, click the Text field and type anything you want. After typing the text, you can 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.

The System Buttons

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. Notice that, in the Properties window, the values are set as False or True. In C++, the Boolean properties are set as true or false (all lowercase). Of course, you can type-define them to anything else you like.

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;
}

Form Implementation

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;
}
 

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 behave like a dialog box that has only the system Close button and cannot be resized, set its FormBorderStyle property to FixedDialog. You can also do this programmatically 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;
	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. To get such a tool window, set the FormBorderStyle of the form to FixedToolWindow. You can also set it programmatically as follows:

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. To create such a tool window, you can set the FormBorderStyle of the form to SizableToolWindow. You can also do this programmatically 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 professionally called the client area. The client area 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 regular style. If you want the form to be minimized or maximized at startup, in the Properties window, select the desired value for the WindowState property. The other values are Minimized and Maximized.

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 of, or behind, other forms. 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 primary 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

A form is implemented by the Form class from 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 and subsequently all forms that are part of 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 activate a form, 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.

 

Home Copyright © 2004-2010 FunctionX, Inc.