![]() |
The Forms of an Application: |
|
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 horizontal measurements move from the origin to the right. The vertical measurements move from the origin to the bottom: ![]() 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 class Exercise : System.Windows.Forms.Form
{
public Exercise()
{
InitializeComponent();
}
private void InitializeComponent()
{
Icon = new System.Drawing.Icon(@"C:\Programs\RedBook.ico");
Text = "Windows Fundmentals - Programming";
Left = 228;
Top = 146;
}
}
Alternatively, you can assign a Point variable to the form's Location property. Here is an example: private void InitializeComponent()
{
Icon = new System.Drawing.Icon(@"C:\Programs\RedBook.ico");
Text = "Windows Fundmentals - Programming";
Location = new System.Drawing.Point(228, 146);
}
At design time, the X and Y values of the Location property allow you to specify a position for the form. At run time, the Left and Top properties accomplish the same purpose. 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:
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: using System;
using System.Windows.Forms;
public class Exercise : System.Windows.Forms.Form
{
public Exercise()
{
InitializeComponent();
}
private void InitializeComponent()
{
Icon = new System.Drawing.Icon(@"C:\Programs\RedBook.ico");
Text = "Windows Fundmentals - Programming";
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.
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. using System;
using System.Drawing;
using System.Windows.Forms;
public class Exercise : System.Windows.Forms.Form
{
public Exercise()
{
InitializeComponent();
}
private void InitializeComponent()
{
Icon = new Icon(@"C:\Programs\RedBook.ico");
Text = "Windows Fundmentals - Programming";
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.
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: private void InitializeComponent()
{
Icon = new Icon(@"C:\Programs\RedBook.ico");
Text = "Windows Fundmentals - Programming";
ShowInTaskbar = false;
}
|
|
|
||
| Previous | Copyright © 2007 FunctionX, Inc. | Next |
|
|
||