C++ Builder XE4, 10.2 Tokyo > TPageControl > TButtonでのページの追加、削除


動作環境
C++ Builder XE4
RAD Studio 10.2 Tokyo Update 2 (追記: 2018/01/05)

TPageControlのページを動的に変更してみる。

参考: https://stackoverflow.com/questions/21970127/delete-tabs-from-a-page-control-at-run-time

Unit1.h
//---------------------------------------------------------------------------

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <System.Classes.hpp>
#include <Vcl.Controls.hpp>
#include <Vcl.StdCtrls.hpp>
#include <Vcl.Forms.hpp>
#include <Vcl.ComCtrls.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:    // IDE で管理されるコンポーネント
    TPageControl *PageControl1;
    TButton *B_add;
    TButton *B_delete;
    void __fastcall B_addClick(TObject *Sender);
    void __fastcall B_deleteClick(TObject *Sender);
private:    // ユーザー宣言
public:     // ユーザー宣言
    __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
Unit1.cpp
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::B_addClick(TObject *Sender)
{
    int count = PageControl1->PageCount;

    TTabSheet *pPage = new TTabSheet(PageControl1);
    pPage->PageControl = PageControl1;
    pPage->Caption = L"Page" + IntToStr(count);
    pPage->Name = L"ts" + IntToStr(count);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::B_deleteClick(TObject *Sender)
{
    int count = PageControl1->PageCount;

    if (count == 0) {
        return;
    }

    delete PageControl1->Pages[count - 1];
}
//---------------------------------------------------------------------------