Home

The Properties of a Windows Control

 

The Properties Window

 

Overview of Controls Properties

Like the classes we have seen in the previous lessons, the objects of an application use characteristics that are defined are properties. A property is a piece of information that characterizes or describes a control. It could be related to its location or size. It could be its color, its identification, or any visual aspect that gives it meaning. The properties are primarily created as we reviewed in the previous lesson. Once the properties have been defined for a control, you can customize them when using a control on your application. The properties of an object can be changed either at design time or at run time. You can also manipulate these characteristics both at design and at run times. This means that you can set some properties at design time and some others at run time.

At design time, the properties of a control are listed in an appropriate window named the Properties window. After adding a control to a form, you can see its characteristics in the Properties window:

To access a property of a control at design time, first click the desired control property from the Toolbox and then add it to the form or to a container control. To change a property of a control at design time, on the form, click the control to select it.

The items in the Properties window display in a list set when installing Microsoft Visual Studio. In the beginning, you may be regularly lost when looking for a particular property because the list is not arranged in a strict order of rules. You can rearrange the list. For example, you can cause the items to display in alphabetic order. To do this, in the title bar of the Properties window, click the Alphabetic button Alphabetic. To restore the list, you can click the categorized button Categorized.

When a control is selected, the Properties window displays only its characteristics. When various controls have been selected, the Properties window displays only the characteristics that are common to the selected controls.

Practical LearningPractical Learning: Introducing the Properties Window

  1. Start Microsoft Visual C++
  2. To create a new application, on the main menu, click File -> New -> Project...
  3. In the New Project dialog box, make sure that Visual C++ Projects is selected in the Project Types list. In the Templates list, click Windows Forms Application
  4. Replace the content of the Name box with Fundamentals1
     
    New Project
  5. Click OK
  6. Display the Toolbox. In the Toolbox, click the ProgressBar button ProgressBar and click the form
  7. Still in the Toolbox, click the Timer button Timer and click the form
  8. To add a new class to the project, on the main menu, click Project -> Add Class...
  9. In the Categories list, expand Visual C++ and click C++
  10. In the Templates list of the Add Class dialog box, click C++ Class and click Add
  11. Set the Class Name to CTriangle and press Enter
  12. To see the properties of the form, on the main menu, click Window -> Form1.h [Design]
  13. Click the Class View tab and expand Fundamentals1
  14. To see the properties of a class, in Class View, click CTriangle
  15. To view the properties of a control, click the only control in the form and observe the Properties window
  16. To view the properties of another control, in the bar under the form, click the timer and observe the Properties window. Notice that it displays different properties

Properties Categories

Each field in the Properties window has two sections: the propertyís name and the property's value:
 


The name of a property is represented on the left column. This is the official name of the property. Notice that the names of properties are in one word. You can use this same name to access the property in code.

The box on the right side of each property name represents the value of the property that you can set for an object. There are various kinds of fields you will use to set the properties. To know what particular kind a field is, you can click its name. To set or change a property, you use the box on the right side of the propertyís name: the property's value, also referred to as the field's value.

General Properties

 

Empty Fields

Property Empty  

By default, these fields have nothing in their properties. Most of these properties are dependent on other settings of your program. For example, you can set a menu property for a form only after you have created a menu.

To set the property on such a field, you can type in it or select from a list. 

 

Practical LearningPractical Learning: Setting Empty Properties

  1. On the form, click the only control (the ProgressBar control) to select it
  2. In the Properties window, click Tag, type ctlProgress and press Enter

Text Fields

There are fields that expect you to type a value. Most of these fields have a default value.

To change the value of the property, click the name of the property, type the desired value, and press Enter. While some properties, such as the Text, would allow anything, some other fields expect a specific type of text, such as a numeric value.

Property Text

Practical LearningPractical Learning: Setting a Text-Based Property

  • In the Properties window, click (Name), type pgrSeconds and press Enter

Boolean Properties

