Reputation: 1393
I tried both of the following code, and it gives me the same result. That yes button, produces another MsgBox then click yes again before closing, while no button works perfectly well. How can I fix this problem?
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
'Dim result = MessageBox.Show("Are You Sure You Want To Quit?", Me.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation)
'If result = DialogResult.No Then
' Application.Exit()
'Else
' e.Cancel = True
'End If
Select Case MessageBox.Show("Are You Sure You Want To Quit?", Me.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation)
Case MsgBoxResult.Yes
Application.Exit()
Case MsgBoxResult.No
e.Cancel = True
End Select
End Sub
Upvotes: 0
Views: 56
Reputation: 38865
I can't see where MsgBoxResult
came from, but this should work:
Dim MsgBoxResult As DialogResult
MsgBoxResult = MessageBox.Show( _
"Are You Sure You Want To Quit?", _
Me.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation)
Select Case MsgBoxResult
Case DialogResult.Yes
' Do nothing - let it close!
'Application.Exit()
Case DialogResult.No
e.Cancel = True
End Select
Or glue it together:
Dim MsgBoxResult As DialogResult = MessageBox.Show("Are You Sure You Want To Quit?",
Me.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation)
By calling Application.Exit you were invoking a new FormClose event before this one completed. Provided this is a MainForm, just let the application close normally.
Upvotes: 1
Reputation: 2992
Try this also
e.Cancel = (System.Windows.Forms.MessageBox.Show("Are You Sure You Want To Quit?", Me.Text, Windows.Forms.MessageBoxButtons.YesNo, Windows.Forms.MessageBoxIcon.Exclamation) = Windows.Forms.DialogResult.No)
Upvotes: 1