according to the previous question TABS @ RUN TIME I create tabsheets at run time. Now I suffer the problem deleting tabs at run time, my solution for the inverse function goes like this
procedure TForm.DeleteAllTabs(sender : TObject);
var i : Integer;
begin
for I := 0 to pagecontrol1.PageCount-1 do
pagecontrol1.Pages[i].Destroy
end;
but it claims that the i is out of bound ..... (access violation)
When 'i' is initially '0' you're deleting Pages[0] and the remaining sheets are moved one position down. That is after the deletion, the first sheet is still at Pages[0]. But in the next iteration you're deleting Pages[1] skipping the first page. When eventually you try to delete a non-existing page, you get the error.
Step by step, let's say at first you have three sheets,
[page0] [page1] [page2]
The index counter is '0', you delete Pages[0], the remaining two sheets are moved to the start,
[page0] [page1]
The index counter is '1', you delete the second page, that's Pages[1]. There's only one remaining page,
[page0]
The index counter is '2', you delete the third page, that's Pages[2]. There's no Pages[2], you get error.
One solution is to always delete the first page:
for I := 0 to pagecontrol1.PageCount-1 do
pagecontrol1.Pages[0].Free;
Another one can be starting to delete from the last page as stated in TLama's comment.
for I := pagecontrol1.PageCount-1 downto 0 do
pagecontrol1.Pages[i].Free;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With