Recent

Author Topic: Help with a TPages and TATTabs problem  (Read 396 times)

zxandris

  • Full Member
  • ***
  • Posts: 170
Help with a TPages and TATTabs problem
« on: December 01, 2025, 06:13:17 pm »
Okay so I'm using a TNotebook and TATTab to essentially do an MDI.  Now for the most part that seems okay but I'm trying to close the tabs and the TPage that is essentially associated.  I'm at the moment trying to close all for a desktop routine, it obviously wants to clear the opened files when loading.  I'm getting errors all over the place here, access violations with these two procedures

Code: Pascal  [Select][+][-]
  1. procedure CloseAllTabs_ATTab(ATTab: TATTabs);
  2. var
  3.   i: Integer;
  4. begin
  5.   // remove from last to first to avoid index shifting
  6.   for i := ATTab.Tabs.Count - 1 downto 0 do
  7.   begin
  8.     // replace with the actual API your TATTab exposes:
  9.     // e.g. ATTab.Tabs.Delete(i); or ATTab.CloseTab(i); or ATTab.Pages[i].Free;
  10.     try
  11.        ATTab.Tabs.Items[i].Free;
  12.     except
  13.  
  14.     end;
  15.     try
  16.        if assigned(ATTab.Tabs.Items[i]) then ATTab.Tabs.Delete(i);
  17.     except
  18.           //
  19.     end;
  20.   end;
  21.   application.ProcessMessages;
  22. end;
  23.  

Code: Pascal  [Select][+][-]
  1. procedure CloseAllNotebookPages(ANotebook: TNotebook);
  2. var
  3.   i : Integer;
  4.   P : TWinControl;
  5. begin
  6.   if ANotebook = nil then Exit;
  7.  
  8.   // Prevent flicker and re-entrancy
  9.   ANotebook.DisableAlign;
  10.   try
  11.     // If Pages supports BeginUpdate/EndUpdate (TStrings), use them
  12.     if Assigned(ANotebook.Pages) then ANotebook.Pages.BeginUpdate;
  13.     try
  14.           // iterate backwards to avoid index shifting
  15.           for i := ANotebook.PageCount - 1 downto 0 do
  16.           begin
  17.             P := ANotebook.Page[i]; // TPage is a TWinControl descendant
  18.             p.hint := '';
  19.             // Free child controls on the page to avoid leaks and dangling events
  20.             // iterate while there are controls because Controls[] shifts when freed
  21.             while P.ControlCount > 0 do
  22.             begin
  23.               // Optionally remove event handlers here if needed
  24.               P.Controls[0].Parent := nil;
  25.               P.Controls[0].Free;
  26.             end;
  27.             try
  28.                if assigned(p) then ANotebook.Pages.Delete(i);
  29.             except
  30.  
  31.             end;
  32.             try
  33.               FreeAndNil(p);
  34.             except
  35.                   //
  36.             end;
  37.  
  38.           end;
  39.         finally
  40.                try
  41.                   if Assigned(ANotebook.Pages) then ANotebook.Pages.EndUpdate;
  42.                except
  43.                      //
  44.                end;
  45.         end;
  46.   finally
  47.          ANotebook.EnableAlign;
  48.   end;
  49. end;
  50.  

I'm using this :-
Code: Pascal  [Select][+][-]
  1. procedure TfrmMain.CloseAllTabs;
  2. begin
  3.      if FLoading then exit;
  4.      if tabMDI.tabCount-1=-1 then exit; // Nothing to close so exit clean
  5.      try
  6.         CloseAllNotebookPages(nbMDI);
  7.      except
  8.            //
  9.      end;
  10.      application.ProcessMessages;
  11.      sleep(15);
  12.      try
  13.         CloseAllTabs_ATTab(tabMDI);
  14.      except
  15.            //
  16.      end;
  17. end;
  18.  

to actually do the closing.  Right now it does close the windows (with an access violation) but it doesn't appear to actually be freeing both the tabs and the TPage.  I've messed around as best I can but it still doesn't work when then obviously I want to open the new desktop of filenames.  I *think* because it thinks the tabs and pages are still there somehow.

If anyone can help me with this migraine inducing issue I would be very happy :).  I'm using Lazarus 4.2 and I'm using the ATFlat controls package from the online package manager that to my knowledge is up-to-date.

Please, this will really help me, thanks

CJ

AlexTP

  • Hero Member
  • *****
  • Posts: 2669
    • UVviewsoft
Re: Help with a TPages and TATTabs problem
« Reply #1 on: December 01, 2025, 07:33:28 pm »
You mix 2 questions:

1. How to correctly delete the tab from TATTabs object? Answer: this method

Code: Pascal  [Select][+][-]
  1. function TATTabs.DeleteTab(AIndex: integer;
  2.   AAllowEvent, AWithCancelBtn: boolean;
  3.   AAction: TATTabActionOnClose=aocDefault;
  4.   AReason: TATTabDeletionReason=adrNone): boolean;

2. How to delete also the corresponding 'page' from TATPages? Usually code knows NTabIndex, it does this

Code: Pascal  [Select][+][-]
  1. var
  2.   Data: TATTabData;
  3. begin
  4.   Data:= ATTabs1.GetTabData(NTabIndex);
  5.   if Assigned(Data) and Assigned(Data.TabObject) then
  6.     // cast Data.TabObject to some proper type of 'page' which your code saved into Data.TabObject
  7.     // and free this 'page'
  8.  

This is answer w/o using TATPages! Only using TATTabs.

zxandris

  • Full Member
  • ***
  • Posts: 170
Re: Help with a TPages and TATTabs problem
« Reply #2 on: December 06, 2025, 11:54:13 am »
Should I be using ATPages.  I'm trying to create an MDI tabbed application.  The example was using forms and I must admit i'm using frames.  Should I not be?

Please, any help would be brilliant


You mix 2 questions:

1. How to correctly delete the tab from TATTabs object? Answer: this method

Code: Pascal  [Select][+][-]
  1. function TATTabs.DeleteTab(AIndex: integer;
  2.   AAllowEvent, AWithCancelBtn: boolean;
  3.   AAction: TATTabActionOnClose=aocDefault;
  4.   AReason: TATTabDeletionReason=adrNone): boolean;

2. How to delete also the corresponding 'page' from TATPages? Usually code knows NTabIndex, it does this

Code: Pascal  [Select][+][-]
  1. var
  2.   Data: TATTabData;
  3. begin
  4.   Data:= ATTabs1.GetTabData(NTabIndex);
  5.   if Assigned(Data) and Assigned(Data.TabObject) then
  6.     // cast Data.TabObject to some proper type of 'page' which your code saved into Data.TabObject
  7.     // and free this 'page'
  8.  

This is answer w/o using TATPages! Only using TATTabs.

AlexTP

  • Hero Member
  • *****
  • Posts: 2669
    • UVviewsoft
Re: Help with a TPages and TATTabs problem
« Reply #3 on: December 06, 2025, 12:01:24 pm »
Seems you don't need ATPages, because ATPages is just a helping thing for the ATGroups component. If you don't use ATGroups then don't use ATPages.

 

TinyPortal © 2005-2018