Recent

Author Topic: Destroy an embedded form in a panel  (Read 7177 times)

timmermanj

  • New Member
  • *
  • Posts: 39
Destroy an embedded form in a panel
« on: November 30, 2010, 01:57:42 pm »
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

  • New Member
  • *
  • Posts: 39
Re: Destroy an embedded form in a panel
« Reply #1 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!

Leledumbo

  • Hero Member
  • *****
  • Posts: 8836
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Destroy an embedded form in a panel
« Reply #2 on: December 01, 2010, 02:07:41 pm »
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

  • Administrator
  • Hero Member
  • *
  • Posts: 2680
Re: Destroy an embedded form in a panel
« Reply #3 on: December 23, 2010, 06:50:01 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!

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
//--
{$I stdsig.inc}
//-I still can't read someones mind
//-Bugs reported here will be forgotten. Use the bug tracker

arbelest

  • New Member
  • *
  • Posts: 15
Re: Destroy an embedded form in a panel
« Reply #4 on: December 29, 2010, 04:04:40 am »
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!

I assume that there is no other control on panel2. You can modify your code like this
Code: [Select]
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;


Note than button2 and button3 event onClick linked to button1.OnClick

 

TinyPortal © 2005-2018