?
Solved

indirect reference

Posted on 2002-03-21
4
Medium Priority
?
153 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 220 total points
ID: 6888155
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
ID: 6888256
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
ID: 6888326
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
ID: 6888368
You are welcome. Glad to hear that I've helped you.

best regards, Geo
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Screencast - Getting to Know the Pipeline
Suggested Courses
Course of the Month13 days, 8 hours left to enroll

750 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