Let components dropped on my control in the IDE become children of my control

孤街醉人 提交于 2019-11-27 07:12:07

问题


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.)


回答1:


Set csAcceptControls flag for ControlStyle.

constructor TGroupPanel.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  ControlStyle := ControlStyle + [csAcceptsControls];
end;


来源:https://stackoverflow.com/questions/31220255/let-components-dropped-on-my-control-in-the-ide-become-children-of-my-control

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!