如何防止表单在btnClose_Click中关闭

时间:2019-06-13 05:10:50

标签: vb.net

当vb.net中的DialogResult.NoTrue时,我试图阻止用户关闭表单。我也尝试过e.cancel=true,但在btnClose_click中不起作用。我提到的不是FormClosingEventArgs。我想把它放在btnClose_Click中。

从OP注释复制的代码;

Private Sub btnClose_Click(sender As Object, e As EventArgs) Handles btnClose.Click
    If CustomMessageBox.Show("Are You Sure?", Buttons.YesNo, Icons.Question,
                          AnimateStyle.ZoomIn) = Windows.Forms.DialogResult.Yes Then
        If Windows.Forms.DialogResult.Yes Then
            ' ---Close The Form
        End If
    ElseIf Windows.Forms.DialogResult.No Or Windows.Forms.DialogResult.None Then
        ' ---Not Closing the Form
    End If
End Sub

1 个答案:

答案 0 :(得分:0)

您需要使用FormClosing事件(或在闭网前关闭NET 2.0)。此事件在关闭事件之前发生,可以取消。

Private Sub Form1_Closing(sender As Object, e As FormClosingEventArgs ) Handles Me.FormClosing
    If CustomMessageBox.Show("Are You Sure?", Buttons.YesNo, Icons.Question,
                          AnimateStyle.ZoomIn) = Windows.Forms.DialogResult.Yes Then
       e.Cancel = true ' <<<< close event will not occur, form stays open
    End If
End Sub

FormClosing Event