Solved

Accessing properties by name

Posted on 2001-06-04
12
239 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now