3

所以这是我下面的代码。当我单击表单上的 X 按钮时,消息框会显示并单击无效,但是当我单击是时,消息框会关闭并再次快速出现,然后第二次单击任一按钮将关闭表单。它出什么问题了?

    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

提前致谢

4

2 回答 2

6

Application.Exit将导致您的表单关闭(递归),以便您再次看到消息框。如果用户在消息框中按下 Yes,您应该在事件处理程序中什么也不做,并允许应用程序退出继续。

通过设置e.Cancel = True,您将表明您希望表单关闭继续。

于 2013-09-17T04:06:18.387 回答
1

此代码正常工作

 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
于 2013-09-17T04:18:30.260 回答