如果消息框为yes,则窗体不会关闭

时间:2014-02-13 12:40:47

标签: vb.net winforms visual-studio-2008

这是我的代码:

Private Sub frmName_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    If e.CloseReason = CloseReason.UserClosing Then
        If MessageBox.Show("Are you sure you want to logout?", "Exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then                   
            Me.Close()
        Else
            e.Cancel = True
        End If
    End If
End Sub

如果我点击no,它将取消关闭表单,但是当我点击yes时,邮件框会重复出现。我想要发生的是当我点击关闭按钮,然后单击yes它将关闭表单。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

由于表格已经结束,因此无需再次致电Close()。以下代码应该有效:

Private Sub frmName_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    If e.CloseReason = CloseReason.UserClosing Then
        If MessageBox.Show("Are you sure you want to logout?", _
                "Exit", MessageBoxButtons.YesNo, _
                MessageBoxIcon.Question) = Windows.Forms.DialogResult.No Then                   
            e.Cancel = True
        End If
    End If
End Sub