0

please help me, i am new about Delphi. I need to generate textbox dynamically like

text[1]
text[2]
...
text[n]

And i can also retrieve their value maybe like this (example),

for (i=1; i<=n; i++)
arrayOfTxt[i] = text[i].text;

Is it possible on Delphi? if it is possible, then how to do that?

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
Ryan Arief
  • 983
  • 2
  • 16
  • 36
  • Start by reading the docs about arrays – David Heffernan Jul 12 '15 at 13:42
  • 1
    Welcome to Stack Overflow. Instead of asking a simple yes-no question, you should just *assume* that the answer is what you want it to be (probably yes), and then ask whatever follow-up question you *really* need help with while working under the initial assumption. So, assume Delphi allows arrays like that; what's preventing you from solving your problem? – Rob Kennedy Jul 12 '15 at 15:01
  • i'm sorry before,,, what i meant is, if that problem can has possibility way to solve, then i wold like to get the answer,.. thankyou – Ryan Arief Jul 12 '15 at 15:43

1 Answers1

3

To answer your stated question: Yes, it is possible.

To elaborate: Delphi has what's called Dynamic Arrays, where you don't specify the boundaries of the index at compile-time, but only at run-time.

So a function to create a user-supplied number of edit boxes (TEdit) could be like this:

TYPE
  TEditArr = ARRAY OF TEdit;

FUNCTION MakeEditBoxes(ParentForm : TForm ; Count : Cardinal) : TEditArr;
  VAR
    I : Cardinal;

  BEGIN
    SetLength(Result,Count);
    FOR I:=1 TO Count DO BEGIN
      Result[PRED(I)]:=TEdit.Create(ParentForm);
      Result[PRED(I)].Parent:=Parent
    END
  END;

You would use it something like this:

Place the following line in your form's class declaration:

Edits : TEditArr;

Then create the boxes like this:

PROCEDURE TForm1.Button1Click(Sender : TObject);
  VAR
    E : TEdit;
    Y : Cardinal;

  BEGIN
    Edits:=MakeEditBoxes(Self,20);
    Y:=0;
    FOR E IN Edits DO BEGIN
      E.Top:=Y; E.Left:=0;
      INC(Y,E.Height+8)
    END
  END;

To access their texts, you can use something like this (assuming you want to copy all their texts to a single mulit-line edit box (TMemo) on your form):

.
.
.
VAR E : TEdit;
.
Memo1.Lines.Clear;
FOR E IN Edits DO Memo1.Lines.Add(E.Text);
.
.
.

or - if you want to access them by index:

.
.
.
VAR I : Integer;
.
Memo1.Lines.Clear;
FOR I:=LOW(Edits) TO HIGH(Edits) DO Memo1.Lines.Add(Edits[I].Text);
.
.
.

If you want the text of a specific edit box (f.ex. the 3rd one), you'd use

Edits[2].Text

remembering that dynamic arrays always start their index at 0 (but if you want to loop around all indexes, use LOW and HIGH as that's what they are there for, and it'll make it clear what you are doing to anyone reading your code, even though they may or may not be familiar with Delphi). Note how I only specify the count of edit boxes at ONE place in my code, and if I want to have a different number, it'll only be that one place that I have to alter - all the remaining code will automatically adapt to the correct number without modifications.

HeartWare
  • 7,464
  • 2
  • 26
  • 30
  • 1
    Looping from 1 to N and accessing with pred(I) certainly works but is far from idiomatic. I point this out because I feel somebody should. – David Heffernan Jul 12 '15 at 21:35
  • this is what i need, thankyou very much.. but i got error like this [Error] uhitung.pas(140): Operator not applicable to this operand type >>FOR E IN Edits DO for now i just skip that part on my code,.. – Ryan Arief Jul 13 '15 at 08:07
  • Which version of Delphi are you using? FOR...IN was introduced several versions ago (Delphi 2005 I think). If you are using an older version than this, you can use the FOR...TO construct instead to iterate over all edit boxes. – HeartWare Jul 13 '15 at 10:46