Reputation: 8043
I've defined a component derived from TCustomPanel
but sometimes I accidentally add some other controls inside of it.
Run-time:
TMyPanel = class(TCustomPanel)
public
//...
end;
Design-time:
procedure Register();
begin
RegisterCustomModule(TCustomPanel, TCustomModule);
RegisterComponents('MyTestComponents', [
TMyPanel
]);
end;
I don't want that who install and uses my component can accidentally add other controls inside of it. How to prevent controls are added to the component when it's direct child of a Form/Frame?
Steps to reproduce the behavior:
TMyPanel
TMyPanel
is selected, add another controlThe new control will be added inside the panel.
Upvotes: 3
Views: 312
Reputation: 6013
Probably the simplest way (which can be overridden) is to set the controlStyle element in its constructor, like this
interface
uses
VCL.ExtCtrls,
VCL.Controls,
System.Classes;
type
TMyPanel = class(TCustomPanel)
public
constructor Create(AOwner: TComponent); override;
end;
implementation
{ TMyPanel }
constructor TMyPanel.Create(AOwner: TComponent);
begin
inherited;
// ...
ControlStyle := ControlStyle - [ csAcceptsControls ];
end;
If you wanted to be able to change this behaviour at design time, you would also publish the ControlStyle property.
Upvotes: 4