11 Vote

Delphi/Lazarus: Create TabSheet on PageControl dynamically

Tutorial by Stefan Trost | Last update on 2023-01-21 | Created on 2013-10-29

In this tutorial, I want to show you how you can create any number of TabSheets on a TPageControl dynamically at runtime using Delphi or Lazarus.

We will see two different examples for that. In the first example, I show you how you can dynamically add individual TabSheets to a PageControl triggered by a button click of the user. In the second example, we take a look at how we can create a fixed number of TabSheets dynamically with creating the form.

Example 1: Create individual TabSheets dynamically at runtime

In the first example, we have a button. Every time when clicking on this button, a new TabSheet should be created and added to our PageControl.

procedure TForm1.Button1Click(Sender: TObject);
var 
  ATabNumber: integer;
  ANewTab: TTabSheet;
  ANewLabel: TLabel;
begin
  ATabNumber := PageControl1.PageCount + 1;

  ANewTab := TTabSheet.Create(PageControl1);
  ANewTab.PageControl := PageControl1;
  ANewTab.Name        := 'Tab' + IntToStr(ATabNumber);
  ANewTab.Caption     := 'Tab ' + IntToStr(ATabNumber);
   
  ANewLabel := TLabel.Create(ANewTab);
  ANewLabel.Name    := 'TabLabel' + IntToStr(ATabNumber);
  ANewLabel.Caption := 'Test';
  ANewLabel.Left    := 20;
  ANewLabel.Top     := 20;
  ANewLabel.Visible := true;
  ANewLabel.Parent  := ANewTab;
end; 

First of all, we are creating a new TabSheet in order to add it to our desired PageControl. In our example, this is PageControl1. Before creating the tabs, we are storing the number of tabs currently located on the PageControl into the variable k so that we can derive a unique name from this number. We are not allowed to give to tabs the same name, this would produce an error message.

Because an empty tab is nothing worth, we would like to add a TLabel to our created tab sheet directly after creating it as an example. Here, it is important to use our Tab as the parent of the new label so that the label will be placed on the tab sheet.

Example 2: Create fixed number of TabSheets when creating the form

In our second example, I would like to show you, how you are able to add a fixed number of TabSheets to a PageControl already at the time when creating the Form. We are using the OnCreate-Event of the form for this.

procedure TForm1.FormCreate(Sender: TObject);
var
  ANewTab: TTabSheet;
  ATabNumber: integer;
begin
  for ATabNumber := 1 to 10 do begin
    ANewTab := TTabSheet.Create(PageControl1);
    ANewTab.PageControl := PageControl1;
    ANewTab.name        := 'tab' + IntToStr(ATabNumber);
    ANewTab.Caption     := 'Tab ' + IntToStr(ATabNumber);
  end;
end;

With this, overall, 10 TabSheets will be created on our PageControl at once. Again, it is important to assign unique names and to add the tabs to the right PageControl. We are not creating Labels on the tabs here, because it is the same as in the example above.

ReplyPositiveNegative

About the Author

AvatarYou can find Software by Stefan Trost on sttmedia.com. Do you need an individual software solution according to your needs? - sttmedia.com/contact
Show Profile

 

Related Topics

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.