![]()
We know that 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. To create a form-based application, you can derive a class from Form.
A form is made of various sections. 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. To specify or change the icon, declare a variable of type Icon 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: private void InitializeComponent()
{
System.Drawing.Icon customIcon =
new System.Drawing.Icon(@"C:\Programs\RedBook.ico");
Icon = customIcon;
}
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.
On the right side of the system icon, there is the caption. By default, the caption displays the name of the form. If you want to specify or change the caption, assign the desired string to the Text property. Here is an example: private void InitializeComponent()
{
Icon = new System.Drawing.Icon(@"C:\Programs\RedBook.ico");
Text = "Windows Fundmentals - Programming";
}
This would produce:
On the right side of the caption, there are three small
buttons called the system buttons, made of the Minimize ( ![]() 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 ( The Maximize (
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: private void InitializeComponent()
{
Icon = new System.Drawing.Icon(@"C:\Programs\RedBook.ico");
Text = "Windows Fundmentals - Programming";
ControlBox = true;
MinimizeBox = true;
MaximizeBox = false;
}
This would produce:
Like every control on an application, a form has a parent: the desktop. 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 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);
}
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:
To specify this characteristic on a form or to change it, 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.
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. 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. 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. 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;
}
A form's size is the amount of space it is occupying on the screen. 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: ![]() To change the size of a form, assign the desired values to either or both its Width and Height properties. Here is an example: private void InitializeComponent()
{
Icon = new Icon(@"C:\Programs\RedBook.ico");
Text = "Windows Fundmentals - Programming";
Width = 425;
Height = 308;
}
Alternatively, you can assign a Size value to the Size property of the form. Here is an example: private void InitializeComponent()
{
Icon = new Icon(@"C:\Programs\RedBook.ico");
Text = "Windows Fundmentals - Programming";
Size = new Size(425, 308);
}
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.
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: private void InitializeComponent()
{
Icon = new Icon(@"C:\Programs\RedBook.ico");
Text = "Windows Fundmentals - Programming";
FormBorderStyle = FormBorderStyle.Fixed3D;
}
This would produce:
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: ![]() 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.
To change the background color of a form, assign a color from the Color structure to the BackColor property. Here is an example: public class Exercise : System.Windows.Forms.Form
{
public Exercise()
{
InitializeComponent();
}
private void InitializeComponent()
{
Icon = new Icon(@"C:\Programs\RedBook.ico");
Text = "Windows Fundmentals";
BackColor = Color.BurlyWood;
}
}
This would produce:
If you prefer to cover the client area with a picture, use the BackgroundImage property instead. To 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: private void InitializeComponent()
{
Icon = new Icon(@"C:\Programs\RedBook.ico");
Text = "A Day at the Beach";
MaximizeBox = false;
BackgroundImage = Image.FromFile(@"E:\Programs\beach.jpg");
}
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:
The form is implemented by the Form class from the System.Windows.Forms namespace. The Form class is equipped with a constructor that allows you to dynamically create it. After a form has been created, it must be loaded to display on the screen. When a form is being loaded, it fires the Load() event which is of type EventArgs. This event is fired when a form is about to be displayed for the first time. Therefore, it can be used to perform last minute initializations.
In order to use a form other than the one that is active, you must activate it. To do this, you can call the Activate() method. Its syntax is: public void Activate(); When a form is activated, it fires the Activated event, which is an EventArgs type of event.
If a form is not active and you want to bring it to the top, you must activate it, which fires 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 Deactivate() event which is an EventArgs type.
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: public void Close(); When this method is called, the process of closing a form starts. At this time, the Closing() event is fired. The Closing() event is implemented by the CancelEventArgs class through the CancelEventHandler delegate. The CancelEventArgs class is equipped with only the Cancel property. The CancelEventArgs.Cancel property allows you to cancel or to continue with the closing of the form. If you set the CancelEventArgs.Cancel property to true, the event will be ignored as if it were not fired. If you want the event to continue closing the form, you can set this property to false. This event occurs before the form is actually closed, giving you time to let the form be closed, prevent the form from being closed, or take any other necessary action. After a form has been closed, a Closed() event is fired. 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.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||