Solved

Something like a eval('oblect'+1).whatever ???????

Posted on 2003-12-08
3
207 Views
Last Modified: 2011-09-20
okie.... supose u have 10 text boxies and i named them like box1, box2, box3..... now i want to make a do while or something..

and i want to call all those 10 boxies in this do while but of corse just using one single call.


In ActionScript there is a function named eval that does that:
counter = 0
same = 0
while counter < 11 do
{
   counter++
   if eval('box'+counter).text == 'lala'
   {
      same++
   }
}

Well there ar any possible to make it in Delphi ?

Thanks in advance :)
0
Comment
Question by:furmiga
3 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 9899969
look at the findcomponent-method

just as hint, no time yet

meikl ;-)
0
 
LVL 26

Accepted Solution

by:
Russell Libby earned 125 total points
ID: 9900184

Perhaps something like...

function Eval(Name: String): TComponent;

  function _Eval(Component: TComponent; Name: String): TComponent;
  var   dwIndex:       Integer;
  begin

     // Set default result
     result:=nil;

     // Check direct component children
     if Assigned(Component.FindComponent(Name)) then
        // Found the component
        result:=Component.FindComponent(Name)
     else
     begin
        // Check the children's children
        for dwIndex:=0 to Pred(Component.ComponentCount) do
        begin
           result:=_Eval(Component.Components[dwIndex], Name);
           if Assigned(result) then break;
        end;
     end;
  end;

begin

  // Start at the application level
  result:=_Eval(Application, Name);

end;

This would allow you to do:

procedure TForm1.Button1Click(Sender: TObject);
var  counter:    Integer;
     same:       Integer;
begin

  counter:=0;
  same:=0;

  while (counter < 11) do
  begin
     Inc(counter);
     if TEdit(Eval('Edit'+IntToStr(counter))).Text = 'lala' then
     begin
        Inc(same);
     end;
  end;

  ShowMessage(IntToStr(same));

end;


The only drawback to this is that you have to cast the result of Eval(...) to the expected component type, like TEdit, TButton, TLabel, etc..., because the function returns a TComponent (or nil).
Note though, you can also use the "as" operator to cast, or use the "is" operator to perform dynamic checking. Using the "with" statement could also make coding a little simpler:

eg:

if (Eval('Edit'+IntToStr(counter)) as TEdit).Text = 'lala' then...
~or~
if Eval('Edit'+IntToStr(counter)) is TEdit then...
~or~

with Eval('Edit'+IntToStr(counter)) as TEdit do
begin
  if (Text = 'lala') then Inc(same);
end;


Hope this helps,
Russell


0
 
LVL 7

Author Comment

by:furmiga
ID: 9900336
ty a lot man that solved my problem :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to convert wav to mp3 in delphi 9 169
Send message from delphi to whatsapp, is it possible? 3 1,139
When I am typing into a TEdit last word repeats 7 116
CheckListBox usage 3 48
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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

932 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now