Keep a TFrame
variable so you know what the currently displayed frame is. When you display a different frame, you can free/cache the current frame first.
If you want to cache frames, then keep five separate TFrame
variables, one for each page type. When you need to show a page, create it only if you have not already done so:
private
CurrentFrame: TFrame;
Frames: array[0..4] of TFrame;
procedure TMyForm.TreeView1Change(Sender: TObject);
begin
if CurrentFrame <> nil then
begin
CurrentFrame.Parent := nil;
CurrentFrame := nil;
end;
if Frames[OneOfTheIndexes] = nil then
Frames[OneOfTheIndexes] := TAppropriateFrameType.Create(Self);
Frames[OneOfTheIndexes].Parent := Panel1;
CurrentFrame := Frames[OneOfTheIndexes];
end;
If you want to recreate frames instead, then simply remove the array and Free()
the current frame before displaying a new one:
private
CurrentFrame: TFrame;
procedure TMyForm.TreeView1Change(Sender: TObject);
begin
if CurrentFrame <> nil then
FreeAndNil(CurrentFrame);
CurrentFrame := TAppropriateFrameType.Create(Self);
CurrentFrame.Parent := Panel1;
end;