Ich habe mal was gebastelt wie ich es lösen würde. Voraussetzung sind ein Gridpanel und zwei Spinedits zum einstellen der Zeilen und Spalten dann kannst du probieren ob das das ist was du brauchst:
Delphi-Quellcode:
type
TForm2 = class(TForm)
gpTest: TGridPanel;
bTest: TButton;
seRows: TSpinEdit;
seCols: TSpinEdit;
procedure bTestClick(Sender: TObject);
private
procedure AddControlsToGridPanel(AGridPanel: TGridPanel; ARows, ACols: integer;
AControls: TArray<TControl>);
public
end;
var
Form2: TForm2;
implementation
{$R *.dfm}
procedure TForm2.AddControlsToGridPanel(AGridPanel: TGridPanel; ARows, ACols: integer;
AControls: TArray<TControl>);
var
i: integer;
begin
Assert(ARows * ACols = Length(AControls));
AGridPanel.ControlCollection.Clear;
with AGridPanel do
begin
ColumnCollection.BeginUpdate;
if ColumnCollection.Count <> ACols then
begin
ColumnCollection.Clear;
for i := 0 to ACols - 1 do
begin
ColumnCollection.Add;
ColumnCollection.Items[i].Value := 50;
end;
end;
ColumnCollection.EndUpdate;
RowCollection.BeginUpdate;
if RowCollection.Count <> ARows then
begin
RowCollection.Clear;
for i := 0 to ARows - 1 do
begin
RowCollection.Add;
RowCollection.Items[i].Value := 50;
end;
end;
RowCollection.EndUpdate;
end;
for i := Low(AControls) to High(AControls) do
begin
AControls[i].Parent := AGridPanel;
AControls[i].Align := alClient;
end;
end;
procedure TForm2.bTestClick(Sender: TObject);
var
vControls: TArray<TControl>;
vB: TButton;
i: integer;
begin
Setlength(vControls, seRows.Value * seCols.Value);
for i := 0 to seRows.Value * seCols.Value - 1 do
begin
vB := TButton.Create(gpTest);
vB.Caption := i.tostring();
vControls[i] := vB;
end;
AddControlsToGridPanel(gpTest, seCols.Value, seRows.Value, vControls);
end;