How to get save dialog box when closing the app without saving data

How to get save dialog box when closing the app without saving data

Set your window dirty, then in the CancelClosing Event, put code to ask the user if (s)he want to save or drop changes:

https://documentation.xojo.com/api/user_interface/desktop/desktopwindow.html.CancelClosing

Yeah, in the past there was a tutorial project made by Xojo (or their former name) which creates a text editor application.

The window may closes w/o asking for Save dialog box, except:
if the user typed something in, the flag (or: boolean variable) was set to “dirty”, or “It’sEditedNow” (true), so when closing that window, a SaveDialog box would appear.

Rough principals:

  • Use the .Changed property of the DesktopWindow class to indicate if the data loaded into that window has changed and needs to be saved.
  • When you load data set .Changed to false.
  • When the user changes data set .Changed to true.
  • Add a CancelClose event to your window. In that event event check if the .Changed property and offer to save if required.

In CancelClose you should offer a few options:

  • An option to save the changes
  • An option to close the window anyway without saving the changes.
  • An option to cancel the close and continue using the app, you can achieve this by returning false from the event.

Close (no pun intended). You must return true instead :wink:

True, you must :slight_smile:

1 Like