Search code examples
delphitpagecontrol

How to check if the tabSheet are already created in the TPageControl


I create the tabsheets dynamically in RunTime and placed a Frame inside it using this code:

  procedure TForm1.Button2Click(Sender: TObject);
 var
  TabSheetG: TTabSheet;
begin
  TabSheetG := TTabSheet.Create(PageControl1);
  TabSheetG.Caption := 'Tab Sheet green  ';
  TabSheetG.PageControl := PageControl1;
  Frame3 := TFrame3.Create(nil);
  Frame3.Parent := TabSheetG;
  Frame3.Show;
end;

and now i want to knew if the tab are already created and just make it activate it when i click the same button


Solution

  • Add a private variable of type TTabSheet to your class.

    type
      TForm1 = class(TForm)
      ....
      private
        FMyTabSheet: TTabSheet;
      end;
    

    It will automatically be initialized to nil.

    In the OnClick event handler, test whether the variable is nil. If not, create the tabsheet, otherwise, use the existing tabsheet.

    procedure TForm1.Button1Click(Sender: TObject);
    begin
      if not Assigned(FMyTabSheet) then
        FMyTabSheet := TTabSheet.Create(PageControl1);
        FMyTabSheet.PageControl := PageControl1;
        ... etc.
      end;
      PageControl1.ActivePage := FMyTabSheet;
    end;