Like properties of the other data types, you can create a property of bool or Boolean type.

When creating a Windows application, in the Properties window, some fields can have only a True or False value. To change their setting, you can either select from the combo box or double-click the property to toggle to the other value.

 

Expandable Fields

 
Some fields have a + button. This indicates that the property has a set of sub-properties that actually belong to the same property and are defined together. To expand such a field, click its + button and a Ė button will appear:

To collapse the field, click the Ė button. Some of the properties are numeric based, such as the Size. With such a property, you can click its name and type two numeric values separated by a comma. Some other properties are created from an enumerator or a class. If you expand such a field, it would display various options. Here is an example from the Font property:

With such a property, you should select from a list.

Practical LearningPractical Learning: Using Expandable Properties

  1. Click an empty area on the form
  2. In the Properties window, click the + button of Size
  3. Click Width and type 620
  4. Click Height and type 244
  5. On the form, click the ProgressBar
  6. In the Properties window, click Location
  7. Type 10,10 and press Enter
  8. Click Size, type 580, 32 and press Enter

Action Fields

Some fields would require a value or item that needs to be set through an intermediary action. Such fields display an ellipsis button Ellipsis . When you click the button, a dialog box would come up and you can set the value for the field.

Property Action
 

Numeric Properties

 

Introduction

A property is referred to as numeric if it must hold a number. To set it, you can click the name of the field and type the desired value. Here is an example:

If you type an invalid value, you would receive a message box notifying you of the error:

When this happens, click OK and type a valid value. If the value is supposed to be an integer, make sure you don't type it as a decimal number.

 

Practical LearningPractical Learning: Setting a Numeric Property

  1. On the form, click the ProgressBar control
  2. In the Properties window, click Value, type 65 and pres Enter

The Location

A control can be visually represented on the screen by a geometric point on a coordinate system. A point is a pixel on the monitor screen, on a form, or on any object of your application. A point is represented by its coordinates with regard to the object that "owns" the point:

Representation of a Point

To identify the concept of a point, the System::Drawing namespace provides the Point structure. One of the properties of the Point structure is X, which represents the horizontal distance of the point from the top-left corner of the object that owns the point. Another property, Y, represents the vertical measurement of the point with regards to the top-left corner of the object that owns the point.

The Size

To represent the size of something, the System::Drawing namespace defines the Size structure that is equipped with two main properties. There are four characteristics that define a Size value: its location and its dimensions. A Size value must have a starting point (X, Y) just as the Point object was illustrated earlier. The Width is the distance from the left to the right borders of a Size value. The Height property represents the distance from the top to the bottom borders of a Size value:

Size Representation

 

Based on this, to create a Size object, if you know only its location (X and Y), you can use the following constructor:

public:
    Size(Point pt);

After declaring a variable with this constructor, you can access its Width and Height properties to complete the definition of the Size object. If you already have the location of a Size object by other means, you may not be interested in (re)defining its location. In this case, you may only want to specify the dimensions of the variable. To do this, you can use the following constructor:

public:
    Size(int width, int height);

Besides Size, the System::Drawing namespace also provides the SizeF structure. It uses the same properties as Size except that its members float values.

The Location and Size

A rectangle is a geometric figure that has four sides. To support this figure, the System.Drawing namespace provides the Rectangle and the RectangleF structures. A rectangle can be represented as follows:

Rectangle

Like every geometric representation in your program, a rectangular figure is based on a coordinate system whose origin is located on a top-left corner. The object that "owns" or defines the rectangle also owns this origin. For example, if the rectangle belongs to a control that is positioned on a form, then the origin is on the top-left corner just under the title bar of the form, provided the form has a title bar.

