문제

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.

다른 팁

This code Work Properly

 Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
            If (MsgBox("Are you sure to Close", MsgBoxStyle.YesNo, "Close Event") = MsgBoxResult.Yes) Then
                e.Cancel = False
            Else
                e.Cancel = True
            End If
End Sub
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top