Solved

Indirect addressing

Posted on 2009-05-13
12
201 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
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!

 
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 37

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 37

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 250 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 250 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

Suggested Solutions

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…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

726 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