kacor
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
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
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;
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.Con trols[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
I tested the first offered solution with one button, two edit boxes and the following procedure
procedure TForm1.Button1Click(Sender
var i : Integer;
begin
for i := 0 to Form1.ControlCount - 1 do
begin
if (Form1.Controls[i] is TEdit) and
pos('ebxB',TEdit(Form1.Con
begin
Form1.Controls[i].Visible:
end;
end;
end;
but I got an error message
[Error] Unit1.pas(34): Operator not applicable to this operand type
Janos
ASKER
the 34. line is:
pos('ebxB',TEdit(Form1.Con trols[i]). Name)) > 0 then
pos('ebxB',TEdit(Form1.Con
should read :
IF pos('ebxB',TEdit(Form1.Con trols[i]). Name)) > 0 then
IF pos('ebxB',TEdit(Form1.Con
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.
Better use component instead of control
for i := 0 to Form1.ComponentCount - 1 do
begin
if (Form1.Components[i] is TEdit) then
.....
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.2 d', [I])));
if Assigned(edt) and edt.Visible then
begin
ShowMessage(edt.Name + ' = ' + edt.Text);
end;
end;
and you can also use FindComponent
for I := 1 to 20 do
begin
edt := TEdit(FindComponent(Format
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
so better with controlcount instead of component ...
unless you have other non wincontrol components off course
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you guys for the excellent tips. I use both of them.
Janos
Janos
Open in new window