### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Author Topic: Make a Dynamic Array of TEdit like a Tower  (Read 1689 times)

#### Boleeman

• Hero Member
• Posts: 656
##### Make a Dynamic Array of TEdit like a Tower
« on: January 13, 2024, 08:47:06 am »
Oops, sorry posted in wrong forum section. I will post in beginners instead.

Kinda lost trying to work out how to make a 2D dynamic array of TEdit controls shaped like a tower, where each row above has one less TEdit (as shown in the attached picture).
The connection between the number of TEdits at the bottom and the total TEdits would be N * (N+1)/2, so 4 TEdits at the bottom would need 4+3+2+1 or 4*(4+1)/2

I looked at an examples of making a 2D label array with rows and columns but I am still stumped.

I would also like to place random numbers withing a certain range at the bottom row to make a simple maths game, where the right number of random numbers are picked according to how many TEdits are on the bottom row.

« Last Edit: January 13, 2024, 08:54:09 am by Boleeman »

#### Eugene Loza

• Hero Member
• Posts: 729
##### Re: Make a Dynamic Array of TEdit like a Tower
« Reply #1 on: January 13, 2024, 09:32:35 am »
You can create elements dynamically. E.g. like this:

Code: Pascal  [Select][+][-]
1. procedure TForm1.FormCreate(Sender: TObject);
2. const
3.   LinesCount = 6;
4. var
5.   Line: Integer;
6.   Column: Integer;
7.   Edit: TEdit;
8. begin
9.   Randomize;
10.   for Line := 0 to Pred(LinesCount) do
11.     for Column := 0 to Line do
12.     begin
13.       Edit := TEdit.Create(Self);
14.       Edit.OnChange := nil; //someting meaningful
15.       Edit.Parent := Self;
16.       Edit.Height := 30;
17.       Edit.Width := 50;
18.       Edit.Alignment := taCenter;
19.       Edit.Top := Line * Edit.Height;
20.       Edit.Left := (Pred(LinesCount) - Line) * Edit.Width div 2 + Column * Edit.Width;
21.       Edit.Text := Random(100).ToString;
22.     end;
23. end;
My FOSS games in FreePascal&CastleGameEngine: https://decoherence.itch.io/ (Sources: https://gitlab.com/EugeneLoza)

#### Boleeman

• Hero Member
• Posts: 656
##### Re: Make a Dynamic Array of TEdit like a Tower
« Reply #2 on: January 13, 2024, 10:02:01 am »
I did not think of your approach but tried modifying the rows and columns of a 2D control array method and was getting frustrated. I will play around some more. I made a VB6 version of fixed rows but wanted to know how to do a variable row version using Lazarus.

ffred
Attached is the Vb6 version that I made. I wanted to do something similar in Lazarus and also in scratch. Made this years ago so don't worry about the copyright as it is freeware for all to enjoy.

« Last Edit: January 14, 2024, 11:39:48 am by Boleeman »

#### guachipelintree

• Newbie
• Posts: 5
##### Re: Make a Dynamic Array of TEdit like a Tower
« Reply #3 on: August 14, 2024, 11:51:12 am »
Happy to having found this.
I want to add this dynamic spinoff just if you are looking for something like this.

Also for complete noobs how to get this to work because noobs always ask me just that.
-Start a new Application.
-Object Inspector -> click on Form1 in the tree.
-select the second tab -> events.
-Click on OnCreate
-Click on the three dots. (A procedure is create)
-Copy the code below from Procedure to end;
-Select in the editor < procedure TForm1.FormCreate(Sender: TObject); to End;>
-paste
-run with F9

Code: Pascal  [Select][+][-]
1. procedure TForm1.FormCreate(Sender: TObject);
2.
3. var
4.   S:String;
5.   i,j: Integer;
6.   Edit: array [0..2] of Array[0..1] of TMaskEdit;
7.
8. begin
9.   Randomize;
10.   for i := 0 to 2 do
11.   Begin
12.     for j := 0 to 1 do
13.     begin
15.       Edit[i,j].OnChange := nil; //someting meaningful
16.       Edit[i,j].Parent := Self;
17.       Edit[i,j].Height := 30;
18.       Edit[i,j].Width := 50;
19.       Edit[i,j].Alignment := taCenter;
20.       Edit[i,j].Top := 50 + j * Edit[i,j].Height;
21.       Edit[i,j].Left :=  i * Edit[i,j].Width;
22.       Str((i+1)*(j+1),S);
23.       Edit[i,j].Text := S;
24.     end;
25.   end;
26. end;