Freelance Writing Jobs | Today's Articles | Sign In

 
Browse Sections

RTTI Part 2. Getting object property listing.


FindComponent function.

Figure 1.

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;

type
TForm1 = class(TForm)
ComboBox1: TComboBox;
Label1: TLabel;
Label2: TLabel;
ComboBox2: TComboBox;
ListBox1: TListBox;
procedure FormCreate(Sender: TObject);
procedure ComboBox1Change(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation
{$R *.DFM}
uses TypInfo;

procedure TForm1.FormCreate(Sender: TObject);
var
tk:TTypeKind;
s:String;
i:integer;
begin
// fill prop types combo
for tk:=Low(TTypeKind) to High(TTypeKind) do
begin
s:=GetEnumName(TypeInfo(TTypeKind),ord(tk));
ComboBox1.Items.Add(s);
end;
ComboBox1.ItemIndex:=1;
// fill objects combo
ComboBox2.Items.Clear;
for i:=0 to ComponentCount-1 do
begin
s:=Components[i].Name;
ComboBox2.Items.Add(s);
end;
ComboBox2.ItemIndex:=0;

ComboBox1Change(Nil);
end;

procedure TForm1.ComboBox1Change(Sender: TObject);
var
i,count:integer;
tk:TTypeKinds;
s:String;
c:TComponent;
pPList:PPropList;
begin
s:=ComboBox1.Text;
i:=GetEnumValue(TypeInfo(TTypeKind),s);
if (i >= Ord(Low(TTypeKind))) and (i <= Ord(High(TTypeKind))) then
tk:=[TTypeKind(i)]
else
tk:=tkAny;
c:=FindComponent(ComboBox2.text);
if Assigned(c) then
begin
pPList:=AllocMem(Sizeof(pPList^));
try
ListBox1.Items.Clear;
count:=GetPropList(c.ClassInfo,tk,pPList);
for i:=0 to count - 1 do
ListBox1.Items.Add(ppList[i].Name);
finally
FreeMem(pPList);
end;
end;
end;

end.

Summary.

The RTTI contains information about every published property of every class. Using the procedures and functions from typInfo.pas you can extract this information at run-time.

Full source code and compiled executable is available at http://www.suite101.com/files/topics/724...
The program is written for Borland Delphi 4, but this code must work under Delphi 3/4/5.
You may experiment with the program for better understanding. I would like to answer any questions that you may have.

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.

Go To Page: 1 2

Articles in this Topic    Discussions in this Topic

;