LoginSignup
0
2

More than 5 years have passed since last update.

c++ builder XE4, 10.2 Tokyo > 動的生成 > TPanel内のTLabelを動的コピーする / TLabelかどうかを知るのはClassName()使用

Last updated at Posted at 2015-10-11
動作確認
C++ Builder XE4 on Windows 7pro
Rad Studio 10.2 Tokyo Update 2 (追記: 2017/12/27)

http://qiita.com/7of9/items/c0e4bcf9a3eae51a3525
において、TLabelの動的生成を行った。

今やりたいのは、TPanel内のすべてのTLabelを別のTPanelにコピーすること。

とりあえず位置、Font、Caption、Visibleをコピー。

code

static bool isTLabel(TControl *srcPtr) { return srcPtr->ClassName() == L"TLabel"; }

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    TLabel *work;
    TLabel *srcPtr;

        // copy all the TLabels from Panel1 to Panel2
    for(int idx=0; idx < Panel1->ControlCount; idx++) {
        if (isTLabel(Panel1->Controls[idx]) == false) {
            continue;
        }

        srcPtr = (TLabel *)Panel1->Controls[idx];

        work = new TLabel( this );
        work->Name = L"test" + IntToStr(idx);
        work->Parent = Panel2;
        work->Height = srcPtr->Height;
        work->Width = srcPtr->Width;
        work->Left = srcPtr->Left;
        work->Top = srcPtr->Top;
        work->Font = srcPtr->Font; /* Font */
        work->Caption = srcPtr->Caption;
        work->Visible = srcPtr->Visible;
    }
}

コンポーネントがTLabel型かどうかのチェックはClassName()で確認。
SOに質問したが、ヘルプで見つけた。
(http://qiita.com/7of9/items/e601b8fb10a2a40423e3)

400個のコピー時間

400個のコンポーネントを動的コピーするとどれくらい重いのかは気になる。

実際にやってみたところ、ButtonOnClick()での処理では600msecだった。
FormCreate()時の処理では1900msecかかる。

デバッグ

動的コピーしたものがどういう名前になったかのデバッグは以下で行う。

    dstPtr->Hint = dstPtr->Name;
    dstPtr->ShowHint = true;

ComponentCountが増えない

上記でコピーしたのはいいが、ComponentCountがコピー前と同じだった。
なんらかの追加処理が必要なのか?

作り直した処理にて以下のthisに相当するものが、別のコンポーネントを指してたのが原因だった。

        work = new TLabel( this );

関連 http://stackoverflow.com/questions/9918013/why-the-componentcount-property-of-my-tgroupbox-returns-0

0
2
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
2