?
Solved

Indirect addressing

Posted on 2009-05-13
12
Medium Priority
?
203 Views
Last Modified: 2012-05-06
Hi Experts,
I have 20 edit boxes (ebxB01 .. ebxB20). Edit box ebxA contains a number between 1 .. 20. Based on this value I'd like to hide/show this edit boxes. For example ebxA.Text=5 in this case ebxB01 .. ebxB05 are visible and ebxB06 .. ebxB20 are hidden.
I don't want to to write 20 times this command. Would be possible to solve this job using a for ...next loop?
Every idea appreciated.
kind regards
Janos
0
Comment
Question by:kacor
[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
  • 4
  • 4
  • 2
  • +2
12 Comments
 
LVL 14

Expert Comment

by:SteveBay
ID: 24374584
You will want to start with a loop that looks something like this:
procedure TForm1.Button1Click(Sender: TObject);
var i : Integer;
begin
     for i := 0 to Form1.ControlCount - 1 do
          begin
          if (Form1.Controls[i] is TEdit) and
             pos('ebxB',TEdit(Form1.Controls[i]).Name)) > 0 then
               begin
               if TEdit(Form1.Controls[i]).Text = SomeValue then
                    begin
                    DoSomeThing;
                    end;
               end;
          end;
end;

Open in new window

0
 
LVL 14

Expert Comment

by:SteveBay
ID: 24374683
A slighlty more detailed version:
procedure TForm1.Button1Click(Sender: TObject);
var  i : Integer;
     FieldNum : Integer;
begin
     for i := 0 to Form1.ControlCount - 1 do
          begin
          // establish that this is a TEdit
          // and that this is one with the correct name
          if (Form1.Controls[i] is TEdit) and
             pos('ebxB',TEdit(Form1.Controls[i]).Name)) > 0 then
               begin
               // extract the edit number from the name
               FieldNum := StrToIntDef(copy(Form1.Controls[i]),5,2),0);
               case FieldNum of
               1 : DoFunction01;
               2:  DoFunction02;
               //..
               //..
               20: DOFunciton20;
               else : DoFunctionDefault; //  StrToIntDef returned 0
                    end;//case
               end; // if
          end; // for
end;

Open in new window

0
 
LVL 10

Author Comment

by:kacor
ID: 24374959
Hi SteveBay,
I tested the first offered solution with one button, two edit boxes and the following procedure

procedure TForm1.Button1Click(Sender: TObject);
var i : Integer;
begin
     for i := 0 to Form1.ControlCount - 1 do
          begin
          if (Form1.Controls[i] is TEdit) and
             pos('ebxB',TEdit(Form1.Controls[i]).Name)) > 0 then
               begin
                    Form1.Controls[i].Visible:=False;
               end;
          end;
end;

but I got an error message

[Error] Unit1.pas(34): Operator not applicable to this operand type

Janos
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 10

Author Comment

by:kacor
ID: 24374972
the 34. line is:

             pos('ebxB',TEdit(Form1.Controls[i]).Name)) > 0 then
0
 
LVL 14

Expert Comment

by:SteveBay
ID: 24374995
should read :
IF pos('ebxB',TEdit(Form1.Controls[i]).Name)) > 0 then
0
 
LVL 10

Author Comment

by:kacor
ID: 24375032

unit Unit1;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;
 
type
  TForm1 = class(TForm)
    Button1: TButton;
    ebxB2: TEdit;
    ebxB1: TEdit;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
 
var
  Form1: TForm1;
 
implementation
 
{$R *.dfm}
 
procedure TForm1.Button1Click(Sender: TObject);
var i : Integer;
begin
     for i := 0 to Form1.ControlCount - 1 do
          begin
          if (Form1.Controls[i] is TEdit) and
             pos('ebxB',TEdit(Form1.Controls[i]).Name)) > 0 then
               begin
                    Form1.Controls[i].Visible:=False;
               end;
          end;
end;
 
end.

Open in new window

0
 
LVL 6

Expert Comment

by:bokist
ID: 24375058
Better use component instead of control


   for i := 0 to Form1.ComponentCount - 1 do
      begin
      if  (Form1.Components[i] is TEdit) then
.....
0
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 24375070
you could also use Self instead of Form1

and you can also use FindComponent

for I := 1 to 20 do
begin
  edt := TEdit(FindComponent(Format('ebxB%2.2d', [I])));
  if Assigned(edt) and edt.Visible then
  begin
    ShowMessage(edt.Name + ' = ' + edt.Text);
  end;
end;
0
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 24375099
controlcount will not iterate the labels, so less iterations,
so better with controlcount instead of component ...
unless you have other non wincontrol components off course
0
 
LVL 14

Accepted Solution

by:
SteveBay earned 1000 total points
ID: 24375152
Sorry about that. Itr should be :

          if (Form1.Controls[i] is TEdit) and
             (pos('ebxB',TEdit(Form1.Controls[i]).Name) > 0) then

Open in new window

0
 
LVL 6

Assisted Solution

by:twocandles
twocandles earned 1000 total points
ID: 24375400
Place all the related edits inside a container (panel, groupbox...) and loop through the container components instead of forms'.

Another approach

procedure TForm2.Button1Click(Sender: TObject);
const
    MAX_EDIT_BOXES = 2;
    EDIT_BOX_PREFIX = 'ebxB';
    EB_FORMAT = '%.2d';
var
    i, firstBox : integer;
    c : TComponent;
    s : string;
begin
    // Check edit contains a valid integer or give it a non useful value
    firstBox := StrToIntDef( edit1.Text, MAX_EDIT_BOXES + 1 );
    // Loop
    for i := firstBox to MAX_EDIT_BOXES do
    begin
        c := FindComponent( EDIT_BOX_PREFIX + Format( EB_FORMAT, [i] ) );
        if( c is TEdit )then
            TEdit( c ).Visible := false;
    end;
end;

Open in new window

0
 
LVL 10

Author Closing Comment

by:kacor
ID: 31580956
Thank you guys for the excellent tips. I use both of them.
Janos
0

Featured Post

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!

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses
Course of the Month7 days, 19 hours left to enroll

765 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