I'm trying to create a control, which creates 3 standard TPanel onto itself in design-time and run-time. Everything is okay: control creates panels perfectly. But I struck with one problem: in design-time I want to have an ability to choose one of panels.
I wish to reproduce a standard behaviour of TPageControl: when user clicks on TabSheet on a screen, TabSheet becomes editable via object inspector.
Below attached a code of my control:
unit MyContainer;
interface
uses
Windows,
Messages,
SysUtils,
Classes,
Graphics,
Controls,
Forms,
StdCtrls,
ExtCtrls,
StrUtils,
Dialogs;
type
TMyContainer = class(TCustomControl)
private
FPanelA: TPanel;
FPanelB: TPanel;
FPanelC: TPanel;
protected
procedure Paint; override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
end;
procedure register;
implementation
{ TMyContainer }
procedure Register;
begin
RegisterComponents('MyComps', [TMyContainer]);
end;
constructor TMyContainer.Create(AOwner: TComponent);
begin
Inherited Create(AOwner);
Width := 200;
Height := 200;
ControlStyle := ControlStyle + [csAcceptsControls];
FPanelA := TPanel.Create(Self);
FPanelA.Parent := Self;
FPanelA.Width := 100;
FPanelA.Height := 60;
FPanelA.Left := 10;
FPanelA.Top := 10;
FPanelB := TPanel.Create(Self);
FPanelB.Parent := Self;
FPanelB.Width := 100;
FPanelB.Height := 60;
FPanelB.Left := 10;
FPanelB.Top := 80;
FPanelC := TPanel.Create(Self);
FPanelC.Parent := Self;
FPanelC.Width := 100;
FPanelC.Height := 60;
FPanelC.Left := 10;
FPanelC.Top := 160;
end;
destructor TMyContainer.Destroy;
begin
FreeAndNil(FPanelA);
FreeAndNil(FPanelB);
FreeAndNil(FPanelC);
Inherited Destroy;
end;
procedure TMyContainer.Paint;
begin
Canvas.Brush.Color := clBlue;
Canvas.FillRect(Canvas.ClipRect);
end;
end.
Is there someone who can show me a way to get a solution for my task?
Thanks in advance.
This can be implemented in many ways, depending on your specific wishes. Because your code does show nothing more than the creation of the panels, I assume you are in need of the very basics before you can even realize what your wishes exactly are, but then the basics may be a little hard for a novice component builder.
First of all: for something to be editable in the object inspector, it has to be a component or be (part of) a published property of a component. Right now your panels are just private fields. So you could try to publish your panels in properties. Or you could add one property for all panels which will be distinguished by a selected index property.
You could also mimic the page control component by adding panels as separate components. In that case you might need to add a component editor for a "New Page" command in its context menu.
Some notes: That control style setting is not needed, unless the component is ment to become parent of other controls by means of the designer. Also, your destructor is superfluous.
Then try asking a very specific component writing question.