Link to home
Start Free TrialLog in
Avatar of kacor
kacorFlag for Hungary

asked on

Indirect addressing

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
Avatar of SteveBay
SteveBay
Flag of United States of America image

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

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

Avatar of kacor

ASKER

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
Avatar of kacor

ASKER

the 34. line is:

             pos('ebxB',TEdit(Form1.Controls[i]).Name)) > 0 then
should read :
IF pos('ebxB',TEdit(Form1.Controls[i]).Name)) > 0 then
Avatar of kacor

ASKER


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

Better use component instead of control


   for i := 0 to Form1.ComponentCount - 1 do
      begin
      if  (Form1.Components[i] is TEdit) then
.....
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;
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
ASKER CERTIFIED SOLUTION
Avatar of SteveBay
SteveBay
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kacor

ASKER

Thank you guys for the excellent tips. I use both of them.
Janos