Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VB.NET MsgBox Yes only works the second time?

Tags:

vb.net

msgbox

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

like image 330
zxzxzx Avatar asked Nov 30 '25 14:11

zxzxzx


1 Answers

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.

like image 141
Ameen Avatar answered Dec 03 '25 23:12

Ameen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!