I'm trying to generate TLabels at runtime and insert them into a VertScrollBox with this code;
var
i, f: integer;
RT_Label: TLabel;
begin
f:= 10;
for i := 0 to 20 do
begin
RT_Label := TLabel.Create(Self);
RT_Label.Name := 'Label' + i.ToString;
RT_Label.Text := 'SampleLabel' + i.ToString;
RT_Label.Position.Y := f;
RT_Label.Align := TAlignLayout.Top;
RT_Label.Parent := VertScrollBox1;
inc(f, 15);
end;
end;
Labels are displayed without any problem, but when I try to free the generated labels with this code:
var
i: integer;
LComponent: TComponent;
begin
for i := 0 to ComponentCount-1 do
begin
if( Components[i] is TLabel )then
if StartsText('Label', (Components[i] as TLabel).Name) then
begin
LComponent := (Components[i] as TLabel);
If Assigned(LComponent) then FreeAndNil(LComponent);
end;
end;
end;
Then I always get the error 'Argument out of range'.
How do I properly remove TLabels added to the VertScrollBox in runtime?