Q&A

  • 페이지 컨트롤에 탭쉬트를 붙일려면?
win32밑에 있는 TPagecontrol에 동적으로 Tabsheet를 붙이려고 해봤는데 잘 안되는군요. 방법을 알고 싶습니다. 그리고 win3.1밑에 있는 TTabbedNoteBook의 탭의 캡션을 코드상에서 변경하는 법을 알고 싶습니다.

1  COMMENTS
  • Profile
    박성훈 1999.07.14 05:48
    박성훈 께서 말씀하시기를...

    > win32밑에 있는 TPagecontrol에 동적으로 Tabsheet를 붙이려고 해봤는데 잘 안되는군요. 방법을 알고 싶습니다. 그리고 win3.1밑에 있는 TTabbedNoteBook의 탭의 캡션을 코드상에서 변경하는 법을 알고 싶습니다.





    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..SOURCEVCL 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.