Home

The Form's Position

 

The Form's Location

When you create an application and while you are designing the form, it is fixed in the top-left corner of the Form Designer. This is not the position the form would have when it runs. In fact, by default, a random position is set for the form when it comes up. Fortunately, you can specify where the form would be located when it comes up and you have various alternatives.

Like every control on an application, a form has a parent: the desktop. The desktop is the wide area of the monitor easily seen when the computer comes up. Everything on the computer is located with regards to this main parent.

In the same way, a form uses the desktop to determine its "physical" location. 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. The distance from the desktopís top border to the formís top border is specified by the Top property. Therefore, the Left and the Top values are known as the formís location. This can be illustrated as follows:

To specify the default location of a form when the application is opened, in the Properties window, click the + button of the Location field to reveal the values of the location. If a value of a property is set to 0, the compiler would select a random value for it when the form comes up. Otherwise, you can change each value to a valid natural number.

If you want to programmatically set the location of the form, you can assign a value to its Left and/or its Top properties. Here is an example:

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

    void InitializeComponent()
    {
	Left = 228;
	Top  = 146;
    }
};

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

#include <windows.h>

#using <System.dll>
#using <System.Drawing.dll>
#using <System.Windows.Forms.dll>

using namespace System;
using namespace System::Drawing;
using namespace System::Windows::Forms;

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

    void InitializeComponent()
    {
	Location = Point(228, 146);
    }
};

The Startup Position of a Form

At design time, the X and Y values of the Location property allow you to specify a precise position for the form. At run time, the Left and Top properties accomplish the same purpose. The Form class provides an alternative position to specify the location of the form relative to its parent. This is the role of the StartPosition property, 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, change the value of its StartPosition combo box in the Properties window. To specify this characteristic when programmatically creating a form or to change this property at run time, call the FormStartPosition enumeration to select the desired value and assign it to the StartPosition property of the form. Here is an example:

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

    void InitializeComponent()
    {
	StartPosition = FormStartPosition::WindowsDefaultLocation;
    }
};

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.

Practical LearningPractical Learning: Setting a Form's Default Position

  1. Click the middle of the form to make sure it is selected
  2. In the Properties window, click StartPosition to reveal its combo box. Click the arrow of its combo box and select CenterScreen
  3. Execute the application to view the result. Notice that it is centered on the screen
  4. Close it and return to your programming environment

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, in the Properties window, change the desired value of the WindowState property to Maximized or Minimized.

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:

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

    void InitializeComponent()
    {
        Text = L"A Day at the Beach";
        Icon = gcnew System::Drawing::Icon(L"C:\\Programs\\RedBook.ico");
		
	WindowState = FormWindowState::Maximized;
    }
};

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, 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:

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

    void InitializeComponent()
    {
        Text = L"A Day at the Beach";
        Icon = gcnew System::Drawing::Icon(L"C:\\Programs\\RedBook.ico");
	WindowState = FormWindowState::Maximized;

        ShowInTaskbar = false;
    }
};
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.
 
 

Previous Copyright © 2007 FunctionX, Inc. Next