|
|
|||
|
|
It's possible to get the names of an object's properties through the RTTI. Let's look at typinfo.pas unit and see GetPropList function.
The first argument is a pointer to an object's RTTI information. You can use the ClassInfo property of an object to obtain this pointer. The second parameter is a set of the property types. TTypeKind enumerated type is defined as foolowing TTypeKind = (tkUnknown, tkInteger, tkChar, tkEnumeration, tkFloat, Note that tkInt64 and tkDynArray constants are included only in Delphi 4. There are no corresponding types (Int64
and Dynamic array) in Delphi 3. TTypeKinds is a set of TTypeKind values. const The third argument is a pointer to an array of TPropInfo records. Each record holds information about particular
property. TPropInfo record includes fields such as Name or PropType. Working with components by number.Each object of the TComponent derived class (for example Form) provides indexed access to all components owned by it through Components and ComponentCount properties. property ComponentCount: Integer; FindComponent function returns a reference to an instance of an object based on string - name of the component. function FindComponent(const AName: string): TComponent; Code exampleThe use of GetPropList is demonstrated in the PROPLIST project. The main form is shown in Figure 1. This form includes two comboboxes whose contents are populated at run-time with the form's onCreate event handler. ComboBox1 is filled with TTypeKind values. This part of the code was discussed in the previous article. ComboBox2 is filled with the names of the components appearing on the form. When user changes either ComboBox text, onChange event occurs. The program stores a kind of properties to retrieve in the tk variable. This part of code uses GetEnumValue function. Refer the previous article for details. If user typed wrong value or leave ComboBox1 empty, the program will retrieve the names of properties of any kind. (See tkAny constant declaration above). The ListBox1 is populated with the property names of the object selected in the ComboBox2. This code is generic because the PPropInfo is extracted using the ClassInfo property of a component, a pointer to which is returned by Go To Page: 1 2
The copyright of the article RTTI Part 2. Getting object property listing. in Delphi Programming is owned by Lyapin Ilya. Permission to republish RTTI Part 2. Getting object property listing. in print or online must be granted by the author in writing.
For a complete listing of article comments, questions, and other discussions related to Lyapin Ilya's Delphi Programming topic, please visit the Discussions page. |
||
|
|
|||