Exit Confirmation box

Spudwiser

New member
Joined
Mar 26, 2011
Messages
2
Programming Experience
Beginner
Hello,

I am trying to make a routine to handle closing a program with a confirmation dialog box.
The problem I am having is I cannot get the confirmation to work right. Here is what I have now:

Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
Dim closeprogramyes As DialogResult
Dim closeprogramno As DialogResult
closeprogramno = Windows.Forms.DialogResult.No
closeprogramyes = Windows.Forms.DialogResult.Yes
MessageBox.Show(
"Are you sure you want to exit?", "Exit", MessageBoxButtons.YesNo)
If closeprogramyes = Windows.Forms.DialogResult.Yes Then
Me.Close()
ElseIf closeprogramyes = Windows.Forms.DialogResult.No Then
End If

End Sub

Right now if I run through that clicking yes or no closes the program. I have also tried:

Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
Dim closeprogram As DialogResult

MessageBox.Show(
"Are you sure you want to exit?", "Exit", MessageBoxButtons.YesNo)
If closeprogram = Windows.Forms.DialogResult.Yes Then
Me.Close()
ElseIf closeprogram = Windows.Forms.DialogResult.No Then
'do nothing
End If

End Sub

If I run this nothing happens. The dialog box goes away but the program stays open. I have also tried options with e.cancel and my version of visual studio (2008) does not recognize that as a proper command.

Any help would be great as I am very new to this.
 
Try:

closeprogram = MessageBox.Show("Are you sure you want to exit?", "Exit", MessageBoxButtons.YesNo)

You need to check whether the user selected yes or no.
 
The core of your problem is
VB.NET:
[SIZE=2]MessageBox.Show([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Are you sure you want to exit?"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Exit"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], MessageBoxButtons.YesNo)[/SIZE]
That doesn't assign the result to any sort of variable. Once you click a button the dialogue is gone, and the chosen button with it.
If you replace that with the following it should work.
VB.NET:
        closeProgram = MessageBox.Show("Are you sure you want to exit?", "Exit", MessageBoxButtons.YesNo)[SIZE=2]
[/SIZE]



On an aside, I think that you are trying to do this in the wrong place, and that it would be better to use the form closing event. As this is written, any other way of closing the program (including the X in the corner) will bypass the confirmation dialogue completely. Simply have the exit toolStripMenuItem tell it to close, and let the program catch the event. The e.cancel code you refer to is only defined within that close handler, so if you do make the switch you'll be able to (and need to) use it.
 
Thanks

I will give this a try in the morning. It is getting late here and I have been looking at this screen all day. Thanks for the replies!


EditNess

HUZZAH, I got it using this:

If MessageBox.Show("Are you sure you want to exit?", "Exit Confirm", MessageBoxButtons.YesNo, _
MessageBoxIcon.Error, _
MessageBoxDefaultButton.Button1) = Windows.Forms.DialogResult.Yes
Then
Me.Close()
End If

This came to me after a good nights sleep. Thanks for all your help
 
Last edited:
Here is a working example. Place code in a button's Click event:

VB.NET:
Dim ans As Integer
	ans = MessageBox.Show("Press OK to keep going, or Cancel to quit",_ 
		 "Press me", MessageBoxButtons.OKCancel)
	If ans = DialogResult.OK Then
		txtFirst.Focus()
	Else
		Application.Exit()
	End If
 
Solitaire, the return type of Show method is DialogResult, not Integer. That the underlying type of that enumeration is Integer is beside the point, why intentionally lose the benefit of using the correct type? Using the DialogResult type will allow the editor to present the valid enumeration values when coding.
 
JohnH: They seem to be interchangeable and I don't see the real distinction. If I replace

If ans = DialogResult.OK Then
with
If ans = 1 Then

the program seems to work exactly the same, whether or not I declare ans as DialogResult or as an Integer.

Under what circumstances would DialogResult apply when Integer would not work?
 
the program seems to work exactly the same, whether or not I declare ans as DialogResult or as an Integer.

Under what circumstances would DialogResult apply when Integer would not work?

Working and being best practice are often very different concepts. For simple readability in your code it is better to have the proper type. After all dialogResult.OK is pretty self-explanatory for what it represents, 1 is not.
 
Under what circumstances would DialogResult apply when Integer would not work?
Integer "would work" because that is the underlying primitive type of that enumeration, which means all enumeration values can be converted to Integer values. The other way around is of course not possible, the enumeration has distict value members defined, see DialogResult Enumeration (System.Windows.Forms)
I recommend you take a few minutes to read up on enumerations, they are defined by the Enum statement in VB.
It's also not only about readability, which still is a key element, intellisense provides valid values for you when working with enumeration types, which makes coding with them a breeze, it eliminates most keyboard input. In UI designer enumerations provide single click choice lists for the valid values.
 
Back
Top