Forum > Games

Make a Dynamic Array of TEdit like a Tower


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;    

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.

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.


[0] Message Index

Go to full version