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
Solved

Indirect addressing

Posted on 2009-05-13
12
200 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

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…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

808 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