Das ist richtig, ich schrieb aber auch gebastelt
Hier die verbesserte Variante:
Delphi-Quellcode:
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;
try
if ColumnCollection.Count <> ACols then
begin
ColumnCollection.Clear;
for i := 0 to ACols - 1 do
ColumnCollection.Add.Value := 50;
end;
finally
ColumnCollection.EndUpdate;
end;
RowCollection.BeginUpdate;
try
if RowCollection.Count <> ARows then
begin
RowCollection.Clear;
for i := 0 to ARows - 1 do
RowCollection.Add.Value := 50;
end;
finally
RowCollection.EndUpdate;
end;
end;
for i := Low(AControls) to High(AControls) do
begin
AControls[i].Parent := AGridPanel;
AControls[i].Align := alClient;
end;
end;