To completely represent it, a rectangle is defined by its location and its dimensions. The location is defined by a point on the top-left corner of the rectangle. The distance from the left border of the object that owns the rectangle to the left border of the rectangle is represented by a property called Left. The distance from the top border of the object that owns the rectangle to the top border of the rectangle is represented by a property called Top. The distance from the left to the right borders of the rectangle is represented by a property called Width. The distance from the left to the right borders of the rectangle is represented by a property called Height. The distance from the left border of the object that owns the rectangle to the right border of the rectangle is represented by a property called Right. The distance from the top border of the object that owns the rectangle to the bottom border of the rectangle is represented by a property called Bottom. Based on this, a rectangle can be illustrated as follows:

Rectangle Representation

 

To create a rectangle, you must provide at least its location and dimensions. The location can be represented by a Point value and the dimensions can be represented with a Size value. Based on this, you can use the following constructor to declare a Rectangle variable:

public:
    Rectangle(Point location, Size size);

This constructor requires that you define a Point and a Size in order to use it. If instead you know the integer values of the location and dimensions, you can use the following constructor to declare a Rectangle object:

public:
    Rectangle(int x, int y, int width, int height);

At any time, you can access or retrieve the characteristics of a Rectangle object as illustrated in the above picture from its properties. You use the same names we used in the picture.

Besides the Rectangle structure, the System::Drawing namespace provides the RectangleF structure that uses the same definition as Rectangle, except that it is defined with float values instead of integers.

Enumerated Properties

 

Creating an Enumerated Property

As done for a regular data type, you can create a property that is an enumeration type. To create the property, start with the property keyword followed by the name of the enumeration and a name for the property. Here is an example:

using namespace System;

public enum class FlowerArrangement
{
	Basket  = L'A',
	Bouquet = L'U',
	Vase    = L'V',
	Bundle  = L'D',
	Any
};

public ref class CFlower
{
private:
    FlowerArrangement arng;
    double price;

public:
    property FlowerArrangement Arrangement;
    property double UnitPrice
    {
        double get() { return price; }
	void set(double p) { price = p; }
    }

    CFlower();
    ~CFlower();
};

CFlower::CFlower()
{
    Arrangement = FlowerArrangement::Any;
    UnitPrice   = 0.00;
}

CFlower::~CFlower()
{
}

void ShowFlower(CFlower ^ %one)
{
    Console::WriteLine(L"== Flower Order ==");
    Console::Write(L"Arrangement:  ");
    Console::WriteLine(one->Arrangement);
    Console::WriteLine(L"Price:        {0:C}", one->UnitPrice);
}

int main()
{
    CFlower ^ inspiration = gcnew CFlower;
	
    inspiration->Arrangement = FlowerArrangement::Bouquet;
    inspiration->UnitPrice   = 32.25;
	
    ShowFlower(inspiration);

    Console::WriteLine(L"");
    return 0;
}

This would produce:

== Flower Order ==
Arrangement:  Bouquet
Price:        $32.25

Press any key to continue . . .

As you can see, the property can be simple, read-only, write-only, or read/write. To create any of these types, follow the techniques we reviewed in Lesson 14 for creating properties.

List-Based Fields

To change the value of some of the fields, you would use their combo box to display the available values. After clicking the arrow, a list would display:

Property Selection

There are various types of list-based fields. Some of them display just two items. To change their value, you can just double-click the field. Some other fields have more than two values in the field. To change them, you can click their arrow and select from the list. You can also double-click a few times until the desired value is selected.

Enumerated Properties

Some properties are used to specify the alignment of text or picture on the control. The field of such a property is equipped with an arrow like the one of a combo box. When you click the arrow of the field, it displays a window with 8 positions:

You will be asked to click the indicated position. Each position has a name based on the enumerator that controls the property. The values of this type of property are:

TopLeft TopCenter TopRight
TopLeft TopCenter TopRight
MiddleLeft MiddleCenter MiddleRight
MiddleLeft MiddleCenter MiddleRight
BottomLeft BottomCenter BottomRight
BottomLeft BottomCenter BottomRight

To programmatically use an enumerated property, type its name, followed by the :: operator, followed by the desired member of the enumeration and assign it to the property. Here is an example:

