0
My Environment: C++ Builder XE4

how to copy all the TLabels parented with a TPanel on delphi to another TPanel?

I would like to implement above code in C++ Builder.

I do not know how to implement below in C++ Builder.

if ParentControl.Controls[i] is TLabel then

Are there any functions to get type as TLabel or some other?

Community
  • 1
  • 1
sevenOfNine
  • 1,509
  • 16
  • 37

3 Answers3

2

You can use ClassType method as:

if(Controls[i]->ClassType() == __classid(TLabel))
{
    ...
}
mh taqia
  • 3,506
  • 1
  • 24
  • 35
1

Use dynamic_cast:

if (dynamic_cast<TLabel*>(ParentControl->Controls[i]) != NULL)

Here is a translation of that code:

void __fastcall CopyLabels(TWinControl *ParentControl, TWinControl *DestControl)
{
   for(int i = 0; i < ParentControl->ControlCount; ++i)
   {
       if (dynamic_cast<TLabel*>(ParentControl->Controls[i]) != NULL)
       {
           TLabel *ALabel = new TLabel(DestControl);
           ALabel->Parent = DestControl;
           ALabel->Left   = ParentControl->Controls[i]->Left;
           ALabel->Top    = ParentControl->Controls[i]->Top;
           ALabel->Width  = ParentControl->Controls[i]->Width;
           ALabel->Height = ParentControl->Controls[i]->Height;
           ALabel->Caption= static_cast<TLabel*>(ParentControl->Controls[i])->Caption;
           //you can add manually more properties here like font or another 
        }
    }
}

With that said, this would be slightly more efficient:

void __fastcall CopyLabels(TWinControl *ParentControl, TWinControl *DestControl)
{
   int count = ParentControl->ControlCount;
   for(int i = 0; i < count; ++i)
   {
       TLabel *SourceLabel = dynamic_cast<TLabel*>(ParentControl->Controls[i]);
       if (SourceLabel != NULL)
       {
           TLabel *ALabel = new TLabel(DestControl);
           ALabel->Parent = DestControl;
           ALabel->SetBounds(SourceLabel->Left, SourceLabel->Top, SourceLabel->Width, SourceLabel->Height);
           ALabel->Caption = SourceLabel->Caption;
           //you can add manually more properties here like font or another 
        }
    }
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • I will use dynamic cast as you teach. It's very difficult to find this kind of thing on the help of C++ Builder and on the Internet. I appreciate your help. – sevenOfNine Oct 11 '15 at 23:28
  • I didn't know the SetBounds() function to set the left, top, etc. Thank you very much for this also. – sevenOfNine Oct 11 '15 at 23:29
0

I found ClassName() method.

Following seems working.

static bool isTCheckBox(TControl *srcPtr)
{
    if (srcPtr->ClassName() == L"TCheckBox") {
        return true;
    }
    return false;
}
sevenOfNine
  • 1,509
  • 16
  • 37
  • 1
    That is not the equivilent of the original Delphi code. The C++ equivilent of Delphi's `is` operator is `dynamic_cast` instead. – Remy Lebeau Oct 11 '15 at 17:25
  • 1
    ClassType or dynamic_cast seem "safer" than checking a text string – M.M Oct 13 '15 at 09:49