Window events

Got a question here…

When a user clicks on the red close button in an application in macOS, I have this in the Close event… When I run it and try to close the window the application quits as expected when I hit YES. I’m not sure of what to do when hitting NO. I don’t want the window to close but it does so when I run the application.

Dim n As Integer
n = MsgBox(“Do you wish to quit Application?”, 4)
If n = 6 Then
// User clicked Yes
// Testing: MsgBox(“User clicked Yes”)
Quit
ElseIf n = 7 Then
// User clicked No
// Testing: MsgBox(“User clicked No”)

End If

You want the CancelClose event for that, http://documentation.xojo.com/api/deprecated/window.html#window-cancelclose

I added the CancelClose event in the application’s main window, but what do I write in the CancelClose event? That’s what I’m stuck on.

@James Rhodes

[code]Dim YN as Integer = msgbox(“Are you sure you want to quit?”,36)

If YN = 6 Then

Return False ’ Continue to close

Else

Return True ’ Cancel the close

End IF[/code]

Then put clean up code in the close event

Thank you, Brian. I will try that. :slight_smile:

The CancelClose event is working now, Brian. Do I put Quit in the Close event?

@James Rhodes
Well I put this in the App Open event

//Tells application to close App.AutoQuit = True

I would also urge you to move to the more usable and code-clear MessageDialog instead of the old MsgBox. You have much more control over what is displayed, how it’s displayed, and how you handle the users’ choices.

@Tim Jones

I know MsgBox is from the older REALbasic versions. Is MessageDialog from the newer Xojo implementation?

This is what I put in the Window1.CancelClose event…

// Declare the MessageDialog object
Dim d As New MessageDialog

// For handling the result
Dim b As MessageDialogButton

// Display warning icon
d.Icon = MessageDialog.GraphicQuestion

d.ActionButton.Caption = “Yes”
d.CancelButton.Caption = “No”

// Show the Cancel button
d.CancelButton.Visible = True

// Display the message dialog
d.Message = “Do you wish to quit Application?”

b = d.ShowModal

// Determine which button was pressed.
Select Case b
Case d.ActionButton
// User pressed Yes
Return False
Case d.CancelButton
// User pressed No
Return True
End Select


This is what I put in the Window1.Close event…

// Quit the application
Quit

That would be correct.

Also, you can now use situation specific icons that use enum mnemonic values like MessageBox.GraphicCaution instead of needing to do math and come up with a number.

@Dave S
OK. Done. Do I add the Quit to the following?

// Determine which button was pressed.
Select Case b
Case d.ActionButton
// User pressed Yes
Return False
Quit
Case d.CancelButton
// User pressed No
Return True
End Select

No - you add Quit to the Close event as you suspected.

@Tim Jones Thank you. :slight_smile: