0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

C++ Builder XE4, 10.2 Tokyo > TPageControl + TFrame > TButtonでページ(TFrame)の追加

Last updated at Posted at 2017-11-01
動作環境
C++ Builder XE4
RAD Studio 10.2 Tokyo Update 2 (追記: 2018/01/05)

TPageControlにTFrameのページを追加したい。

構成

  • Unit1(TForm)
    • TPageControlを持つ
  • Unit2(TFrame)
    • TLabelを持つ
    • TTimerを持つ

Unit2のデザインは以下。
qiita.png

参考

papyさんによる言語掲示板
http://www.papy.in/bbs/delphi/200906/09060057.html

情報感謝です。

コード

Unit2.h
//---------------------------------------------------------------------------

# ifndef Unit2H
# define Unit2H
//---------------------------------------------------------------------------
# include <System.Classes.hpp>
# include <Vcl.Controls.hpp>
# include <Vcl.StdCtrls.hpp>
# include <Vcl.Forms.hpp>
# include <Vcl.ExtCtrls.hpp>
//---------------------------------------------------------------------------
class TFrame2 : public TFrame
{
__published:	// IDE で管理されるコンポーネント
	TLabel *Label1;
	TTimer *Timer1;
	void __fastcall Timer1Timer(TObject *Sender);
private:	// ユーザー宣言
public:		// ユーザー宣言
	__fastcall TFrame2(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TFrame2 *Frame2;
//---------------------------------------------------------------------------
# endif
Unit2.cpp
//---------------------------------------------------------------------------

# include <vcl.h>
# pragma hdrstop

# include "Unit2.h"
//---------------------------------------------------------------------------
# pragma package(smart_init)
# pragma resource "*.dfm"
TFrame2 *Frame2;
//---------------------------------------------------------------------------
__fastcall TFrame2::TFrame2(TComponent* Owner)
	: TFrame(Owner)
{
	Timer1->Enabled = false;
	Timer1->Interval = 5000; // msec
	Timer1->Enabled = true;
}
//---------------------------------------------------------------------------
void __fastcall TFrame2::Timer1Timer(TObject *Sender)
{
	String msg = Now().FormatString(L"yyyy/mm/dd hh:nn:ss");
	OutputDebugString(msg.c_str());
}
//---------------------------------------------------------------------------
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"
# include "Unit2.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);

	TFrame2 *pFrame = new TFrame2(pPage);
	pFrame->Parent = pPage;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::B_deleteClick(TObject *Sender)
{
	int count = PageControl1->PageCount;

	if (count == 0) {
        return;
	}

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

実行

ページを増やすとデバッグ出力が増える。

デバッグ出力: 2017/11/01 19:00:18 プロセス Project1.exe (2240)
デバッグ出力: 2017/11/01 19:00:19 プロセス Project1.exe (2240)
デバッグ出力: 2017/11/01 19:00:20 プロセス Project1.exe (2240)
デバッグ出力: 2017/11/01 19:00:21 プロセス Project1.exe (2240)
デバッグ出力: 2017/11/01 19:00:21 プロセス Project1.exe (2240)
デバッグ出力: 2017/11/01 19:00:22 プロセス Project1.exe (2240)
デバッグ出力: 2017/11/01 19:00:22 プロセス Project1.exe (2240)
デバッグ出力: 2017/11/01 19:00:23 プロセス Project1.exe (2240)
デバッグ出力: 2017/11/01 19:00:23 プロセス Project1.exe (2240)
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?