Board index » cppbuilder » Form1->Hide()

Form1->Hide()

Quote
Digby Millikan wrote:
> Hello Everyone,
>  Does it make sense that if I have hidden a form using Form1->Hide(), that at a later
> date to completely deallocate the resources occupied by this Form that I should call
> Form1->Close(), or are they already deallocated.

Calling TForm::Hide() simply sets the Visible property of the form to false.
That is, the form is simply not shown, but no resources are freed.
The form exists, you just can't see it.
Therefore, to actually free the memory etc., you should call TForm::Close().

Adam.

 

Re:Form1->Hide()


Hello Everyone,
 Does it make sense that if I have hidden
a form using Form1->Hide(), that at a later
date to completely deallocate the resources
occupied by this Form that I should call
Form1->Close(), or are they already deallocated.
Thanks in advance.
Regards Digby Millikan.

Re:Form1->Hide()


Thanks Adam.

Re:Form1->Hide()


Quote
Digby Millikan wrote in message <888gg1$h...@bornews.borland.com>...
>Hello Everyone,
> Does it make sense that if I have hidden
>a form using Form1->Hide(), that at a later
>date to completely deallocate the resources
>occupied by this Form that I should call
>Form1->Close(), or are they already deallocated.

Hiding a form does not deallocate it. If you are completely finished with a
form, call delete on it to free the memory.

Calling Close will work for the main form of the app. In fact, the whole
program closes when you close the mainform. However, closing other forms
does not, by default, free the memory consumed by the form. Why? Because by
default, closing a form equates to hiding it.

To see proof of this, look at TCustomForm::Close in forms.pas.

procedure TCustomForm.Close;
var
  CloseAction: TCloseAction;
begin
   ...
      if FormStyle = fsMDIChild then
        if biMinimize in BorderIcons then
          CloseAction := caMinimize else
          CloseAction := caNone
      else
        CloseAction := caHide;
      DoClose(CloseAction);
      if CloseAction <> caNone then
        if Application.MainForm = Self then Application.Terminate
        else if CloseAction = caHide then Hide
        else if CloseAction = caMinimize then WindowState := wsMinimized
        else Release;
    end;
end;

If your form is not an MDI child, then CloseAction is initialized to caHide.
After that, the OnClose handler of your form is called. If you do not write
an OnClose handler, or if the OnClose handler does not alter the CloseAction
variable, then CloseAction will still be caHide when that last if statement
is reached. That if statement simply hides the form if CloseAction is
caHide.

PS: You can also see why calling Close on the mainform exits the program.
The line:

    if Application.MainForm = Self then Application.Terminate

is responsible for that.

Harold Howe [TeamB]
http://www.bcbdev.com

Re:Form1->Hide()


Quote
"Harold Howe (TeamB)" wrote:
> If your form is not an MDI child, then CloseAction is initialized to caHide.
> After that, the OnClose handler of your form is called. If you do not write
> an OnClose handler, or if the OnClose handler does not alter the CloseAction
> variable, then CloseAction will still be caHide when that last if statement
> is reached. That if statement simply hides the form if CloseAction is
> caHide.

And to that, one may add that if you wish to deallocate the form's memory and
completely destroy it, you can should change the CloseAction to caFree, in the OnClose handler:
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
   Action = caFree;

Quote
}

But then you'd have to recreate the form if you want to show it again:
   TForm1 *frm = new TForm1(this);
   ...

Adam.

Other Threads