How to find out why "OnCloseQuery" is called - is the child MDI closed or the application terminated?

I am currently developing an MDI application.
Each time a new MDI child window is created, the basic data is saved on the fly in the SQLite database, and the open column is set to 1 , so if the user closes the program and opens it again, the windows are restored (also in the case of Anything Bad TM ) .
Thus, each document is always present in the database - the only thing that happens if the user clicks β€œSave” is that the persistent column is set to 1 .
Now, if the MDI child window is closed, open set to 0 - and each line with persistent=0 AND open=0 is doomed and will be deleted.

As a result of this behavior, I don’t need to ask "Save documents"? on ApplicationClose.
But I do have to set every time the MDI child window closes.
This would be easy to do if Mainform.OnCloseQuery is called before MDIChild.OnCloseQuery , but unfortunately this is not the case.

Summarizing:
I need a way to find out if MDIChild.OnCloseQuery is called because

  • the application closes, or
  • MDI child window closes.

Is there any way to do this?

+4
source share
1 answer

You need to override the protected CloseQuery virtual method in your main form. When this fires, you know that the application is going down. But the legacy implementation calls CloseQuery for MDI children before the OnCloseQuery event OnCloseQuery in the main form.

Here is the implementation of TCustomForm CloseQuery :

 function TCustomForm.CloseQuery: Boolean; var I: Integer; begin if FormStyle = fsMDIForm then begin Result := False; for I := 0 to MDIChildCount - 1 do if not MDIChildren[I].CloseQuery then Exit; end; Result := True; if Assigned(FOnCloseQuery) then FOnCloseQuery(Self, Result); end; 

Please note that MDI children receive their CloseQuery notifications before this for Self , i.e. basic form.

So, in your main form, you need:

 type TMainForm = class(TForm); private FCloseQueryExecuting: Boolean; protected function CloseQuery: Boolean; override; public property CloseQueryExecuting: Boolean read FCloseQueryExecuting; end; 

and then an implementation that looks like this:

 function TMainForm.CloseQuery: Boolean; begin FCloseQueryExecuting := True; try Result := inherited CloseQuery; finally FCloseQueryExecuting := False; end; end; 

MDI children can then check the status of the main FCloseQueryExecuting in their OnCloseQuery events.

+7
source

All Articles