System::Void btnSubmit_Click(System::Object ^ sender, System::EventArgs ^ e)
{
	 ctlObject->Dock = DockStyle::Top;
}

Notice that you must know the name of the enumeration.

Characteristics of Windows Controls: Control Identification

 

Introduction

Most of the controls you will use to create your applications are defined in the .NET Framework and, as mentioned in the previous lesson, each is based on a particular class. To provide them with basic and common characteristics, all visual Windows controls of the .NET Framework are based on a class called Control which is available in the System::Windows::Forms namespace of the System.Windows.Forms.dll assembly. Based on this, the characteristics common to .NET Framework graphical controls are accessed and managed from one point, then inherited by those controls.

Practical LearningPractical Learning: Introducing Characteristics of Controls

  1. To start a new program, on the main menu, click File -> New -> Project...
  2. In the Templates list, click Windows Forms Application
  3. In the Name box, type Identification1 and click OK
  4. On the Toolbox, click the Auto Hide button
  5. On the Toolbox, click the Label control Label and click the form (no need for precision)
  6. On the Toolbox again, click the Label Label and click an empty area of the form (no need for precision)
  7. Once again, on the Toolbox, click Label Label and click an unoccupied area of the form (no need for precision)
  8. One more time, on the Toolbox, click Label Label and click an unoccupied area of the form (no need for precision)

A Control's Handle

Up to 2002, Microsoft Windows programmers relied on a library called Win32 to create applications for the operating system. The Win32 library was the only resource of classes (in fact structures), functions, etc, that gave functional instructions to the operating system. The other languages such as Pascal, Visual Basic, etc, directly or indirectly used these resources in their own "dialect" to communicate with Microsoft Windows. The Win32 library is still around (it can never disappear unless the operating system changes completely) and serves as the platform for Microsoft Windows programming. To harmonize programming for this platform, Microsoft created the .NET Framework as a substitute library. This is the library used in Microsoft Visual Studio 2005 programming environment.

Most of the functionality of Win32 has been redefined in the .NET Framework. The operations were implemented in various libraries or assemblies. Some other operations, such as those related to the Registry, were kept in the Microsoft::Win32 namespace. Because the Win32 library was written in C, Visual C++ is the only programming environment of Visual Studio 2005 that directly understands Win32 without always needing to import a DLL of Win32. For example, in the previous lesson, you noticed that we used the window.h library and the WinMain() function without caring where they came from. Many other (unfortunately not all) Win32 functions can be easily used like that.

The Win32 library uses a concept called handle to represent each object of an application. A handle in Win32 is simply an unsigned integer used in place of a control. The new .NET Framework also uses handles to internally represent controls but defines a handle as a pointer to an integer (IntPtr). Based on this, every control has a handle. You have a choice of using it or not but it exists. The handle is created by the operating system when the application comes up. This means that you don't need to create it but you can access it to retrieve its value.

To access the handle to a control, you can call its Handle property. Here is an example:

Form1(void)
{
	InitializeComponent();
	this->Text = this->Handle.ToString();
}
 

The Text of a Control

Some controls, as we will see, are text-based, meaning they are meant to display or sometimes request text from the user. For such controls, this text is referred to as caption while it is simply called text for some other controls. This property is not available for all controls.

If a control displays text, it then has a Text property in the Properties window. After adding such a control to a form, its Text field would hold the same text as its name. At design time, to change the text of the control, click its Text field and type the desired value. For most controls, there are no strict rules to follow for this text. Therefore, it is your responsibility to type the right value.

The text provided in a Text field of a text-based control can only be set ďas isĒ at design time. If you want the text to change while the application is running, you can format it. For example, such a control can display the current time or the name of the user who is logged in. These format attributes cannot be set at design time. To change the text of a text-based control at run time, either assign a simple string or provide a formatted string to the Text property. Here is an example:

this->checkBox1->Text = DateTime::Today.ToString();

