Forum > Games

Make a Dynamic Array of TEdit like a Tower

(1/1)

**Boleeman**:

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.

Thanks in advance.

**Eugene Loza**:

You can create elements dynamically. E.g. like this:

--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.FormCreate(Sender: TObject);const LinesCount = 6;var Line: Integer; Column: Integer; Edit: TEdit;begin Randomize; for Line := 0 to Pred(LinesCount) do for Column := 0 to Line do begin Edit := TEdit.Create(Self); Edit.OnChange := nil; //someting meaningful Edit.Parent := Self; Edit.Height := 30; Edit.Width := 50; Edit.Alignment := taCenter; Edit.Top := Line * Edit.Height; Edit.Left := (Pred(LinesCount) - Line) * Edit.Width div 2 + Column * Edit.Width; Edit.Text := Random(100).ToString; end;end;

**Boleeman**:

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.

Thankyou Eugene Loza for your quick reply and your solution.

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.

**guachipelintree**:

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 [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.FormCreate(Sender: TObject); var S:String; i,j: Integer; Edit: array [0..2] of Array[0..1] of TMaskEdit; begin Randomize; for i := 0 to 2 do Begin for j := 0 to 1 do begin Edit[i,j] := TMaskEdit.Create(Self); Edit[i,j].OnChange := nil; //someting meaningful Edit[i,j].Parent := Self; Edit[i,j].Height := 30; Edit[i,j].Width := 50; Edit[i,j].Alignment := taCenter; Edit[i,j].Top := 50 + j * Edit[i,j].Height; Edit[i,j].Left := i * Edit[i,j].Width; Str((i+1)*(j+1),S); Edit[i,j].Text := S; end; end;end;

Navigation

[0] Message Index