I´m trying to create a Delphi non-visual component that can hold some visual components.
In design time I create a custom TPanel, so I can put my visual components in it and then I try to get this controls from the TPanel and store them in another component.
This is my custom panel
TDesignTimePanel = class(TPanel)
private
FPanel: TPanelDialogo;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure GetChildren(Proc: TGetChildProc; Root: TComponent); override;
function GetChildOwner: TComponent; override;
end
The method GetChildren does nothing, as I don't want to write this panel in a traditional way in the DFM file. The method GetChildOwner returns the TPanelDialogo where I want the visual controls to be stored.
And this is the component where I want to store the controls from the TDesignTimePanel
TPanelDialogo = class(TComponent)
private
FDesignPanel: TDesignTimePanel;
procedure VolcarFrameEnLista();
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
function CrearPanel(AOwner: TComponent): TPanel;
procedure GetChildren(Proc: TGetChildProc; Root: TComponent); override;
function GetChildOwner: TComponent; override;
end;
I create the custom panel this way
function TPanelDialogo.CrearPanel(AOwner: TComponent): TPanel;
var
i: integer;
Componente : TControl;
begin
if FDesignPanel = nil then
begin
FDesignPanel := TDesignTimePanel.Create(self);
FDesignPanel.AsociarPanel( self );
end;
FDesignPanel.Name := Name + '_frame';
FDesignPanel.Left := FX;
// some other config
FDesignPanel.Parent := Owner as TWinControl;
FDesignPanel.Show;
Result := FDesignPanel;
end;
So my GetChildren method does the following, where VolcarFrameEnLista is the method where I take the controls from the TDesignTimePanel object and stores them in the TPanelDialogo (FListaComponentes is a TComponentList)
procedure TPanelDialogo.GetChildren(Proc: TGetChildProc; Root: TComponent);
var
i: integer;
OwnedComponent: TComponent;
begin
if FDesignPanel <> nil then
begin
VolcarFrameEnLista();
if Root = Self then
for i := 0 to self.FListaComponentes.Count - 1 do
begin
OwnedComponent := FListaComponentes.Items[i];
Proc(OwnedComponent);
end;
end;
end;
procedure TPanelDialogo.VolcarFrameEnLista( );
var
i: integer;
Componente: TControl;
begin
for i := FDesignPanel.ControlCount - 1 downto 0 do
begin
Componente := FDesignPanel.Controls[i];
if Pos( self.Name + '_', Componente.Name ) = 0 then
begin
Componente.Name := self.Name + '_' + Componente.Name;
end;
Componente.Parent := nil;
if FListaComponentes.IndexOf(Componente) < 0 then
begin
FListaComponentes.Add( Componente );
end;
end;
end;
I want my DFM to have something like this:
object Form1: TForm1
object PanelDialogo1: TPanelDialogo
Left = 712
// ...
object PanelDialogo1_Label1: TLabel
Left = 88
// ..
end
object PanelDialogo1_Label2: TLabel
Left = 40
// ..
end
end
end
But I´m getting something like this
object Form1: TForm1
object PanelDialogo1: TPanelDialogo
Left = 712
// ...
end
object PanelDialogo1_Label1: TLabel
Left = 88
// ..
end
object PanelDialogo1_Label2: TLabel
Left = 40
// ..
end
end
What should I do so the TPanelDialogo takes "ownership" of the components drawn on the TDesignTimePanel.
InsertComponent
. Doesn't look quite right at first sight though... – Sertac AkyuzGet|SetParentComponent
to modify the behavior you describe. – Sertac Akyuz