Board index » delphi » Dynamically Creating/Destroy TFrames

Dynamically Creating/Destroy TFrames

I have a project using many frames within a base frame.  At design time I place each frame within the base frame, and at runtime I display each frame by setting the active frame's visible property to True and all others to False.  The active frame is determined by whichever button the user clicks on my main form.  My problem is that by using this technique all my frames stay in memory (once they are shown) and take up valuable system resources.  Currently using 20% of total resources -- way too much.  Does anyone know how to dynamically create and destroy frames like you do forms.  I want to do something similar to the following (but with frames):

  Application.CreateForm(TfrmBuydown, frmBuydown);
  frmBuydown.ShowModal;
  frmBuydown.Release;

Thanks, Tom

 

Re:Dynamically Creating/Destroy TFrames


Quote
In article <3963b75c@dnews>, Tom2000 wrote:
> My problem is that by using this technique all my frames stay in
> memory (once they are shown) and take up valuable system resources.
>  Currently using 20% of total resources -- way too much.  Does
> anyone know how to dynamically create and destroy frames like you do
> forms.  I want to do something similar to the following (but with
> frames):

You can create frames dynamically as any other component. Here's one
way you can try, it does not implement the modal behavior, but it
should get you started. Frame21 is an empty frame placed on the form at
design time, TFrame3/4 are declared in unit3/4.

type
  TForm1 = class(TForm)
  ...
  private
    ActiveFrame: TFrame;
..
implementation

{$R *.DFM}

uses Unit3, Unit4;

procedure TForm1.ButtonClick(Sender: TObject);
begin
  if ActiveFrame <> nil then
    ActiveFrame.Free;
  if Sender = Button1 then
    ActiveFrame := TFrame3.Create(self);
  if Sender = Button2 then
    ActiveFrame := TFrame4.Create(self);
  //giving self as owner so the last instance
  //is freed with the form.
  with ActiveFrame do
  begin
    Parent := Frame21;
    Align := alClient;
  end;
end;

If you refer to ActiveFrame in code, you need to typecast it to the
right current type.

Renate

Re:Dynamically Creating/Destroy TFrames


Your suggestion is exactly what I was thinking about doing.  Though I wasn't
sure how to make my child frames appear inside the parent.  I didn't think
of simply setting the Parent property to be the master frame.  I will give
this a try.  Thanks for the help.

Tom

Quote
"Renate Schaaf" <rena...@xmission.com> wrote in message

news:VA.000001c8.04a40d00@xmission.com...
Quote
> In article <3963b75c@dnews>, Tom2000 wrote:
> > My problem is that by using this technique all my frames stay in
> > memory (once they are shown) and take up valuable system resources.
> >  Currently using 20% of total resources -- way too much.  Does
> > anyone know how to dynamically create and destroy frames like you do
> > forms.  I want to do something similar to the following (but with
> > frames):

> You can create frames dynamically as any other component. Here's one
> way you can try, it does not implement the modal behavior, but it
> should get you started. Frame21 is an empty frame placed on the form at
> design time, TFrame3/4 are declared in unit3/4.

> type
>   TForm1 = class(TForm)
>   ...
>   private
>     ActiveFrame: TFrame;
> ..
> implementation

> {$R *.DFM}

> uses Unit3, Unit4;

> procedure TForm1.ButtonClick(Sender: TObject);
> begin
>   if ActiveFrame <> nil then
>     ActiveFrame.Free;
>   if Sender = Button1 then
>     ActiveFrame := TFrame3.Create(self);
>   if Sender = Button2 then
>     ActiveFrame := TFrame4.Create(self);
>   file://giving self as owner so the last instance
>   file://is freed with the form.
>   with ActiveFrame do
>   begin
>     Parent := Frame21;
>     Align := alClient;
>   end;
> end;

> If you refer to ActiveFrame in code, you need to typecast it to the
> right current type.

> Renate

Re:Dynamically Creating/Destroy TFrames


The code I posted has a tiny flaw:

You don't need to check for ActiveFrame<>nil before you free it. Free
does this for you. I need to get used to this :)

Renate

Other Threads