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:
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):
function TMainForm.CloseTab(ATab: TTabSheet): Boolean;
var
Canceled: Boolean;
AMemo: TMemo;
AFile: String;
begin
Canceled := False;
AMemo := MemoInTab(ATab); {Get the memo inside the tabsheet}
AFile := ATab.Caption;
if (Assigned(AMemo) and AMemo.Modified) then
{UderSaidYes is just a convenience function calling MessageDlg with "Yes, No, Cancel' buttons}
if UserSaidYes(Format(sSaveModified, [AFile]), Canceled) then
DoMemoSaveAs(AMemo);
if not Canceled then
ATab.Free;
Result := not Canceled;
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.