Solved

Accessing properties by name

Posted on 2001-06-04
12
242 Views
Last Modified: 2013-11-23
Hi,

I am trying to access properties by using a text string and am not sure how to go about it, or indeed if it is possible. The sort of thing I am trying to do is as follows:

procedure TForm1.Button1Click(Sender: TObject);
var
xx : TComponent;
yy : TProperty;
ss : string;

begin
  xx  := FindComponent('Edit1');//find the component
  yy := (xx as xx.ClassType).FindProperty('Text');//type cast the component and find the Text property
  yy := 'Test Data'; //write to property
  ss := yy; //read from property
end;

I know this is rubbish but it illustrates the sort of thing I am trying to do.

Please illustrate any possible answers with code.

Thanks

Chris
0
Comment
Question by:ChrisBerry
  • 4
  • 3
  • 3
  • +2
12 Comments
 
LVL 6

Expert Comment

by:edey
ID: 6154036
0
 
LVL 2

Expert Comment

by:DidierD
ID: 6154295
Hi,


Add TypInfo to the uses. Put an editbox and a button on the form and use the code below.

procedure TForm1.Button1Click(Sender: TObject);
var
  xx: TComponent;
  ss : string;
  PropInfo: PPropInfo;
begin
  xx := FindComponent('Edit1');
  PropInfo := GetPropInfo(xx.ClassInfo,'Text');
  if PropInfo <> nil then
  begin
    if PropInfo^.PropType^.Kind = tkLString then
    begin
      SetStrProp(xx, PropInfo, 'test');
      ShowMessage(GetStrProp(xx, PropInfo));
    end;
  end;
end;

See in TypInfo unit for more information. For integer check for tkInteger, etc..
 
TTypeKind = (tkUnknown, tkInteger, tkChar, tkEnumeration, tkFloat,
    tkString, tkSet, tkClass, tkMethod, tkWChar, tkLString, tkWString,
    tkVariant, tkArray, tkRecord, tkInterface, tkInt64, tkDynArray);

In typeInfo you can also find the other getters and setters.


Didier
0
 
LVL 2

Expert Comment

by:DidierD
ID: 6154309
The other getters and setters

function GetOrdProp(Instance: TObject; PropInfo: PPropInfo): Longint; overload;
procedure SetOrdProp(Instance: TObject; PropInfo: PPropInfo;
  Value: Longint); overload;

function GetEnumProp(Instance: TObject; PropInfo: PPropInfo): string; overload;
procedure SetEnumProp(Instance: TObject; PropInfo: PPropInfo;
  const Value: string); overload;

function GetSetProp(Instance: TObject; PropInfo: PPropInfo;
  Brackets: Boolean = False): string; overload;
procedure SetSetProp(Instance: TObject; PropInfo: PPropInfo;
  const Value: string); overload;

function GetObjectProp(Instance: TObject; PropInfo: PPropInfo;
  MinClass: TClass = nil): TObject; overload;
procedure SetObjectProp(Instance: TObject; PropInfo: PPropInfo;
  Value: TObject); overload;
function GetObjectPropClass(Instance: TObject; PropInfo: PPropInfo): TClass; overload;

function GetStrProp(Instance: TObject; PropInfo: PPropInfo): string; overload;
procedure SetStrProp(Instance: TObject; PropInfo: PPropInfo;
  const Value: string); overload;

function GetFloatProp(Instance: TObject; PropInfo: PPropInfo): Extended; overload;
procedure SetFloatProp(Instance: TObject; PropInfo: PPropInfo;
  Value: Extended); overload;

function GetVariantProp(Instance: TObject; PropInfo: PPropInfo): Variant; overload;
procedure SetVariantProp(Instance: TObject; PropInfo: PPropInfo;
  const Value: Variant); overload;

function GetMethodProp(Instance: TObject; PropInfo: PPropInfo): TMethod; overload;
procedure SetMethodProp(Instance: TObject; PropInfo: PPropInfo;
  const Value: TMethod); overload;

function GetInt64Prop(Instance: TObject; PropInfo: PPropInfo): Int64; overload;
procedure SetInt64Prop(Instance: TObject; PropInfo: PPropInfo;
  const Value: Int64); overload;
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 2

Expert Comment

by:DidierD
ID: 6154327
:-(

I just checked out edey's comment and his answer is exactly the same. So don't give me the points.

Didier
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 6154366
listening...
0
 
LVL 6

Expert Comment

by:edey
ID: 6154575
There is an alternative method - for activeX controls.  Use the MS scripting control, then all of your parsing, evaluating & property/method access is taken care of.  For these very reasons I've wrapped most of the common controls (very easy in pro+ ver's of delphi).

GL
Mike
0
 

Author Comment

by:ChrisBerry
ID: 6155244
Hi Mike and Didier,

Thanks for your answers, I will check it out.

Mike, could you please amplify your scripting comment. I have never used this.

Regards

Chris
0
 
LVL 14

Expert Comment

by:DragonSlayer
ID: 6155271
listening to what Mike has to say :)
0
 

Author Comment

by:ChrisBerry
ID: 6155415
I have tested and it works great for simple properties such as a string, integer etc. How do I deal with a property such as a Font or TStrings etc? I know the kind returned is tkClass but I don't know how to access the methods and properties of that class.

Thanks

Chris
0
 
LVL 6

Accepted Solution

by:
edey earned 100 total points
ID: 6156294
A quick scripting example:

uses
 ..., comobj;

procedure TForm1.Button1Click(sender:TObject)
var
 scripting : variant;
begin
 scripting := createOLEObject('ScriptControl');
 scripting.language := 'JScript';
 scripting.addObject('doc',webBrowser1.document.true);
 scripting.eval('doc.innerHTML = 'hello, and welcome to a <b>scripted</b> world');
end;

This snippet assumes the form has a TWebBrowser named webBrowser1 on it.

As for accessing the properties of the member object, you know it's a pointer as you've got a type of tkClass, the pointer can then be simply recast as a descendant of TObject.  Unfortunately I don't know of any way to determine an arbitrary type cast at runtime - you'll have to know what it's likely to be at design time.  Of course any tobject desc. can be cast as tobject, you just lose any functionality not inhernet to TObject.

GL
Mike
0
 

Author Comment

by:ChrisBerry
ID: 6157264
Mike,

Thanks for your help.

Regards

Chris
0
 
LVL 6

Expert Comment

by:edey
ID: 6157280
Glad to be of service :)

GL
Mike
0

Featured Post

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

777 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