【问题标题】:Skip Me.FormClosingSkip Me.FormClosing
【发布时间】:2015-11-08 16:46:47
【问题描述】:

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

我的代码:

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 的情况下关闭表单吗?

【问题讨论】:

  • 使用模块级标志或者CloseReason。一个简单的isDone 标志似乎就足够了。该消息实际上与任务是否完成有关;不是表单是否正在关闭。
  • @Plutonix 我找不到在某处写的方法,VS 在我写的任何地方都会显示错误
  • Private IsDone As Boolean = False 完成后将其设置为 true,然后在表单关闭中进行评估。您可能想(需要!)了解Scope in Visual Basic

标签: vb.net formclosing


【解决方案1】:

使用Boolean 标志来确定安装状态(成功或失败/中止)

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-21
    • 2018-02-03
    • 2021-05-16
    • 2016-08-05
    • 1970-01-01
    相关资源
    最近更新 更多