?
Solved

Indirect addressing

Posted on 2009-05-13
12
Medium Priority
?
206 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
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month16 days, 21 hours left to enroll

864 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