Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

indirect reference

Posted on 2002-03-21
4
Medium Priority
?
152 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

618 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