how do I create a TCustomControl that will behave like Tpanel? eg MyCustomComponent, that I can drop components in like labels, images etc.
Asked
Active
Viewed 1,729 times
1 Answers
8
The trick is this piece of code in TCustomPanel:
constructor TCustomPanel.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
ControlStyle := [csAcceptsControls {, ... } ];
//...
end;
There are many more VCL controls you can descend from that have csAcceptsControls in their ControlStyle property.
If you want to do this in your own controls, but do not descend from such a VCL control, then you should do something like this:
- Override the Create constructor
- Add
csAcceptsControlsto theControlStyleproperty
Like this sample code:
//MMWIN:MEMBERSCOPY
unit _MM_Copy_Buffer_;
interface
type
TMyCustomControl = class(TSomeControl)
public
constructor Create(AOwner: TComponent); override;
end;
implementation
{ TMyCustomControl }
constructor TMyCustomControl.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
ControlStyle := ControlStyle + [csAcceptsControls {, ...} ];
//...
end;
end.
--jeroen
Jeroen Wiert Pluimers
- 23,965
- 9
- 74
- 154