Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Read published properties at run-time.

Posted on 2000-03-01
5
Medium Priority
?
175 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
[X]
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
  • 3
  • 2
5 Comments
 
LVL 17

Accepted Solution

by:
inthe earned 150 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

610 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