Two days ago I gave an accepted answer for this question (that is what bothering me the most):
newtabsheet:=ttabsheet.Create(PageControl1);
NewTabSheet.PageControl := PageControl1;
newtabsheet.Caption:='tab1';
i1:=tabs.Count;
tabs.Add(newtabsheet);
I have analysed this code the best I can, and these are my results.
Line 1: I create a ttabsheet
with pagecontrol1
as the parent/owner (based on the constructor below).
constructor TTabSheet.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
Align := alClient;
ControlStyle := ControlStyle + [csAcceptsControls, csNoDesignVisible,
csParentBackground, csPannable];
Visible := False;
FTabVisible := True;
FHighlighted := False;
end;
Then I stored a reference to it in the variable Newtabsheet
(this remarque is based on an answer @David Heffernan gave to one of my questions).
Line 2: I used the reference to assign pagecontrol1
to the property .pagecontrol
.
And this is the code of the setting method:
procedure TTabSheet.SetPageControl(APageControl: TPageControl);
begin
if FPageControl <> APageControl then
begin
if FPageControl <> nil then
FPageControl.RemovePage(Self);
Parent := APageControl;
if APageControl <> nil then
APageControl.InsertPage(Self);
end;
end;
Based on this I think (maybe wrong) it is comparing the existing parent to the new passed parameter, if there is a different then if the parent<>nil
removes this newtabsheet
from the previous pagecontrol
(visual affects) and assign it to the new parent/owner, then if the new parent<>nil
insert it in the new pagecontrol
(visual affects).
So this line is unnecessary (maybe wrong), because I already did that in the first line.
Line 3: I use the reference to assign the caption of the tabsheet
with 'tab1'.
Line 5: I use the add method to store a reference to the tabsheet
in the tabs list tabs:TList<ttabsheet>;
a generic.
Here is where things start to fly over my head.
I think that the parent/owner is tabs
, but the pagecontrol1
is still showing the tab (based on the accepted answer to this question changing the parent visually removes the tabsheet
from pagecontrol1
), but it does not.
Now this could be wrong, but again if it is just a reference then why when I delete the tabsheet
from pagecontrol1
by doing PageControl1.ActivePage.free
the tabs.count
remains constant.
And if I delete the tabsheet
from tabs then the tabsheet
on the pagecontrol1
is not deleted (removed visually).
From this question I understood that generics becomes the parent/owner and that you do not need to worry about freeing tabsheet
from pagecontrol1
, because tabs is the parent and you only need to free it from tabs
.
My question: What is happening in this code and why I'm facing this behavior?
Clarification
What is driving the question is when I delete the ttabsheet in the pagecontrol why it is not raising an error when I try to use the reference to it in the list are they two different objects.