C++Builder Tips


コントロールを動的に作成


コントロールを動的に作成するサンプルです。

「AddButton」というボタンをクリックするとボタンが動的に作成されます。

//---------------------------------------------------------------------------
// Unit1.h
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE 管理のコンポーネント
  TButton *AddButton;
  TButton *Button1;
  void __fastcall FormClose(TObject *Sender, TCloseAction &Action);
  void __fastcall AddButtonClick(TObject *Sender);
  void __fastcall Button1Click(TObject *Sender);
private:
  TList *lstButton; // ユーザー宣言
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)
{
  
lstButton = new TList;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
  
/* やらなくても可
  int i;
  TButton *button;

  for (i=0;i<lstButton->Count;i++)
  {
    button = (TButton*)lstButton->Items[i];
    delete button;
  }
  */


  
delete lstButton;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::AddButtonClick(TObject *Sender)
{
  TButton *button = new TButton(this);

  lstButton->Add(button);

  button->Parent = Form1;
  button->Caption = lstButton->Count;
  button->OnClick = Button1Click;
  button->Top = Button1->Top + Button1->Height * lstButton->Count;
  button->Left = Button1->Left;
  button->Height = Button1->Height;
  button->Width = Button1->Width;

}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
  TButton *button = dynamic_cast<TButton *>(Sender);
  int i = lstButton->IndexOf(button) + 1;

  AnsiString str1 = button->Caption + "のボタンが押下されました。";
  AnsiString str2 = str1 + "インデックスは" + i + "です。";

  Application->MessageBoxA(str2.c_str(),Form1->Caption.c_str(),MB_OK);
}
//---------------------------------------------------------------------------

DownLoad bcbtips027.lzh 4KB(BCB5)