Jump to content
Tom F

How to .Free a non-modal form?

Recommended Posts

I create a non-modal form like this:
 

procedure TForm1.Button1Click(Sender: TObject);
begin
  FNonModalForm := TForm2.Create(Self);
  FNonModalForm.Show;
end;


What's a good way to have a non-modal Form2 signal its owner Form1 that the non-modal form has been closed by the user so the owner can free Form2?

AFAIK, it can't be done in a callback from Form2.FormClose because that's too early for the owner Form1 to free Form2.

 

Send a Windows message to the owner form?

Share this post


Link to post

A TForm or descendant has an event OnClose. 

 

Quote

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

  Action := caFree;

end;
 

Thats it. No further interaction needed.

 

Share this post


Link to post
2 hours ago, Tom F said:

Is Assigned(FNonModalForm) false after that statement?

No.  You have to reset it manually.

1 hour ago, Attila Kovacs said:

A TForm or descendant has an event OnClose. 

 

Thats it. No further interaction needed.

Unless the parent form needs to reset its FNonModalForm variable, in which case you would need to move the OnClose handler to the parent form instead, eg:

procedure TForm1.Button1Click(Sender: TObject);
begin
  if FNonModalForm = nil then
  begin
    FNonModalForm := TForm2.Create(Self);
    FNonModalForm.OnClose := NonModalFormClosed;
  end;
  FNonModalForm.Show;
end;

procedure TForm1.NonModalFormClosed(Sender: TObject; var Action; TCloseAction);
begin
  FNonModalForm := nil;
  Action := caFree;
end;

 

Edited by Remy Lebeau
  • Like 4

Share this post


Link to post

Thanks, Remy!  That's what I needed.  It never occurred to me to hook the OnClose event itself!


 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×