通过用户选择取消表单关闭

时间:2012-01-11 04:55:46

标签: vb.net

在vb.net windows应用程序中,我需要用户在关闭应用程序之前确认。我在FormClosing event

中有这段代码
If BackgroundWorker1.IsBusy Then
    Dim UserSelection As Integer = MsgBox("Do you want Cancel Processing and Exit Application?", MsgBoxStyle.Exclamation + MsgBoxStyle.YesNo, "Exit Application")
    If UserSelection = 6 Then
        BackgroundWorker1.CancelAsync()
        e.Cancel = True
    Else
        ????
    End If
End If

如果用户点击No

,如何取消表单关闭?

尝试e.Cancel = false,但它不起作用(退出应用程序)。

3 个答案:

答案 0 :(得分:3)

e.Cancel = True会停止表单关闭。

答案 1 :(得分:1)

根据文档“e.Cancel = True”预防结束时的表格

答案 2 :(得分:0)

这是取消表单已关闭的完整代码。我们应该使用FormClosing Event。

Private Sub Frm1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If MessageBox.Show("Do you want to closed", Me.Text, MessageBoxButtons.OKCancel) = Windows.Forms.DialogResult.Cancel Then
            e.Cancel = True
        End If
    End Sub