Placing existing VCL controls on a TPanel component

458 Views Asked by At

As we all know, TPanel is a container for other visual components (TLabel, TEdit etc). However, it seems that we can place only new components (from the component palette) onto an existing TPanel and the VCL framework will then automatically make the TPanel their parent).

I wonder if there's a way to place existing visual components (those that are already on our VCL form) onto a TPanel. I've tried

  • Placing components on an existing TPanel (this doesn't work because the framework will not make the TPanel their parent)

  • Manually editing the form's dfm file (the components on the TPanel are now invisible -- a bug?)

  • Explicitly setting a component's Parent property in code (Label1 is visible even when Panel1 height is set to 0)

    Label1->Parent = Panel1;
    

Neither of the above worked but this seems to be such a basic thing that it has to be a way to do this.

Thank you

1

There are 1 best solutions below

1
Fabrizio On

Based on my experience with VCL (I use Delphi but I think the same is valid for C++), I know 3 ways to change the Parent of an already existing control at design time:

  • Drag and drop the Control into the desired Parent from the Structure window enter image description here

  • Select the Control, cut it Ctrl + X, select the desired Parent and paste Ctrl + V. In this way the Control will be pasted as child control of the desired Parent

  • Manually edit the DFM file. Doing this way, you have to pay attenction to the Left and Top properties, because the Control could be placed outside the new Parent and it could seems invisible. In this case I suggest to set the control's Left and Top to 0 in order to be sure the Control will be visible inside its new Parent