Home

The Forms of an Application

 

Fundamentals of a Form

 

Introduction

The form is the most fundamental object used in an application. By itself, a form does nothing. Its main role is to host other objects used to interact with the computer:

 

Form 1

Creating a Form

A form is based on the Form class that is defined in the System.Windows.Forms namespace created in the System.Windows.Forms.dll assembly. Therefore, when you start an application and you want to have a form in it, include the System.Windows.Forms.dll library to your application. To refer to a form, you can include the System.Windows.Forms namespace in your application.

To get a form, declare a variable of type Form. Here is an example:

open System
open System.Windows.Forms

let exercise : Form = new Form()

Application.Run exercise

The Form's Title Bar

 

The System Icon

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 side 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, declare a variable of type Icon of the System.Drawing namespace and initialize it with the name of an icon file. After initializing the icon, assign it to the form's Icon property. Here is an example:

open System
open System.Windows.Forms

let exercise : Form = new Form()

let customIcon = new System.Drawing.Icon("C:\common\book.ico")
exercise.Icon <- customIcon

Application.Run exercise

Whether you had assigned an icon to the form or not, you can control whether the form should display an icon. To support this, the Form class is equipped with a Boolean property named ShowIcon. If you set this property to false, the icon would not appear in the title bar of the form. If the Form.ShowIcon property is set to true, which is its default value, the form's title bar would display an icon.

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, assign the desired string to Text property. Here is an example:

popen System;
open System.Windows.Forms;

let exercise : Form = new Form()

exercise.Icon <- new System.Drawing.Icon("C:\common\book.ico");

exercise.Text <- "Windows Fundmentals - Programming";

Application.Run exercise

This would produce:

The Caption of a Form

The System Buttons

On the right side of the caption, there are three small buttons called the system buttons, made of the Minimize (Minimize,, or ), the Maximize (Maximize,, or ), the Restore, and the Close (Close, , or ) buttons. The presence or absence of these buttons is controlled by the Boolean ControlBox property whose default value is true to indicate that the form will be equipped with the system buttons. If you set it to false, no system button would be displayed:

A form without the control box

In this case, the user would not be able to close the form using the system buttons. Therefore, if you create this type of form, make sure you provide the user with a way to close it.

The Minimize (Minimize,, or ) button is controlled by a Boolean property called MinimizeBox. By default, this property is set to true and the form would display a Minimize button. The Maximize (Maximize,, or ) button is controlled by the Boolean MaximizeBox property, which also is set to true by default. Depending on what you are trying to achieve in your application, you can change the value of either one or both of these properties. The four combinations are as follows:

MaximizeBox MinimizeBox Display Result
True True Maximize = True and Minimize = True The form can be minimized or maximized
True False Maximize = True and Minimize = False The form can be maximized but cannot be minimized
False True Maximize = False and Minimize = True The form can be minimized but cannot be maximized 
False False Maximize = False and Minimize = False The form can be neither minimized nor maximized

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:

open System;
open System.Windows.Forms;

let exercise : Form = new Form()

Exercise.Icon <- new System.Drawing.Icon("C:\common\book.ico");
exercise.Text <- "Windows Fundmentals - Programming";

Exercise.ControlBox <- true
Exercise.MinimizeBox <- true
Exercise.MaximizeBox <- false

Application.Run exercise

This would produce:

Control Box

The Form's Position

 

The Form's Location

Like every control on an application, a form has a parent: the computer desktop. Based on this, when an application is launched, its form occupies an area of the desktop. To locate its children, the desktop uses a Cartesian coordinate system whose origin is located on the top-left corner of the screen:

The Origin of the Windows default coordinate system

The horizontal measurements move from the origin to the right. The vertical measurements move from the origin to the bottom:

Axes of the Windows Coordinate System

The distance from the the desktop's left border to the form's left border is represented by the form's Left property:

member Left : int with get, set

The distance from the desktop's top border to the form's top border is specified by the Top property:

member Top : int with get, set

Therefore, the Left and the Top values are known as the form's location. This can be illustrated as follows:

The Form Location

To specify the default location of a form when the application is opened, you can assign a value to its Left and/or its Top properties. Here is an example:

open System;
open System.Windows.Forms;

let exercise : Form = new Form()

exercise.Text <- "Windows Fundmentals - Programming";

exercise.Left <- 228
exercise.Top  <- 146

Application.Run exercise

Alternatively, you can assign a Point variable to the form's Location property. Here is an example:

open System;
open System.Drawing;
open System.Windows.Forms;

let exercise : Form = new Form()

