当vb.net中的DialogResult.No
为True
时,我试图阻止用户关闭表单。我也尝试过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
答案 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