Solved

Indirect addressing

Posted on 2009-05-13
12
194 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
 
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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…
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…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now