2

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

Fabrizio
  • 7,603
  • 6
  • 44
  • 104
dsp_user
  • 2,061
  • 2
  • 16
  • 23
  • Tried to move the controls in the tree of the structure view? – Kerem Aug 17 '17 at 06:16
  • I've tried to edit the dfm manually so that the controls are children of the TPanel in question. Didn't work. The controls are now completely invisible. – dsp_user Aug 17 '17 at 06:44
  • What happens when you try to move the controls in the tree of the structure view? You should also look at the `Left` and `Top` properties. – Kerem Aug 17 '17 at 07:03
  • What exactly do you mean by 'move the controls in the tree of structure view'? I think I can only move the controls manually at design-time or by editing the dfm file. Anyway, if nothing else works, I'll simply add new controls to my TPanel and that'll work but this is slightly more work. – dsp_user Aug 17 '17 at 07:21
  • The IDE has a view called "Structure" wich shows the structure of the form at design time. You can open it via the main menu "View". There you can drag and drop items to change their parent. – Kerem Aug 17 '17 at 11:45
  • OK, will give it a try. – dsp_user Aug 17 '17 at 11:49
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/152150/discussion-between-kerem-d-and-dsp-user). – Kerem Aug 17 '17 at 11:54

1 Answers1

2

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

Fabrizio
  • 7,603
  • 6
  • 44
  • 104
  • 1
    Yes, the second method works as already suggested by Kerem D (thanks Kerem). I wasn't able to make the third method work even when Left and Top look OK (they seem to be within the bounds of its parent control). I will still upvote your answer. – dsp_user Aug 19 '17 at 20:38