How to create a TCustomControl that behaves like a Tpanel?

How to create a TCustomControl that will behave like a Tpanel? e.g. MyCustomComponent, that I can remove components in the form of shortcuts, images, etc.

+1
source share
1 answer

The trick is part of the code in TCustomPanel:

constructor TCustomPanel.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  ControlStyle := [csAcceptsControls {, ... } ];
//...
end;

There are a few more VCL controls that you can go down with csAcceptsControlsin a property ControlStyle.

If you want to do this in your own controls, but do not get down from such a VCL control, then you should do something like this:

  • Override Create constructor
  • Add csAcceptsControlsto PropertyControlStyle

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

+7

All Articles