exercise.Text <- "Windows Fundmentals - Programming";

exercise.Location <- new Point(228, 146);

Application.Run exercise

The Startup Position of a Form

The Left and Top properties or the Location property allow you to specify a position for the form. Microsoft Windows provides an alternative position to specify the location of the form relative to its parent. This is the role of the StartPosition property of the Form class, which is a value of the FormStartPosition enumerator. It provides the following five values:

Value Result
CenterParent The form will be positioned in the center of its parent. If the application is made of only one form, this form would be positioned in the middle of the desktop
CenterScreen The form will be positioned in the middle of the desktop even if this form is part of an application that is made of various forms.
Manual The form will use the values of the X, Y, Left, and/or Top properties of the Location
WindowsDefaultLocation The operating system will specify the form's position using a value known in Win32 as CW_USEDEFAULT

Based on this, to set the default relative location of a form when it comes up, call the FormStartPosition enumeration to select the desired value and assign it to the StartPosition property of the form. Here is an example:

open System;
open System.Drawing;
open System.Windows.Forms;

let exercise : Form = new Form()

exercise.Text <- "Windows Fundmentals - Programming";

exercise.StartPosition <- FormStartPosition.WindowsDefaultLocation;

Application.Run exercise

The StartPosition property provides another value that is related to the size. Therefore, we will mention it when we review the size of a form.

The Window State of a Form

When creating your application, you can configure its (main) form to be minimized or maximized when the application is launched. This feature is controlled by the WindowState property. The default value of this property is Normal which means the form would appear in the same dimensions it was designed. If you want the form to be minimized or maximized at startup, assign the Maximized or Minimized value, which are members of the FormWindowState enumerator, to the WindowState property. Here is an example:

open System;
open System.Windows.Forms;

let exercise : Form = new Form()

exercise.Text <- "Windows Fundmentals - Programming";

exercise.WindowState <- FormWindowState.Maximized;

Application.Run exercise

If you want to check the state of a window before taking action, 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, 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.

Here is an example:

open System;
open System.Windows.Forms;

let exercise : Form = new Form()

exercise.Text <- "Windows Fundmentals - Programming";

exercise.ShowInTaskbar <- false;

Application.Run exercise
Unless you have a good reason, and it is hard to see what that reason could be, you should not set the ShowInTaksbar property of the first or main form of an application to false.

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:

The Size of a Form

When you create a form, it assumes a default size. To change the size of a form, assign the desired values to either or both its Width and Height properties. Here is an example:

open System;
open System.Windows.Forms;

let exercise : Form = new Form()

exercise.Text <- "Windows Fundmentals - Programming";

exercise.Width <- 425
exercise.Height <- 308

Application.Run exercise

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

open System;
open System.Drawing;
open System.Windows.Forms;

let exercise : Form = new Form()

exercise.Text <- "Windows Fundmentals - Programming";

exercise.Size <- new Size(425, 308);

Application.Run exercise

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.

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:

open System
open System.Windows.Forms

let exercise : Form = new Form()

exercise.Icon <- new System.Drawing.Icon("C:\common\Something.ico")
exercise.Text <- "Windows Fundmentals - Programming"

exercise.FormBorderStyle <- FormBorderStyle.Fixed3D

Application.Run exercise

This would produce:

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
 

The Client Area of a Form

 

Introduction

When a form has been created, you can add Windows controls to it. These controls can be positioned only in a specific area, the body of the form. The body spans from the left border of the form, excluding the border itself, to the right border of the form, excluding the border. It also spans from the top side, just under the title bar, to the bottom border, excluding the bottom border, of the form. The area that the form makes available to the controls added to it is called the client area:

If a control is positioned on a form, its location uses a coordinate system whose origin is positioned on the top-left section of the client area (just under the title bar). The x axis moves from the origin to the left. The y axes moves from the origin down:

The origin of the coordinate system and its axes

The distance from the left border of the client area to the left border of the control is the Left property. The distance from the top border of the client area to the top border of the control is the Top property. These can be illustrated as follows:

To know the amount of space that a form is making available to its child control, you can access the form's ClientSize property.

The Background Color of a Form

The client area of a form is painted with a color specified by the operating system. To change the color to anything you like, you can use the BackColor property. Assign a color from the Color structure to the BackColor property. Here is an example:

open System
open System.Drawing
open System.Windows.Forms

let exercise : Form = new Form()

exercise.Icon <- new System.Drawing.Icon("C:\common\Something.ico")
exercise.Text <- "Windows Fundmentals - Programming"

