Solved

indirect reference

Posted on 2002-03-21
4
144 Views
Last Modified: 2010-04-05
Hi everybody,

a string variable contains the name of an object. How can I set the properties of this object referencing the variable?

wbr

Janos
0
Comment
Question by:kacor
  • 2
  • 2
4 Comments
 
LVL 17

Accepted Solution

by:
geobul earned 55 total points
Comment Utility
Hi,

Place a label, an edit and a button on a form and try this:

uses
 TypInfo;

function SetProperty(fForm: TForm; sControl: string; sProperty: string; value: variant): integer;
var
  i: integer;
  PRecProp: PPropInfo;
begin
 result := 0;
 try
  for i := 0 to fForm.ComponentCount - 1 do
    if (fForm.Components[i] is TControl) then
      if (fForm.Components[i]).Name = sControl then begin
        PRecProp := GetPropInfo(fForm.Components[i].ClassInfo, sProperty);
        if Assigned(PRecProp) then
          case VarType(value) of
            varInteger, varBoolean: SetOrdProp(fForm.Components[i], PRecProp, value);
            varString: SetOrdProp(fForm.Components[i], PRecProp, integer(string(value)));
          end;
        break;
      end;
 except
   result := 1;
 end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  SetProperty(Form1,'Label1','color', clRed);
  SetProperty(Form1,'Label1','caption', 'Hello world!!!');
  SetProperty(Form1,'Edit1','enabled', false);
end;

Regards, Geo
0
 
LVL 10

Author Comment

by:kacor
Comment Utility
Hi geo,

tnx, I'll try it

regards

Janos

PS. In the meantime I've got 5 points :-)))
0
 
LVL 10

Author Comment

by:kacor
Comment Utility
Thanks, geo,

it works perfectly. I've made a little change:

it was placed a 2nd label and the button click procedure

procedure TForm1.Button1Click(Sender: TObject);
begin
 Label1.Color := clWhite;
 Label2.Color := clWhite;
 Edit1.Color := clWhite;
 SetProperty(Form1,Edit1.Text,'color', clRed);
end;

so after setting to white all controls, according to the content of edit1 the control's color will be changed to red

Thanks again

with best regards

Janos
0
 
LVL 17

Expert Comment

by:geobul
Comment Utility
You are welcome. Glad to hear that I've helped you.

best regards, Geo
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

728 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

10 Experts available now in Live!

Get 1:1 Help Now