CancelClose

  1. 2 weeks ago

    Martin F

    Oct 6 Pre-Release Testers, Xojo Pro Kiama, Australia

    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

    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
  2. Dave S

    Oct 6 San Diego, California USA

    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

  3. Tim P

    Oct 6 Pre-Release Testers Answer feedback://46303,51561
    Edited 2 weeks ago

    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
  4. Robert W

    Oct 6 Western Canada

    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.

  5. last week

    Martin F

    Oct 7 Pre-Release Testers, Xojo Pro Kiama, Australia

    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?

  6. Tim P

    Oct 7 Pre-Release Testers feedback://46303,51561

    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.

  7. 7 days ago

    Martin F

    Oct 10 Pre-Release Testers, Xojo Pro Kiama, Australia

    Thanks Tim

  8. 2 days ago

    Martin F

    2 days ago Pre-Release Testers, Xojo Pro Kiama, Australia

    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??

  9. Emile S

    2 days ago Europe (France, Strasbourg)

    @Tim P Returning true cancels the close, returning false allows it.

    Did you Return False ?

  10. Martin F

    2 days ago Pre-Release Testers, Xojo Pro Kiama, Australia

    In what part Tim? After the SaveMethod?

or Sign Up to reply!