Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

dynamic textfields 1

Status
Not open for further replies.

math

Programmer
Mar 21, 2001
56
BE
Hi,

I'm working on a program that uses 10 textfields, these have to be filled at a certain time (with a button), the strings are in an array.
Now I was wondering if it would be possible to use a for-loop to fill the textfields ??
I've named the textfields q1 to q10. and the array goes from array[1] to array[10]..

ofcourse q1 has to have the value of array[1]:

for i:=1 to 10 do begin
q+ i +.text := array;
end;

THIS obviously doesn't work, but isn't there a way??

Thanks so much,
math
 
Have a look at the following code.

What I do is create the controls at run time as an array of controls this way it is east to access them as you require in a loop


Code:
  public
    { Public declarations }
     myTextBoxes : array[1..10] of TEdit;
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.button1Click(Sender: TObject);
var
   i :Byte;
begin
 for i := 1 to 10 do
 begin
   {Create the buttons}
   myTextBoxes[i] := Tedit.Create(Self);
   With myTextBoxes[i] do
   begin
    Parent := Form1;
    Text := 'EditBox '+ inttostr(i);
    Top := i * Height; 
    Tag := i;
    {Assign a OnClick handler} 
    //OnClick :=  myTextBoxesButtonClick;
    Visible := True; 
   end;
  end;

end;



procedure TForm1.Button2Click(Sender: TObject);
var
  myString : array[1..10] of String;
  loop : integer;
begin
   myString[1] := 'one';
   myString[2] := 'two';
   myString[3] := 'three';
   myString[4] := 'four';
   myString[5] := 'five';
   myString[6] := 'six';
   myString[7] := 'seven';
   myString[8] := 'eight';
   myString[9] := 'nine';
   myString[10] := 'ten';
   for loop:=1 to 10 do
   begin
      myTextBoxes[loop].text := myString[loop];
   end;

end;

procedure TForm1.FormDestroy(Sender: TObject);
var
   loop:Byte;
begin

 for loop := 1 to 10 do
   myTextBoxes[loop].free;

end;
Billy H

bhogar@acxiom.co.uk
 
You can say something like

for x := 1 to 10 do
begin
(myForm.FindComponent('q'+inttostr(x)) as TLabel).Text := array[x-1]; //assuming zero based array
end;

TealWren
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top