Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 171
  • Last Modified:

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





0
f4
Asked:
f4
  • 3
  • 3
  • 2
  • +1
1 Solution
 
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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
 
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now