Solved

Accessing properties by name

Posted on 2001-06-04
12
245 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

808 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