|
||||||
This article continues the review of RTTI possibilities. We'll learn the generic way of changing any property of any control on a form. Using RTTI allows taking no care of the control's type. You may treat objects that descend from different classes similarly. The 'name' property of any component that descends from TComponent can be accessed using a TComponent reference. Any component that descends from TControl (a control) has height and width properties. So you can write the code like following: For i:=0 to ComponentCount-1 do Or For i:=0 to ComponentCount-1 do However, it's possible only when the property being accessed is visible in the shared ancestor class. When two or more objects have the same property, but the property is not declared public or published in a common ancestor, it can not be accessed using a reference to an ancestor. An example of a property like this is Color. The color property of both TLabel and TEdit is inherited from TControl.
However, this property is declared protected in TControl and re-declared as published in the TEdit and TLabel. For i:=0 to ControlsCount-1 do The decision is to cast controls[i] to proper type: For i:=0 to ControlsCount-1 do Let's look to more elegant way. Through the RTTI you can access property in you know its name, even if objects do not have a common ancestor at all. The RTTI provides a series of functions with names like SetOrdProp, GetOrdProp, SetStrProp,GetStrProp and so forth. These methods require a PPropInfo reference to property. You should already know how to get it (see previous article). The GetXXX functions return value of the property, the SetXXX functions receive a new values as second parameter. The use of these functions is demonstrated in the example. Code example.Figure 1 shows the main form of this project as it might appear while running. Listing 1 contains a source. Figure 1. unit Unit1;
Go To Page: 1 2
The copyright of the article RTTI part 4. Using RTTI with properties in Delphi Programming is owned by . Permission to republish RTTI part 4. Using RTTI with properties in print or online must be granted by the author in writing.
|
||||||
|
|
||||||
|
|
||||||