Lazarus

Free Pascal => Beginners => Topic started by: BIT on September 19, 2021, 06:37:01 pm

Title: Pages Free
Post by: BIT on September 19, 2021, 06:37:01 pm
Again the problem (
ExtendedNotebook is the same PageControl, I'm trying to delete the dynamically created tab, in the code for creating the tab everything seems to be done correctly (I wrote the code many times and everything worked).
The index is correct, an error on ExtendedNotebook1.Pages .Free;

Code: Pascal  [Select][+][-]
  1. for i := 0 to ExtendedNotebook1.PageCount - 1 do
  2.     begin
  3.       if trim(ExtendedNotebook1.Pages[i].Hint) = trim(PIR.TreeViewSelectedPath) then
  4.  
  5.        ExtendedNotebook1.Pages[i].Free;
  6.  
  7.     end;
Title: Re: Pages Free
Post by: BIT on September 19, 2021, 07:51:46 pm
Did the search for the tab from the end, the error disappeared))
Code: Pascal  [Select][+][-]
  1. for i := ExtendedNotebook1.PageCount - 1  downto 0 do
Title: Re: Pages Free
Post by: Paolo on September 19, 2021, 09:59:10 pm
That seems reasonable. In the for-loop the pagecount is computed only once at the begin of loop, but when you delete some page the actual pagecount decreases, so when you reach some "i" value you are out of range of pages. Using downto avoid this.
Title: Re: Pages Free
Post by: BIT on September 20, 2021, 05:53:45 am
You always need to go backwards if you keep going after removing an item because the list is getting smaller..

That control should have a IndexOf feature so that you could just sit there in a WHILE  Loop and use the returned index of the given search item.
Thanks! and it’s strange that I didn’t use IndexOf) it’s quite suitable here!
Title: Re: Pages Free
Post by: BIT on September 20, 2021, 05:58:19 am
You always need to go backwards if you keep going after removing an item because the list is getting smaller..

That control should have a IndexOf feature so that you could just sit there in a WHILE  Loop and use the returned index of the given search item.
Although didn't find an IndexOf function for ExtendedNotebook1.Pages.Hint
TinyPortal © 2005-2018