CancelClose

I have the following code in my CancelClose Event but it seems to fire multiple times ie the MsgBox appears twice.

dim n as integer
n = MsgBox(“Are you sure you want to Quit.”, 65)
If n = 1 Then
// user pressed OK
if gsavecrossword = false then
gsavecrossword = TRUE
if uppercase(right(currentfile,8)) = “.CWPTEXT” then
SavePCW
else
SavePCW1
end if
end if
ElseIf n = 2 Then
// user pressed Cancel
return true
End If

In the Close event I have Quit

by putting a msgbox in the CancelClose event, that causes the Activate Event for the Window to first again when you close the Msgbox… so… don’t do that

Guess: Using Quit is probably what’s causing the dialog to appear twice. Some kind of funny order of events that logically works itself out to CancelClose occurring twice.

If you are trying to ask about saving a document, using specific phrasing can get the classic keyboard shortcuts working (Cmd-D for Don’t Save).

This is what I recommend for a “Would you like to save changes before closing?” CancelClose. Place it on the Window, not App, nor should you invoke Quit manually at any point. This example uses the phrasing that allows the user to control the dialog with shortcuts on Mac. While nothing special happens on Windows, no workarounds are necessary to use this example on Windows.

Function CancelClose(appQuitting as Boolean) Handles CancelClose as Boolean
  // Don't bother the user if the document isn't dirty
  if self.ContentsChanged = false then return false
  
  dim oDlg as new MessageDialog
  oDlg.Message = "Do you want to save changes to this document before closing?"
  oDlg.Explanation = "If you don't save, your changes will be lost."
  oDlg.ActionButton.Caption = "&Save"
  oDlg.CancelButton.Caption = "Cancel"
  oDlg.CancelButton.Visible = true
  oDlg.AlternateActionButton.Caption = "&Don't Save"
  oDlg.AlternateActionButton.Visible = true
  
  dim oBtn as MessageDialogButton = oDlg.ShowModalWithin(self)
  
  select case oBtn
  case oDlg.ActionButton
    // Save
    SaveMethod
    
  case oDlg.CancelButton
    // Cancel close
    return true
    
  case oDlg.AlternateActionButton
    // Don't Save
    
  end select
End Function

Also note that in code of the first post, the only explicit return statement is in the ElseIf clause of the If/Else construct, where the user has clicked Cancel. The implicit return that happens in all other cases will return false, which could cause problems.

I left return false out as it is the default to allow quitting, so shouldn’t it do the saving and proceed to the close event and Quit?

The cancel close event is “Do you want to cancel the close?” Returning true cancels the close, returning false allows it.
Whether or not it saves is up to you.

Thanks Tim

Using Tim’s cancel close method seemed to work fine in the IDE but when I test a compiled version it comes up with a App quit unexpectedly Dialog.

I don’t have quit invoked anywhere and the save method seems to work without error when invoked from the menu so not sure where to look for a possible error??

Did you Return False ?

In what part Tim? After the SaveMethod?