Characteristics of Windows Controls

 

Introduction 

A Windows control, also called a control, is an object you provide to the user as part of your application. This object allows the user to interact with the computer. As various applications address different issues, there are various types of controls. There are some characteristics that all or most controls share. A characteristic of a control is also referred to as, or called, a property of the control.

There are some characteristics that are appropriate for a category of controls. There are some other characteristics so restrictively particular that only a few or just one control would use them. We will review properties shared by all or many controls. Characteristics that only one or a few controls use will be reviewed when studying those controlled.

Controls Names

Every object of your computer has a name. That is, except for the letters you see on a document, anything that has a "physical" presence on your application must have a name. The name allows you and the operating system to identify the object and refer to it appropriately when necessary.

The first object created on a Windows Application is the form. It is named Form1. When you add a control to a form, it automatically receives a name. For example, the first button would be called button1. Every control is named after its class.

The names automatically given to controls added to an application are incremental. That is, the first button is named button1. If you add another button, it would be named button2, etc. If an application is using various controls, and especially if it is using various copies of the same type of controls, such names become insignificant and can lead to some confusion. Therefore, you should make it a (good) habit to give appropriate names to your controls. For example, if a control deals with an issue related to the employees marital status, the name of the control should reflect it.

When naming controls, you must follow some rules of naming variables. Besides these rules, you should also follow the .Net's suggestions that the name of a control starts in lowercase. Examples are address or age. If the name is a combination of words, the first part should be in lowercase. The first letter of the second part should start in uppercase. Examples are firstName, dateOfBirth.

Control Location

Once a control is part of your application, it must have "physical" presence. Every control has a parent. The parent gives presence to the control. The parent is also responsible for destroying the control when the parent is destroyed. When you create a Windows Application, it creates a default or first form. The parent of this main form is the Windows desktop. In fact any form that is not part of an MDI is located with regards to its parent the desktop.

The location of an object whose parent is the desktop is based on a coordinate system whose origin is located on the top-left corner of the desktop:

The Origin of the Windows default coordinate system

 

The axes of this coordinate system are oriented so the x axis moves from the origin to the right and the y axis moves from the origin down:

The axes of the default coordinate system

The distance from the left side of the desktop to the left border of the form is called the Left property. The distance from the top section of the desktop to the top border of the form is referred to as the Top property.

The distance from the left border of the desktop to the right border of the form is called the Right property. The distance from the top border of the desktop to the bottom border of the form is called the Bottom property. These characteristics can be illustrated as follows:

 

Representation of a window object on the desktop

 

The rectangular area that the desktop makes available to all objects you can see on your screen is called the client area because this area is reserved to the children, called clients, of the desktop.

When a form has been created, we saw in the previous lesson that 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, excluding the border, of the form to the right border, excluding the border, of the form. It also spans from the top side, just under the title bar, to the bottom border, excluding the 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:

The location of a control inside a parent window

 

To change the location of a control, at design time, click and drag it in the desired direction. Once you get to the necessary location, release the mouse. Alternatively, select it and, in the Properties window, click the + button of the Location field. Type the Left value on the X field and type the Top value in the Y field.

To programmatically change the location of a control, call the Point constructor from the Drawing namespace of the System with the desired value. Here is an example:

private void Form1_Load(object sender, System.EventArgs e)
		{
			Location = new System.Drawing.Point(200, 180);
		}

To retrieve the location of a control, declare a Point variable and assign the Location property of the desired control to it. This would be done as follows:

private void Form1_Load(object sender, System.EventArgs e)
		{
			System.Drawing.Point Pt = Location;
		}

 

Control Size

A control size is the amount of space it is occupying on the screen or on a form. It is expressed as its width and its height. The width of a control is the distance from its left to its right borders. The height is the distance from the top border of the control to its bottom border. When you create a form or add a control to a form, it assumes a default size, unless you draw the control while adding it.

