manipulate object via string

Hi,
is there a function in d4 like eval() in php.
want to change color:

Not:
form1.edit1.color:=clwhite;

But somthing like this:
xyz('form1.edit1.color:=clwhite');

thanks





f4Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

esoftbgCommented:
If you can manipulate property Color of an object as it is: Form1.Edit1.Color := clWhite;

what could be the reason for you to manipulate it as string: S := 'form1.edit1.color:=clwhite';

An object is a pointer in memory, but string is a consecution of characters ????

I really do not understand....
0
grolschisgoodCommented:
no delphi has no function like eval() in php. you can however use strings to call procedures of the name of the string, or even use strings as enumerated types.

Uses
        TypInfo;

Function ReturnColorType(Value : String) : TColorType;
begin
 
Result := TColorType(GetEnumValue(TypeInfo(TColorType),Value));
End;

form1.color:=ReturnColorType("White");

by using this you can have a drop down box on the user interface where the user can select a color, and then use the read the selected color from the drop down and turn it into a color type.  

you would have to hardcode which forms colors will be effected by the users choosen color though


0
grolschisgoodCommented:
sry didnt finish,

the "White" past into the procedure will need to converted to a exact string of a color type so you need to add "cl" to the front of it

Function ReturnColorType(Value : String) : TColorType;
begin
 Value := 'cl' + Value;
Result := TColorType(GetEnumValue(TypeInfo(TColorType),Value));
End;
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

f4Author Commented:
thank you for answering. i see immediately that my try, idea was bad. but my first idea give you a more detailed view to the problem. gladly i would work with object-pointers, but don't know how to do.  

i have many eventhandler-procedures like
edit1.click(Sender: TObject);
now i want to change color of editfields with a smart common procedure. but in this common procedure i dont't have the posibility to locate the object to which i want to change the color-property.

first i look for a way to send the objectname ('edit1') as argument to the colorchange-routine.

edit1.click(Sender: TObject);
begin
colorchange('edit1');
end;

edit2.click ...
edit3.click ...
...

procedure colorchange(x);
begin
x.color:=clwhite;
end;
0
NoturnoCommented:
You can use this procedure:

procedure TForm1.ColorChange(EditName: String; NewColor: TColor);
var
   A: Integer;
begin
   for A:= 0 to ControlCount - 1 Do
      if (Controls[A].Name = EditName) and (Controls[A] is TEdit) Then
         TEdit(Controls[A]).Color:= NewColor;
end;

This procedure is on the Form1, so it will change the color of the TEdit in Form1 that matches the EditName parameter. Using the color specified by NewColor.

You can call this function like:
Form1.ColorChange('Edit1', clRed);

Try!

Regards,

Noturno
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
f4Author Commented:
Thank you, that's it :-).
0
esoftbgCommented:
Why to make Delphi code complex when it can be so easy ?

procedure TForm1.SetColor(Edit: TEdit; C: TColor);
begin
  Edit.Color := C;
end;

When the question is not correct, the answer may not to be correct too.

My friend f4, never search for something you know !!!
Next code is a sample of a super unnecessary search for TEdit's name:

  for A:= 0 to ControlCount - 1 Do
     if (Controls[A].Name = EditName) and (Controls[A] is TEdit) Then
        TEdit(Controls[A]).Color:= NewColor;

If you know the name you can manipulate component directly without search !!!!!!!!

0
f4Author Commented:
esoftbg, thank you for your help too.
0
esoftbgCommented:
f4, some day you will understand Delphi.
:-) For now: happy search by name !
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.