Link to home
Start Free TrialLog in
Avatar of ChrisBerry
ChrisBerryFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Accessing properties by name

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
Avatar of edey
edey

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
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;
:-(

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

Didier
listening...
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
Avatar of ChrisBerry

ASKER

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
listening to what Mike has to say :)
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
ASKER CERTIFIED SOLUTION
Avatar of edey
edey

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Mike,

Thanks for your help.

Regards

Chris
Glad to be of service :)

GL
Mike