3

I have a descendant of TWinControl (in fact it is just that for now) and I registered it as a component in the IDE:

type
  TGroupPanel = class(TWinControl);

But when I drop other components on it, they attach to the form instead of to my control. In other words, I want my custom control to behave like a TPanel so that components dropped on it become its children.

If I create the components at runtime and assign them manually to my control, like in the code below, then it works:

  TForm1 = class(TForm)
    Group: TGroupPanel;
    procedure FormCreate(Sender: TObject);
  private
    Panel: TPanel;
  end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Panel := TPanel.Create(Group);
  Panel.Parent := Group;
  Panel.Align := alClient;
end;

So, what should I do to get components dropped on a TWinControl at design time become children of that control?

(What I am trying to do is to make a special control to group other components on, so I can align and position them together. Of course, I can do that with a normal panel, but I want to do this with a lightweight control that does not paint anything, and in TWinControl I found the solution.)

NGLN
  • 43,011
  • 8
  • 105
  • 200
Marus Gradinaru
  • 2,824
  • 1
  • 26
  • 55
  • 1
    See also [this almost identical question](http://stackoverflow.com/q/374451/757830). – NGLN Jul 04 '15 at 16:50

1 Answers1

6

Set csAcceptControls flag for ControlStyle.

constructor TGroupPanel.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  ControlStyle := ControlStyle + [csAcceptsControls];
end;
Dalija Prasnikar
  • 27,212
  • 44
  • 82
  • 159