Recent

Author Topic: why i cant delete TTabSheet page  (Read 349 times)

fq19851220

  • Newbie
  • Posts: 3
why i cant delete TTabSheet page
« on: June 18, 2019, 04:53:08 pm »
 At the TPageControl i add a page  ,when i delete it a
 error occured  .
 Access violation
 Press OK to ignore and risk data corruption
 Press Abort to kill the program
 

Bart

  • Hero Member
  • *****
  • Posts: 3452
    • Bart en Mariska's Webstek
Re: why i cant delete TTabSheet page
« Reply #1 on: June 18, 2019, 05:01:29 pm »
Please show us the code.

Bart

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2186
    • havefunsoft.com
Patron Cocoa Widgetset development https://www.patreon.com/skalogryz

fq19851220

  • Newbie
  • Posts: 3
Re: why i cant delete TTabSheet page
« Reply #3 on: June 18, 2019, 06:09:11 pm »
Please show us the code.

Bart

is the lazarus IDE project  The  About Lazarus form. I add a page then can't delete it by click the right mouse button.
The Object Inspector->select TabSheet1:TTabSheet->right click ->delete page->error occured

fq19851220

  • Newbie
  • Posts: 3
Re: why i cant delete TTabSheet page
« Reply #4 on: June 18, 2019, 06:13:27 pm »
https://wiki.freepascal.org/TPageControl#Deleting_a_Page

thanks reply,Can I just delete the TTabsheet with the mouse without changing the code?
I tried but there is an error

jamie

  • Hero Member
  • *****
  • Posts: 1656
Re: why i cant delete TTabSheet page
« Reply #5 on: June 18, 2019, 11:47:45 pm »
If you are doing this  at run time you should be doing it with MouseUP not down..

You need to ensure the mouse input is complete before deleting a sheet at runtime.

Also, you should first select a different sheet into the page control before deleting the
current one.

lucamar

  • Hero Member
  • *****
  • Posts: 1814
Re: why i cant delete TTabSheet page
« Reply #6 on: June 18, 2019, 11:54:43 pm »
Can I just delete the TTabsheet with the mouse without changing the code?
I tried but there is an error

To close a tab by clicking the "close tab" button (the [X] in the tab) you just have to add a handler for the OnCloseTabClicked event of the page control. What I usually do is create a procedure that closes a TTabSheet [say, CloseTab(ATab; TTabSheet);] and call it from that handler with:
Code: [Select]
procedure TMainForm.CloseTabClicked(Sender: TObject);
begin
  CloseTab(PageControl.ActivePage);
end;

An example of such a CloseTab() from a demo project mine (a TUI text editor):
Code: Pascal  [Select]
  1. function TMainForm.CloseTab(ATab: TTabSheet): Boolean;
  2. var
  3.   Canceled: Boolean;
  4.   AMemo: TMemo;
  5.   AFile: String;
  6. begin
  7.   Canceled := False;
  8.   AMemo := MemoInTab(ATab); {Get the memo inside the tabsheet}
  9.   AFile := ATab.Caption;
  10.   if (Assigned(AMemo) and AMemo.Modified) then
  11.     {UderSaidYes is just a convenience function calling MessageDlg with "Yes, No, Cancel' buttons}
  12.     if UserSaidYes(Format(sSaveModified, [AFile]), Canceled) then
  13.       DoMemoSaveAs(AMemo);
  14.   if not Canceled then
  15.     ATab.Free;
  16.   Result := not Canceled;
  17. end;

As you can see, the bulk of the process is making sure that we can (and the user in fact wants to) close the tab. Really (and fully) closing it is just a matter of calling its Free method.

If you're getting an error then it's something else in your code; you'll have to show it to us to get any meaningfull diagnostic.
« Last Edit: June 18, 2019, 11:59:55 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 1.8.4 & 2.0.2 w/FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.