Logo

Hacker’s Guide to Visual FoxPro
An irreverent look at how Visual FoxPro really works. Tells you the inside scoop on every command, function, property, event and method of Visual FoxPro.

QueryUnload

We ask and ask: Thou smilest and art still,
Out-topping knowledge.
—Matthew Arnold, Shakespeare, 1849

This method is called when the user closes a form with the Close box or when code causes a form to close. It’s the place to double-check that the user really wants to close the form and to give her a chance to save her work before closing.

Usage

PROCEDURE oForm.QueryUnload

QueryUnload is called before the form’s Destroy method and gives you a chance to abort the closure. Including NoDefault in QueryUnload prevents the form from closing.

QueryUnload is not called when a form’s associated variable is released or when the form’s Release Method is called directly.

The ReleaseType property indicates how a form is being closed. It’s set before QueryUnload triggers, so you can investigate in the QueryUnload method and take different actions based on the reason the form is closing.

Example

* Here's a typical QueryUnload method:
LOCAL nchoice, lReturn

* Assumes the form has #INCLUDEd "FoxPro.H"
nchoice = MESSAGEBOX("Save changes", ;
    MB_YESNOCANCEL + MB_ICONQUESTION + MB_DEFBUTTON1)

lReturn=.t.
DO CASE
CASE nchoice = IDYES
   WAIT WINDOW "Saving changes"
CASE nchoice = IDNO
   WAIT WINDOW "Exit without saving"
CASE nchoice = IDCANCEL
   NODEFAULT
   lReturn=.f.
ENDCASE

RETURN lReturn

You’ll normally want to integrate QueryUnload for individual forms with an application’s ON SHUTDOWN routine. That way, when a user closes your application by clicking the Close box on the main window, she’ll get a chance to save her work in open windows.

The return value lReturn in the example lets you call QueryUnload from a Shutdown routine and determine whether or not the user allows the form to close. If so, you can call the form’s Release Method from the Shutdown routine to close the form. If not, you can abort the closing of the application.

See Also

Destroy, NoDefault, On ShutDown, Release Method, ReleaseType