Solved

Read published properties at run-time.

Posted on 2000-03-01
5
168 Views
Last Modified: 2010-04-04
I can't remember how to read the names of all published properties at run-time.

Peter
0
Comment
Question by:PeterLarsen
  • 3
  • 2
5 Comments
 
LVL 17

Accepted Solution

by:
inthe earned 50 total points
ID: 2573295
hi peter,
i hope this is what your after as longish post :o)

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    SpeedButton1: TSpeedButton;
    ListBox1: TListBox;
    procedure SpeedButton1Click(Sender: TObject);
  private
   FFred: integer;
published
    property Fred: integer read FFred write FFred;

  public
    { Public declarations }
  end;

var
  Form1: TForm1;


implementation

{$R *.DFM}
uses
  TypInfo;
procedure ListProperties(AInstance: TPersistent; AList: TStrings);
var
  i:        integer;
  pInfo:    PTypeInfo;
  pType:    PTypeData;
  propList: PPropList;
  propCnt:  integer;
  tmpStr:   string;
begin
  pInfo := AInstance.ClassInfo;

  if (pInfo = nil) or (pInfo^.Kind <> tkClass) then
    raise Exception.Create('Invalid type information');

  pType := GetTypeData(pInfo); // Pointer to TTypeData.

  AList.Add('Class name: ' + pInfo^.Name);

  // If any properties, add them to the list.
  //
  propCnt := pType^.PropCount;

  if propCnt > 0 then begin
    AList.Add (EmptyStr);
    tmpStr := IntToStr(propCnt) + ' Propert';

    if propCnt > 1 then
      tmpStr := tmpStr + 'ies'
    else
      tmpStr := tmpStr + 'y';

    AList.Add(tmpStr);
    FillChar(tmpStr[1], Length(tmpStr), '-');
    AList.Add(tmpStr);

    // Get memory for the property list.
    //
    GetMem(propList, sizeOf(PPropInfo) * propCnt);
    try
      // Fill in the property list.
      //
      GetPropInfos(pInfo, propList);

      // Fill in info for each property.
      //
      for i := 0 to propCnt - 1 do
        AList.Add(propList[i].Name + ': ' + propList[i].PropType^.Name);
    finally
      FreeMem(propList, sizeOf(PPropInfo) * propCnt);
    end;
  end;
end;

function GetPropertyList(AControl: TPersistent; AProperty: string):
PPropInfo;
var
  i:        integer;
  props:    PPropList;
  typeData: PTypeData;
begin
  Result := nil;

  if (AControl = nil) or (AControl.ClassInfo = nil) then
    Exit;

  typeData := GetTypeData(AControl.ClassInfo);

  if (typeData = nil) or (typeData^.PropCount = 0) then
    Exit;

  GetMem(props, typeData^.PropCount * SizeOf(Pointer));
  try
    GetPropInfos(AControl.ClassInfo, props);

    for i := 0 to typeData^.PropCount - 1 do begin
      with Props^[i]^ do
        if (Name = AProperty) then
          result := Props^[i];
    end;
  finally
    FreeMem(props);
  end;
end;

procedure TForm1.SpeedButton1Click(Sender: TObject);
var
  c: integer;
begin
  ListProperties(self, ListBox1.Items);

  for c := 0 to ComponentCount - 1 do begin
    ListBox1.Items.Add(EmptyStr);

    ListProperties(Components[c], ListBox1.Items);
  end;


end;

end.
0
 
LVL 2

Author Comment

by:PeterLarsen
ID: 2573356
I think there is a easier way to do this - something  like GetName[x].

Regards
Peter
0
 
LVL 17

Expert Comment

by:inthe
ID: 2573586
possibly but not that i know of..the above came from marco cantu's Delphi Developer's Handbook.
i understand delphi5's typinfo unit has better functions maybe take a look at it (i dont have d5)
0
 
LVL 2

Author Comment

by:PeterLarsen
ID: 2575612
That Delphi instruction i'm talking about - with that it is only possible to read published properties.

Does your sample read any kind of properties ??

Regards
Peter
0
 
LVL 2

Author Comment

by:PeterLarsen
ID: 2575909
I have found a similar sample as yours - maybe this is the only way to do it :

http://community.borland.com/delphi/article/0,1410,16457,00.html.

procedure TForm1.FormCreate(Sender: TObject);
     var
       PropList: PPropList;
       i: integer;
     begin
       PropList := AllocMem(SizeOf(PropList^));
       i := 0;
       try
         GetPropList(TForm.ClassInfo, tkProperties + [tkMethod], PropList);
         while (PropList^[i] <> Nil) and (i < High(PropList^)) do
         begin
           ListBox1.Items.Add(PropList^[i].Name + ': '  +
             PropList^[i].PropType^.Name);
           Inc(i);
         end;
       finally
         FreeMem(PropList);
       end;
     end;

Thanks for your time
Peter
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
TVirtualStringTree  search using TEdit 7 121
Mydac connection data base issue 3 158
Delphi 2 60
Delphi Ado Query To Converts Joined Columns Into Rows Based on Primary Key 4 56
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question