To set the size of a control, at design time, select it. Then, position the mouse on one of its handles and drag in the desired direction. To assist you with this, the mouse cursor changes depending on the handle you grab. The cursors used are:

Cursor Role
Cursor to heighten or shrink a selected object Heightens or shortens the control
Cursor to narrow or enlarge a selected object Narrows or enlarges the control
Resizes the control in the North-East <-> South-West direction
Resizes the control in the South-East <-> North-West direction

Alternatively, to change the size of a control, select it and, in the Properties window, click the + button of the Size field. Then type the desired value for the Width and the desired value for the Height.

To programmatically change the size of a control, assign the desired values of either or both its Width and Height properties. Here is an example:

public static void main(String[] args) 
	{
		Form1 Fm = new Form1();
		Fm.set_Size(new System.Drawing.Size(450, 320));
		Application.Run(Fm);
	}

To find out the size of a control, declare a Size variable and assign it the Size property of the control.

The Bounding Rectangle of a Control

When a control is positioned on the screen, we saw that it can be located by its Left and its Top properties. A control also occupies an area represented by its Width and its Height. These four values can be grouped in an entity called the bounding rectangle and represented by the Bounds property.

Control Text or Caption

Some controls must display text to indicate what they are used for. Such a text can also be referred to as a caption. Some controls may not need this text and some others should or must display it. For a form, this text displays on the title bar. For most other controls, it displays in the middle of their body. If you create a form, it automatically gets text on its title bar. If you add a control to a form, it automatically gets a default text if that control is supposed to display it.

To change the text of a control, select it. Then, in the Properties window, click the Text field and type the desired value. At design time, you can only provide a static type of text to the control. If you want text that can be changed in response to another action, you must change it programmatically.

To programmatically change the text of a control, assign a string to its Text property. Here is an example:

public static void main(String[] args) 
	{
		Form1 Fm = new Form1();
		Fm.set_Text("Employment Application");
		Fm.set_Size(new System.Drawing.Size(450, 320));
		Application.Run(Fm);
	}

The text can also be created from an expression or an operation.

Control Visibility

In order to use a control, the user must be able to see it. When a control is not visible, it is hidden. This aspect is controlled by the control's visibility.

The visibility of a control is controlled by the Visible property, which is a Boolean type. To make a control visible, at design time, set its Visible property to True. To hide it, set this property to False.

To programmatically display or hide a control, assign the true or false value to its Visible property. If you assign a true value to this property, if the control was hidden, it would become visible. If the control was already visible, nothing would happen. On the other hand, if you assign a false value to a visible control, it would become hidden. If it was already hidden, nothing would happen.

To find out whether a control is visible or hidden at one time, check the state of its Visible property, whether it is true or false.

Control Availability

When a control is visible, in order to use it, it must allow it to the user. A control is referred to as enabled if it can receive input from the user. For example, if it is a control that can be clicked, at a certain time, it must be "clickable". If it is not, clicking it would not make any difference. This aspect is controlled by the Enabled property.

To make a control available to the user, set its Enabled property to True. To prevent the user from interacting with a control, set its Enabled property to False.

To programmatically enable or disable a control, assign a value of true or false to its Enabled property. To find out whether a control is enabled or disabled, check its Enabled state by comparing to a true or false value.

A control that is enabled displays in its normal or regular appearance. A control that is disabled sometimes appears dimmed.

Tab Ordering

A user can navigate through controls using the Tab key. When that key has been pressed, the focus moves from one control to the next. By their designs, not all controls can receive focus and not all controls can participate in tab navigation. Even controls that can receive focus must be explicitly included in the tab sequence.

At design time, the participation to tab sequencing is controlled by the TabStop property on the Properties window. Fortunately, every control that can receive focus is also configured to have this Boolean property set to true. If you want to remove a control from this sequence, set its TabStop value to false.

If a control has the TabOrder property set to true, to arrange the navigation order of controls, at design time, change the value of its TabIndex field. The value must be a positive short integer.

 


Previous Copyright 2003 FunctionX, Inc. Next