Forum > General
Destroy an embedded form in a panel
(1/1)
timmermanj:
I made an application with 2 panels & 3 buttons.
The 3 buttons are located on "panel1"
"Button 1": Embeds a "form1" into "panel 2" (so panel 2 is parent & form 1 is the child of panel 2)
"Button 2": Embeds "form 2" into "panel 2"
"Button 3": Embeds "form 3" into "panel 2"
Embedding these forms is no problem.
But, is there a way to destroy embedded child forms in panels without calling them by name?
Something like: destroy the child form (however this form is called) of panel 2?
Thanks!
Jeffrey
timmermanj:
I tried to do something like this:
for i := frmMain.ComponentCount - 1 downto 0 do
begin
if frmMain.Components is TMyClass then
frmMain.Components.Free;
end;
But it doesn't work...
Anybody any suggestions?
Thanks!
Leledumbo:
If you set the panel as the owner (not parent) of the form, then the panel should free the form automatically when it's freed.
Marc:
--- Quote from: timmermanj on December 01, 2010, 01:35:32 pm ---I tried to do something like this:
for i := frmMain.ComponentCount - 1 downto 0 do
begin
if frmMain.Components is TMyClass then
frmMain.Components.Free;
end;
But it doesn't work...
Anybody any suggestions?
Thanks!
--- End quote ---
frmMain.Components will only contain components which have frmMain as Owner. If you do so (as side effect) they will automagically get destroyed when frmMain is destroyed.
You still are allowed to destroy your component manually.
If you dont want to set the owner or if you have a different owner, then when frmMain is set as parent, use frmMain.Controls
arbelest:
--- Quote from: timmermanj on December 01, 2010, 01:35:32 pm ---I tried to do something like this:
for i := frmMain.ComponentCount - 1 downto 0 do
begin
if frmMain.Components is TMyClass then
frmMain.Components.Free;
end;
Thanks!
--- End quote ---
I assume that there is no other control on panel2. You can modify your code like this
--- Code: ---procedure TFrmMain.Button1Click(Sender: TObject);
var
lFrmClass : TFormClass = nil;
begin
if Panel2.ControlCount>0 then
Panel2.Controls[0].Free;
if (Sender=Button1) then
lFrmClass :=TForm1
else if (Sender=Button2) then
lFrmClass :=TForm2;
else if (Sender=Button3) then
lFrmClass :=TForm3;
if lFrmClass<>nil then
with lFrmClass.Create(Self) do
begin
Parent :=Panel2;
Align :=alClient;
Show;
end;
end;
--- End code ---
Note than button2 and button3 event onClick linked to button1.OnClick
Navigation
[0] Message Index