Practical LearningPractical Learning: Setting Controls Text

  1. On the form, click label1
  2. In the Properties window, click Text and type Length:
  3. On the form, click label2 and type Height:
  4. On the form, click label3 and type Perim:
  5. On the form, click label4 and type Area:
  6. On the form, click Perim:
  7. In the Properties window, in the Text field, click between m and : then type eter to produce Perimeter:

The Name of a Control

To create a control, the primary piece of information you must provide is its name. This allows you and the compiler to know what control you are referring to when the program is running. Specifying the name of a control may depend on the technique you decide to use to create the control.

After adding a control to a form, it automatically receives a name. In the Properties window, the controlís name displays in the (Name) field.
The Name property is in parentheses so that, since it is the most regularly used property, if you alphabetically arrange the names of properties in the Properties window, the (Name) property will be on top. This would help you to easily find it.

The default name of a newly added control reflects the name of its control. To differentiate newly added controls of the same class, the Properties window adds an incremental number. For example, if you click the TextBox button on the Toolbox and click the form, the control would be named TextBox1. If you add a second TextBox control, it would be named TextBox2. This causes the default names to be incremental. Since a program usually consists of many controls, it is usually a good idea to rename your controls and give them meaningful and friendlier names. The name should help you identify what the button is used for.

To change the name of a control, on the Properties window, click the (Name) field, type the desired name and press Enter. You can also change the name of a control programmatically but you should avoid doing that unless you have a good reason to. If you want to change the name of a control, use the Properties window. To programmatically change the name of a control, access its Name property and assign a string, using the C++ rules of variable names. Here is an example:

this->checkBox1->Name = L"chkPepperoni";

To retrieve the name of a control, access its Name property.

 

Practical LearningPractical Learning: Naming Controls

  1. Press and hold Ctrl
  2. On the Toolbox, click the TextBox button TextBox
  3. Click the right section of the form four times (no need for precision)
  4. On the Toolbox, click the Pointer button Pointer
  5. On the form, click textBox1
  6. In the Properties window, click (Name) and type txtLength

Control Selection

 

Single Control Selection

If you visually add a control to a form (at design time), in order to perform any type of configuration on the control, you must first select it. Sometimes you will need to select a group of controls.

To select a control, if you know its name, you can click the arrow of the combo box in the top section of the Properties window and select it.

To select a control on the form, you can simply click it. A control that is selected indicates this by displaying 8 small squares, also called handles, around it. Between these handles, the control is surrounded by dotted rectangles. In the following picture, the selected rectangle displays 8 small squares around its shape:

Form 2

After selecting a control, you can manipulate it or change its characteristics, also called properties. To change some, if not most, of the characteristics of a control, you use the Properties window:

When a control is selected, the Properties window displays only its characteristics.

Practical LearningPractical Learning: Naming Controls

  1. In the top combo box of the Properties window, select textBox2
     
    Properties Window: Control Selection
  2. Notice that it becomes selected in the form. In the Properties window, click (Name) and type txtHeight
  3. In the same way, select textBox3 and change its Name to txtPerimeter
  4. Select textBox4 and change its Name to txtArea
  5. Save all

Multiple Control Selection

To select more than one control on the form, click the first. Press and hold either Shift or Ctrl, then click each of the desired controls on the form. If you click a control that should not be selected, click it again. After selecting the group of controls, release either Shift or Ctrl that you were holding. When a group of controls is selected, the last selected control displays 8 handles too but its handles are white while the others are black. In the following picture, a form contains four controls, three controls are selected:

Form 3

Another technique you can use to select various controls consists of clicking on an unoccupied area on the form, holding the mouse down, drawing a fake rectangle, and releasing the mouse:

Every control touched by the fake rectangle or included in it would be selected:

When various controls have been selected, the Properties window displays only the characteristics that are common to the selected controls.

Practical LearningPractical Learning: Selecting and Using Various Controls

  1. On the form, click one of the text boxes
  2. Press and hold Shift
  3. Click each of the other three text boxes. When the four text boxes have been selected, release Shift
  4. In the Properties window, click Text
  5. Type 0.00 and press Enter. Notice that the string in each text box has changed

