How to make a "Are you sure you want to exit?" prompt.

Beginner to programming and new to Xojo , I have been recommended Xojo as a better alternate from Visual Basic 6 . I have made a basic project on Xojo to get started .
Could anyone inform me on how I could use an Exit button , which when clicked , creates a message box/ prompt , asking if the user was sure they wanted to exit by use of buttons ; and then based on the the answer either exit , or cancel .

http://documentation.xojo.com/index.php/MessageDialog

In the cancel close event of your window, add a message box and code something like this:

Dim YN as Integer = msgbox("Are you sure you want to leave?",36)

If YN = 6 Then
   Return False  ' Continue to close
Else
   Return True  ' Cancel the close
End IF

[quote=137261:@Jon Ogden]In the cancel close event of your window, add a message box and code something like this:

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

If YN = 6 Then
Return False ’ Continue to close
Else
Return True ’ Cancel the close
End IF
[/code][/quote]

Could you explain the purpose of value 36 and 6 ?

Please review the MsgBox documentation in the language reference:

http://documentation.xojo.com/index.php/Msgbox

You will see that “6” is the returned value for the Yes button pressed.

36 is the code that sets up the Yes and No buttons. This value could be other things depending on what you want the buttons to read.

@Jon Ogden thanks, its starting to make sense now.

@Jon Ogden

One more thing, is there a key command word that closes a a form? On Visual Basic 6 it was ;

Unload Me 

[quote=137268:@leon johnson]@Jon Ogden

One more thing, is there a key command word that closes a a form? On Visual Basic 6 it was ;

Unload Me [/quote]

me.close for a control or for a window self.close

In Xojo, me refers to an object itself (such as a button, slider, etc. - it could be a window too if you are in the window’s events). Self refers to the parent object - usually a container control or w Window.

So:

self.close

Will close a window.

@Jon Ogden thank you for your time sir , it helped me greatly .

But what about If I don’t want to close the main Window? I want to close the App.
Then use the Close’s App Event, But in the prompt how did you tell to Xojo To stay opened or close the app?

Because, Return True or Return False is not allowed

The Idea of this is Prevent when User exit, via App/Quit Menu or Command+Q(Mac) or Alt+F4(Windows)

Thanks

try this

if MsgBox (“Are you sure you want to Exit ?”, 1, “Exit Alert !”) = 2 then
Exit
end if

[quote=315901:@Gerardo García]But what about If I don’t want to close the main Window? I want to close the App.
Then use the Close’s App Event, But in the prompt how did you tell to Xojo To stay opened or close the app?

Because, Return True or Return False is not allowed

The Idea of this is Prevent when User exit, via App/Quit Menu or Command+Q(Mac) or Alt+F4(Windows)

Thanks[/quote]

You put it in the CancelClose event for the app.

Use something like:

Dim YN as Integer = msgbox("Are you sure you want to leave?",36)

If YN = 6 Then
   Return False  ' Continue to close
Else
   Return True  ' Cancel the close
End IF

The CancelClose event is where you want to do any checking or any undoing of things before you actually close it.

[quote=315910:@Jon Ogden]Dim YN as Integer = msgbox(“Are you sure you want to leave?”,36)

If YN = 6 Then Return False ' Continue to close Else Return True ' Cancel the close End IF[/quote]
Thanks Jon, I tried with this I read you in another thread, I put that code on App Close event and I Got this error:

You cannot return a value because this method has not defined a return type

You cannot use the App.Close event.

Please use App.CancelClose event.

Jon

[quote=315912:@Jon Ogden]You cannot use the App.Close event.

Please use App.CancelClose event.

Jon[/quote]
Perfect, I was using the wrong event. :smiley:

I do not like apps that ask “Are you sure you want to Exit ?”

I general I do not either.

Me either. But sometimes it just makes sense. :slight_smile:

If there isn’t a document that’s been modified, it shouldn’t ask again.