Exercise.BackColor <- Color.BurlyWood

Application.Run exercise

This would produce:

Back Color

If you look closely, you will notice that only the client area of the form is painted. The borders, since they are not part of the client area, are not painted.

 
 
 

 

The Background Image of a Form

 

Introduction

If you prefer to cover the client area with a picture, use the BackgroundImage property. 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:

open System
open System.Drawing
open System.Windows.Forms

let exercise : Form = new Form()

exercise.Icon <- new Icon("C:\Programs\Book.ico")
exercise.Text <- "A Day at the Beach"
exercise.MaximizeBox <- false

exercise.BackgroundImage <- Image.FromFile("C:\Programs\beach.jpg")

Application.Run exercise

Form Background

Background Image Options

There are two ways you can use a background image on a form. You can fill the whole client area with the picture if the picture is bigger than the client area. If the picture is narrower and/or shorter than the picture, you can resize or repeat it. To assist you with making this decision, the Form class is equipped with a property named BackgroundImageLayout.

The Form.BackgroundImageLayout property is based on the ImageLayout enumeration. Its values are:

  • None: The picture will display once, from the top-left origin of the client area. Here is an example:
    open System;
    open System.Drawing;
    open System.Windows.Forms;
    
    let exercise : Form = new Form()
    
    exercise.Icon <- new System.Drawing.Icon("C:\Programs\Book.ico");
    exercise.Text <- "Fire Wall";
    exercise.MaximizeBox <- false;
    
    exercise.BackgroundImage <- Image.FromFile("C:\Programs\FireWall.png");
    exercise.BackgroundImageLayout <- ImageLayout.None;
    
    Application.Run exercise

  • Center: The picture will display once, in the middle-center of the client area of the form. Here is an example:
    open System;
    open System.Drawing;
    open System.Windows.Forms;
    
    let exercise : Form = new Form()
    
    exercise.Icon <- new System.Drawing.Icon("C:\Programs\Book.ico");
    exercise.Text <- "Fire Wall";
    exercise.MaximizeBox <- false;
    
    exercise.BackgroundImage <- Image.FromFile("C:\Programs\FireWall.png");
    exercise.BackgroundImageLayout <- ImageLayout.Center;
    
    Application.Run exercise

    BackgroundImageLayout <- ImageLayout.Center;

  • Tile: The picture will start displaying from the top-left origin of the client area. Then it will repeat itself horizontally, followed by the next line, then the next, and so on. Here is an example:
    open System;
    open System.Drawing;
    open System.Windows.Forms;
    
    let exercise : Form = new Form()
    
    exercise.Icon <- new System.Drawing.Icon("C:\Programs\Book.ico");
    exercise.Text <- "Fire Wall";
    exercise.MaximizeBox <- false;
    
    exercise.BackgroundImage <- Image.FromFile("C:\Programs\FireWall.png");
    exercise.BackgroundImageLayout <- ImageLayout.Tile;
    
    Application.Run exercise

    BackgroundImageLayout <- ImageLayout.Tile

  • Stretch: The picture will first be positioned in the middle of the client area of the form, then it will stretch itself to occupy the whole client area, and so on. Here is an example:
     
    open System;
    open System.Drawing;
    open System.Windows.Forms;
    
    let exercise : Form = new Form()
    
    exercise.Icon <- new System.Drawing.Icon("C:\Programs\Book.ico");
    exercise.Text <- "Fire Wall";
    exercise.MaximizeBox <- false;
    
    exercise.BackgroundImage <- Image.FromFile("C:\Programs\FireWall.png");
    exercise.BackgroundImageLayout <- ImageLayout.Stretch;
    
    Application.Run exercise
  • Zoom: The compiler will find out which measure is smaller between the width and the height of the client area of the form. That smaller measure will be used for both the width and the height of the picture. Here is an example:
    open System;
    open System.Drawing;
    open System.Windows.Forms;
    
    let exercise : Form = new Form()
    
    exercise.Icon <- new System.Drawing.Icon("C:\Programs\Book.ico");
    exercise.Text <- "Fire Wall";
    exercise.MaximizeBox <- false;
    
    exercise.BackgroundImage <- Image.FromFile("C:\Programs\FireWall.png");
    exercise.BackgroundImageLayout <- ImageLayout.Zoom;
    
    Application.Run exercise

    If the form's width is lower than its height, then the picture will be positioned in the middle of the form:

    If the form's height is lower than its width, then the picture will be positioned in the center of the form:

   
   
 

Previous Copyright © 2015 FunctionX Next