So this is my code below. When I click the X button on the form, the message box shows and clicking no works but when I click yes, the message box closes and comes up again quickly and then the second time, clicking either button will close the form. What's wrong with it?
Private Sub Config_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
Dim result = MessageBox.Show("Would you like to quit?", MessageBoxButtons.YesNo)
If result = DialogResult.No Then
e.Cancel = True
ElseIf result = DialogResult.Yes Then
Application.Exit()
End If
End Sub
Thanks in advance
Application.Exit will cause your form to be closed (recursively) so you see the message box again. In the event the user presses Yes in the message box, you should just do nothing in your event handler and allow the application exit to continue.
By not setting e.Cancel = True you would indicate that you want the form shutdown to continue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With