?
Solved

manipulate object via string

Posted on 2003-03-28
9
Medium Priority
?
170 Views
Last Modified: 2010-04-04
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
Comment
Question by:f4
[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
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 12

Expert Comment

by:esoftbg
ID: 8228391
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
 
LVL 1

Expert Comment

by:grolschisgood
ID: 8229656
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
 
LVL 1

Expert Comment

by:grolschisgood
ID: 8229663
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
Industry Leaders: 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!

 

Author Comment

by:f4
ID: 8229910
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
 
LVL 1

Accepted Solution

by:
Noturno earned 60 total points
ID: 8230545
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
 

Author Comment

by:f4
ID: 8231279
Thank you, that's it :-).
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 8235106
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
 

Author Comment

by:f4
ID: 8237081
esoftbg, thank you for your help too.
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 8237410
f4, some day you will understand Delphi.
:-) For now: happy search by name !
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses
Course of the Month11 days, 7 hours left to enroll

752 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