Visit our Sponsor   Visit our Sponsor
delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
498 Users Online NOW
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi



Loremo - the 1.5 liter car coming in 2009




Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (1)


How to change properties of objects just by name?Format this article printer-friendly!Bookmark function is only available for registered users!
Using typinfo to enrich your applications
Product:
Delphi 5.x (or higher)
Category:
Open Tools
API / IDE
Skill Level:
Scoring:
Last Update:
08/28/2001
Search Keys:
delphi delphi3000 article borland vcl code-snippet typinfo rtti ota properties setpropvalue getobjectprop components
Times Scored:
12
Visits:
4154
Uploader: Jürgen Sommer
Company:
Reference: N/A
 
Question/Problem/Abstract:
How can i access properties of classes that are not implemented via  the uses-clause, just knowing their names ( by string)?
Answer:



You have to use the TypInfo unit.
Simple properties, like strings and integer, can be accessed in the
following manner:

-------------------------------------------------------------
Uses TypInfo;

procedure AlterProp(AName, APropName, AValue:string);
var  i : integer;
     C : TComponent;
begin
    // Run through all Components to find the right Component
    for i:=0 to Form1.Componentcount-1 do
    begin
        C := Form1.Components[i];
        if (C.Name = AName) then
        begin
            SetPropValue(C,APropName, AValue);
       end;
    end;
end;
-------------------------------------------------------------

Now there are also some Properties like Font. How do i reach those
sub-properties?
Here is a solution to that:

-------------------------------------------------------------
Uses TypInfo;

procedure AlterFontColor(AName:string; AColor:TColor);
var  i : integer;
     C : TComponent;
     AObj : TObject;
begin
    // Run through all Components to find the right Component
    for i:=0 to Form1.Componentcount-1 do
    begin
        C := Form1.Components[i];
        if (C.Name = AName) then
        begin
            AObj := GetObjectProp(C,'Font');
            SetPropValue(AObj,'Color',AColor);
        end;
    end;
end;
-------------------------------------------------------------

And finally you have many indexed properties like TStrings or stuff.
Now how do i reach those indexed properties?
All indexed properties are stored in TCollection-Objects. So you
have to Typecast them like in the following function:

-------------------------------------------------------------
Uses TypInfo;

procedure AlterIndexObject(ACompName, APropName:string; ACaption: string);
var  i : integer;
     C : TComponent;
     ACollectionItem, AObj : TObject;
begin
    // Run through all Components to find the right Component
    for i:=0 to Form1.Componentcount-1 do
    begin
        C := Form1.Components[i];
        if (C.Name = ACompName) then
        begin
            AObj:= GetObjectProp(C,APropName);
            ACollectionItem := TCollection(AObj).Items[0];
            SetPropValue(ACollectionItem, 'Caption', ACaption);
        end;
    end;
end;
-------------------------------------------------------------

There are many possibilities for this functions to use in your projects.
Mainly there are usefull for language-changings or skin-components.
The possibilities are endless.
I hope my article was somewhat usefull for you.
I like to share knowledge with other developers.

keep on coding :-)

Jürgen Sommer





Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment
Good BUT...
    Azza Azza (Sep 4 2001 11:23AM)

Any idea on how I can execute a procedure/function using the run-time information?
Respond














 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
Peganza
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)