-----Original Message-----
From: Donovan J. Edye <[EMAIL PROTECTED]>
To: Multiple recipients of list delphi <[EMAIL PROTECTED]>
Date: Wednesday, 10 November 1999 10:49 AM
Subject: [DUG]: Freeing forms..... Confusion....



Try something like this
implementation
uses Unit2;


procedure Blah...
var
    Changed: Boolean;
begin
    with TForm2.Create(Self) do
        try
            Caption := 'Hello, I'm Form2'
            Changed := ShowModal = mrOK;
        finally
            Free;
        end;
        if Changed then
            DoSomeOtherProcessing;
end;

Put 2 buttons OK and Cancel on Form2

Set ButtonOK.ModalResult = mrOK and Default = True
set ButtonCancel.ModalResult = mrCancel and Cancel = True

This is a good starting point for many forms.

Note the Create is before the Try, and the Finally guaranteess it will
allways be freed.
Some say that free should be replaced by release, but I fail to see this is
relevant argument in this situation.
/***************************************************************************
**************************/

P,

Given TForm1, TForm2 (TForm1 is the main form and TForm2 is NOT
automatically created) with the following code:

Instance1:

No code in form2 and the following works as expected. Form2 is shown,
closed.

procedure TForm1.Button1Click(Sender: TObject);
begin
     try
        Application.CreateForm(TForm2, Form2);
        Form2.Show;
        Sleep(2000);
        Form2.Close;
        Sleep(2000);
        file://ShowMessage('Hello');
        Form2.Caption := 'I was never freed';
        Form2.Show;
     except
       on E : Exception do
          ShowMessage(E.Message);
     end;
end;

Instance2:

procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);
begin
     Action := caFree;
end;

TForm1 code from above. Why do I not get an access violation when I attempt
to set the caption? The close event has triggered and the action should have
freed the form. Put a showmessage PRIOR to the caption being set and you get
an access violation. Can someone explain this to me?


------------------------------------------------------------------------
--Donovan
Donovan J. Edye [www.edye.wattle.id.au]
Namadgi Systems, Delphi Developer
Web: www.namsys.com.au E-Mail: [EMAIL PROTECTED]
Voice: +61 2 6285-3460 Fax: +61 2 6285-3459
TVisualBasic = Class(None);
------------------------------------------------------------------------

---------------------------------------------------------------------------
    New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
                  Website: http://www.delphi.org.nz

---------------------------------------------------------------------------
    New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
                  Website: http://www.delphi.org.nz

Reply via email to