Solved

indirect reference

Posted on 2002-03-21
4
146 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
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi inherited method 6 76
Convert MS Word document to a PDF file 9 91
DBGrid or StringGrid ? 6 104
Breakpoint doesn't stop in my variable 3 30
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

856 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