跳过 Me.FormClosing

Skip Me.FormClosing

我在关闭表单前设置了警告消息,但有时有办法跳过它吗?

我的代码:

Sub Me_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
    If MessageBox.Show("Are you sure you want to cancel the installation?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
        e.Cancel = False
    Else
        e.Cancel = True
    End If
End Sub

但我有一个最终代码,必须在没有此消息的情况下关闭应用程序:

Private Sub Done_Click(sender As Object, e As EventArgs) Handles Done.Click
    'need to close without warning
    Close()
End Sub

你能帮我改变这个或添加一些东西让那个按钮在不启动的情况下关闭表单吗Me.FormClosing

使用 Boolean 标志确定安装状态(成功或 fail/abort)

Private installSuccess As Boolean ' False by default.

Private Sub Install()

    Try 
        ' Installer logic here
        ' ...

        Me.installSuccess = True

    Catch ' ex As Exception

    End Try

End Sub

然后:

Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) _
Handles MyBase.FormClosing

    If Me.installSuccess Then
        Exit Sub
    End If

    If MessageBox.Show("Are you sure you want to cancel the installation?", "Warning", 
                       MessageBoxButtons.YesNo, 
                       MessageBoxIcon.Question) = DialogResult.Yes Then
        e.Cancel = False

    Else
        e.Cancel = True

    End If

End Sub