Solved

Finding Published Properties At Run-time

Posted on 1997-07-04
7
344 Views
Last Modified: 2010-04-06
I am in the process of designing a generic visual designer that
will be used in a few projects we are starting...in some of our
initial conversations I came up with the idea (since Java can do
it) that it would be really valuable if Delphi could find all
the properties/fields (public/published) of any component.

The idea is this...use right-clicks on some object on a form...
Delphi examines the object and builds a property list of all the
accessible fields of that object so that the user can set
and view them.

JavaBeans allow this kind of thing...and considering the elegance of
Delphi's RTTI, I figured it was probably possible in Delphi too.

Besides just getting the fields/properties of a component...is
it possible to also get the data type of the property??  I would
suspect that if the former is true, then the latter is true too.

And finally...where in the help files should I go looking to start
getting this information??
0
Comment
Question by:mheacock
[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
  • 3
7 Comments
 
LVL 1

Expert Comment

by:ygolan
ID: 1338216
You can find all the information displayed in the Delphi Object Inspector at runtime, for any *published* property.

The information you need is accessed through the TypInfo unit, you can find an example that does it all in the TWriter.WriteProperties and TWriter.WriteProperty methods of TWriter, defined in Classes.pas.

I've implemented a fully working Delphi-like Object Inspector, so you can be assured that this can be implemented.

If you need source code to help you, I am afraid you will need to further raise the points for this question.
0
 
LVL 3

Expert Comment

by:sperling
ID: 1338217
I assume you'd prefer comments first on this one too.

AFAIK you won't find any RTTI documentation in the helpfiles. You need 3rd-party books.

But the code in source\vcl\TypInfo.pas is quite readable, a bit of trying and failing ought to get you going.

Here's some code which'll add property Name, type and value(s) for most (I've skipped a few typekinds) published properties of any object. Just reconstruct the form and step through it. Delphi only generates RTTI for published properties, not public.

Code's tested with D3, it might be somewhat different using D2 but I doubt it.

--
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    ListBox1: TListBox;
    StringGrid1: TStringGrid;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation
uses
  TypInfo;
 
{$R *.DFM}

function TypeName (PropInfo : PPropInfo) : STRING;
begin
  Result := PropInfo^.PropType^^.Name;
end;

function EnumeratedName (PropInfo : PPropInfo; OrdValue : INTEGER) : STRING;
begin
  Result := GetEnumName(PropInfo^.PropType^, OrdValue);
end;

function SetMemberName (PropInfo : PPropInfo; Index : INTEGER) : STRING;
begin
  Result := GetEnumName(GetTypeData(PropInfo^.PropType^)^.CompType^, Index);
end;


procedure GetPropInfo (AObject : TObject; PropInfo : PPropInfo; AStrings : TStrings);
var
  s, v          : STRING;
  ndx,
  OrdValue      : INTEGER;
  FloatValue    : EXTENDED;
begin


  S := 'Name = '+PropInfo^.Name+
       ', Type = ' + TypeName(PropInfo) +
       ', Value = ';

  case PropInfo^.PropType^^.Kind of
    tkUnknown : v := 'Unknown';
    tkInteger : v := IntToStr(GetOrdProp(AObject, PropInfo));
    tkChar    : v := '#'+IntToStr(GetOrdProp(AObject, PropInfo));
    tkFloat   : v := FloatToStrF(GetFloatProp(AObject, PropInfo), ffFixed, 18, 2);
    tkLString,
    tkString  : v := GetStrProp(AObject, PropInfo);
    tkSet :
      begin
        OrdValue := GetOrdProp(AObject, PropInfo);
        for ndx := 0 to 31 do begin
          if ( (1 shl ndx) and OrdValue) <> 0 then begin
            if v = '' then
              v := SetMemberName(PropInfo, ndx)
            else
              v := v + ', ' + SetMemberName(PropInfo, ndx);
          end;
        end;
        v := '['+v+']';
      end;
    tkEnumeration : v := EnumeratedName (PropInfo, GetOrdProp(AObject, PropInfo));
    tkClass : v := '$'+IntToHex(GetOrdProp(AObject, PropInfo), 8);
  end;
  AStrings.Add(s+v);
end;

procedure GetObjectRTTI (AObject : TObject; AStrings : TStrings);
var
  PropList  : PPropList;
  ndx       : INTEGER;
  PropCount : INTEGER;
begin
  PropCount := GetTypeData(AObject.ClassInfo)^.PropCount;
  GetMem(PropList, PropCount * SizeOf(POINTER));
  GetPropList(AObject.ClassInfo, tkAny, PropList);
  for ndx := 0 to PropCount-1 do
    GetPropInfo (AObject, PropList^[ndx], AStrings);
  FreeMem(PropList);
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
  GetObjectRTTI (StringGrid1, Listbox1.Items);
end;

end.
---

Regards,

Erik.
0
 
LVL 3

Expert Comment

by:sperling
ID: 1338218
Humm... ygolan seem to have answered while I was writing...

Erik.
0
Independent Software Vendors: 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!

 
LVL 3

Author Comment

by:mheacock
ID: 1338219
What an excellent answer...

Everyone else I asked (mailing lists, newsgroups) kept saying
"Check out Secrets of Delphi 2".

I'm not usually too keen on buying books...

Anyhow, all I usually need is a good pointer in the right
direction and away I go...typinfo.pas and your code has
helped me...I'm already 30% of the way through my visual
designer...thanks!!!
0
 
LVL 3

Author Comment

by:mheacock
ID: 1338220
"Mark a comment as an answer" doesn't work...please post a
short note, sperling, that says "Grade me!".


0
 
LVL 3

Accepted Solution

by:
sperling earned 200 total points
ID: 1338221
Okie, grade me!  ;)


Regards,

Erik.
0
 
LVL 3

Author Comment

by:mheacock
ID: 1338222
Thanks for the code.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying 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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

749 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