Control Deletion

If there is a control on your form but you don't need it, you can remove it from the application. To delete a control, first select it and then click or press Delete. You can also right-click a control and click Cut. To remove a group of controls, first select them, then click or press Delete or right-click the selection and click Cut.

Design Time Controls Location

 

Introduction

The controls added to a parent window are confined to the area of the body offered by that window. After adding it to a window, the control is positioned in the body of the parent using a Cartesian coordinate system whose origin is located on the top-left corner of the parent window. If the parent is a form, the origin is located just under the title bar to the left. The horizontal measurements move from the origin to the right. The vertical measurements move from the origin to the bottom:

The distance from the controlís left border to the parentís left border is referred to as the Left property. The distance from the controlís top border to the parentís top border is referred to as the Top property. The Left and Top values are known as the controlís location. This can be illustrated as follows:

When you click a control on the Toolbox and click its parent window, the Left and Top values are set where the mouse landed. One of the operations you will perform during design consists of correctly positioning your controls to give them a better location and take advantage of the form's real estate. Various options are available to do this. To assist you with setting control's location, the form designer draws aligned dots on the form, forming columns and rows.

The Client Area

As mentioned already, a control is confined to the area made available by its parent. This section is called the client area. For a form, the client area is the body of the form without the title bar, its borders and other sections we have not mentioned yet such as the menu, scroll bars, etc:

 

Besides the form, every control also has a client area. The role of the client area is to specify the bounding section where the control can be accessed by other controls positioned on it. Based on this, a control can be visible only within the client area of its parent. Since not all controls can be parent, we will come back to this property in future lessons.

The client area is programmatically represented by a property called ClientRectangle.

Control Locking

In the next section we will review various ways of positioning controls on a parent. One technique we will review consists of moving one or more controls. In some cases, while working on one or more controls for better positioning, you may not want one particular control or a certain group of controls to be moved around. If you want to, you can "fix" a controls so that it cannot be moved (or resized) even if you click and start dragging it. This is referred to as locking the control or the group of controls.

To lock a control, click in on the form. Then, in the Properties window, set its Locked Boolean property to True. To lock a group of controls, first select them on the form. Then, in the Properties window, set the Locked Boolean property to True.

Control Moving

One of the techniques you can use to change the location of a control or a group of controls consists of moving it. To move one control, click and hold the mouse on it, then drag in the desired direction, and release the mouse. This technique allows you to move a control one unit at a time in either direction. You can also click the control, then press one of the arrow keys to move one unit at a time, either left to move the control left, up to move the control up, right to move the control in the right direction, or down to move the control down.

To move a control with more precision, click the control to select it, press and hold Ctrl, then click one of the arrow keys, either left to move the control left, up to move the control up, right to move the control in the right direction, or down to move the control down.

To move a group of controls, select them first. Then click and drag any area in the selection to the desired location. Alternatively, once you have  the controls, you can press one of the arrow keys to move the whole group.

When moving either a control or a group using either the mouse or the keyboard, the control or the group would follow the grids on the form and it can move only one grid mark at a time. This allows you to have a better alignment of controls. If you want to move the control or the group in smaller units than those of the grid, press and hold Ctrl. Then press one of the arrow keys. Once the control or the group is positioned to your liking, release the Ctrl key.

To programmatically move a control, which is equivalent to changing the values of the Left or the Top properties at run time, assign the desired respective values. If you set a negative value for the Left field, the left border of the control would be hidden. In the same way, a negative Top value would hide the top border of the control. Make sure you use valid integer values; otherwise you would receive an error when you compile the project.

The location of a control is also defined by its Location property, which is a Point value we reviewed earlier. Based on this, you can set or change the location of a control using its Location property and passing it a Point.

 
 

Previous Copyright © 2007, yevol.com Next