![]() |
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. |
|
|

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.
Like properties of the other data types, you can create a property of bool or Boolean type.
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.
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. 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.
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: ![]() 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. 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: ![]()
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. 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: ![]() 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: ![]()
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.
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.
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: ![]() 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.
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:
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.
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.
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();
}
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();
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 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.
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: ![]() 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.
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: ![]() 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.
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.
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.
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.
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.
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. |
|
|
||
| Home | Copyright © 2007 FunctionX, Inc. | |
|
|
||