简体   繁体   English

VB.NET MsgBox是的,只能第二次使用吗?

[英]VB.NET MsgBox Yes only works the second time?

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. 当我单击表单上的X按钮时,消息框显示并单击“无”,但是当我单击“是”时,消息框将关闭并快速再次出现,然后第二次单击任一按钮将关闭表单。 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. Application.Exit将导致您的表单被关闭(递归),因此您再次看到消息框。 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. 如果设置e.Cancel = True ,则表示您希望表单关闭继续。

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 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM