RAD Studio 10.4.1 is now available! Learn more. Looking for discounts? Visit our Special Offers page!
Delphi

How to dynamically create a TPageControl

Author: Embarcadero USA

 Technical Information Database

TI1321D.txt - How to dynamically create a TPageControl

Category   :VCL
Platform   :All-32Bit
Product    :All32Bit,   

Description:
This document demonstrates how to dynamically add tab sheets 
(TTabSheet objects) to a Windows 9x/NT Page Control (TPageControl 
object).  Both of these objects are declared in the ComCtrls 
unit.  So make sure that ComCtrls is listed in the 'uses' clause 
on your form.


HOW TO DYNAMICALLY CREATE A PAGE CONTROL
----------------------------------------

Before we get into dynamically creating tab sheets, lets first 
discuss how to dynamically create a PageControl (if one isn't 
on the form already).  This is done by calling TPageControl's 
Create constructor with an owner parameter of Self.  The Create 
constructor returns a object reference of the newly created Page 
Control object and assigns it to the 'PageControl' variable.  
The second step is to set PageControl's Parent property to Self. 
The Parent property determines where the new PageControl is to be 
displayed; in this case its the form itself.  Here's a code snippet 
that demonstrates this.

var
  PageControl : TPageControl;

PageControl := TPageControl.Create(Self);
PageControl.Parent := Self;

Note: When the form gets destroyed the Page Control and it tab 
sheets will be destroyed also because they are owned by the form. 


HOW TO DYNAMICALLY CREATE A TAB SHEET
--------------------------------------

There are two basic steps to dynamically add a new page to a 
PageControl.  The first is to dynamically create the TTabSheet 
as follows:

var
  TabSheet : TTabSheet;

TabSheet := TTabSheet.Create(Self);

Then we need to give it a caption as follows:

TabSheet.Caption := 'Tabsheet 1';

And finally, the most important piece is to tell the new tab sheet 
which Page Control it belongs to.  This is done by assigning the 
TTabSheet's PageControl property a TPageControl reference variable 
like the one created above (PageControl). Here's a code snippet 
that demonstrates this.

TabSheet.PageControl := PageControl;


HOW TO DYNAMICALLY ADD A CONTROL TO A TAB SHEET
-----------------------------------------------

The key to creating and placing any control on a tab sheet is to 
assign it's Parent property a TTabSheet class reference variable.  
Here is an example.

var
  Button : TButton;

Button := TButton.Create(Self);
Button.Caption := 'Button 1';
Button.Parent := TabSheet;

For more information on the TPageControl and TTabSheet objects 
refer to the on-line documentation as well as look at the 
ComCtrls.pas file in your ..\Delphi..\SOURCE\VCL directory. 

FULL SOURCE EXAMPLE
-------------------

// This code is extracted from a form with a single button on it.

unit DynamicTabSheetsUnit;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, 
  Forms, Dialogs, StdCtrls, Buttons;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure TestMethod(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses ComCtrls;

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var
  PageControl : TPageControl;
  TabSheet : TTabSheet;
begin
  // Create the PageControl
  PageControl := TPageControl.Create(Self);
  PageControl.Parent := Self;

  // Create 1st page and associate it with the PageControl
  TabSheet := TTabSheet.Create(Self);
  TabSheet.Caption := 'Tabsheet 1';
  TabSheet.PageControl := PageControl;

  // Create the first page

  with TButton.Create(Self) do
  begin
    Caption := 'Button 1';
    OnClick := TestMethod;  // Assign an event handle
    Parent := TabSheet;
  end;

  // Create 2nd page and associate it with the PageControl

  TabSheet := TTabSheet.Create(Self);
  TabSheet.Caption := ' Tabsheet 2';
  TabSheet.PageControl := PageControl;
end;

procedure TForm1.TestMethod(Sender: TObject);
begin
  ShowMessage('Hello');
end;

end.


Reference:
 

3/26/99 11:29:14 AM
 

Article originally contributed by Borland Staff


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial   Upgrade Today

   Free Delphi Community Edition   Free C++Builder Community Edition

Related posts
C++News

Using C++17 Algorithms Library Parallel Sorting with C++Builder 10.4 Sydney for Win32 and Win64

C++

What's New in the GetIt Package Manager - June 2020

C++

RAD Studio 10.4 Now Available, Learn More

C++NewsRAD Studio

Open for Business with 10.4! - May 2020 GM Update

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

IN THE ARTICLES