Skip to content

FIX #40019: LCL/TPageControl: FIX algorithm of clear tabs in TNBPages.Clear()

rich2014 requested to merge richware/lazarus:lcl/notebook into main

FIX #40019 (closed) LCL/TPageControl: tabs are activated unnecessarily in TNBPages.Clear()

when clear tabs in TPageControl(TCustomTabControl), a large number of Tabs that will be removed are activated unnecessarily.
this is because the algorithm in TNBPages.Clear() will cause the next Tab to be activated time by time after removing the currently active Tab.
moreover, If the Form calls TPageControl.Clear() in Destroy(), an exception may be thrown because the related control may have been destroyed.

it was fixed by fine-tuning the algorithm in Clear(). remove the Tab in the same reverse order, but skip the activated tab, and remove the activated tab at the end, to avoid activating other tabs in Clear().

tested on MacOS 12 and Windows 11.

the new algorithm will not activate even one Tab.

procedure TNBPages.Clear;
var
  i: Integer;
begin
  // remove the pages in reverse order but skip the Active Page,
  // and remove the Active Page at the end,
  // to avoid activating other Pages.
  for i:=FNoteBook.PageCount-1 downto 0 do
    if i<>FNoteBook.PageIndex then Delete(i);
  if FNoteBook.PageCount>0 then Delete(0);
end;
Edited by rich2014

Merge request reports