Fabrizio
Fabrizio

Reputation: 8043

How to prevent controls are added into a component derived from TCustomPanel?

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:

  1. Create a new form
  2. Add a TMyPanel
  3. While the TMyPanel is selected, add another control

The new control will be added inside the panel.

Upvotes: 3

Views: 312

Answers (1)

Dsm
Dsm

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

Related Questions