Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Read published properties at run-time.

Posted on 2000-03-01
5
169 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

765 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