【问题标题】:Close Application with FormClosing Event VB.net使用 FormClosing 事件 VB.net 关闭应用程序
【发布时间】:2017-06-09 09:43:14
【问题描述】:

我尝试通过表单关闭事件退出我的应用程序,但确认消息框出现两次。

这就是我所拥有的:

Private Sub FrmMainPlatform_FormClosing(sender As Object, e As FormClosingEventArgs) _
 Handles MyClass.FormClosing

    Dim result As Integer
    result = MessageBox.Show("Are you want to close", "Exit", MessageBoxButtons.YesNo, MessageBoxIcon.None)
    If (result = DialogResult.No) Then
        e.Cancel = True
    Else
        Application.Exit()
    End If

End Sub

我也尝试过这个解决方案:

Private Sub FrmMainPlatform_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Select Case MessageBox.Show("Are you sure you want to exit?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
    Case Windows.Forms.DialogResult.Yes
        'nothing to do here the form is already closing
    Case Windows.Forms.DialogResult.No
        e.Cancel = True 'cancel the form closing event
        'minimize to tray/hide etc here 
    End Select
End Sub

表单已关闭,但应用程序仍在运行。

【问题讨论】:

  • 点击yes会显示两次吗?
  • 是的,它出现了两次
  • 该代码对我来说工作正常,所以一定有其他地方导致了问题。您确定是调用两次的确切代码吗?在Select Case MessageBox.Show("Are you sure you want to exit?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) 上放置一个断点并运行代码。它会击中两次吗?

标签: vb.net formclosing


【解决方案1】:

@karihalan,我相信,您首先需要确保 Form1 实际上是您的应用程序的启动表单。您可以从 Project 的属性中确认这一点。如果是这样,那么您甚至不需要调用 Application.Exit()。

其次,尝试用 MyBase.FormClosing 替换 Me.FormClosing... 像这样:

Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing

另外,请确保您没有订阅两次表单关闭事件,可能使用 Addhandler 语句。

希望这会有所帮助。

【讨论】:

  • 嗨 Azaz 我试过了,但同样的结果出现了两次
  • 哦,我认为您使用两个 Handles 语句订阅了两次 FormClosing 事件。一个使用 Form1_FormClosing 方法,另一个使用 FrmMainPlatform_FormClosing。您需要订阅一次 FormClosing。换句话说,将 Handles 语句与 Form1_FormClosing 或 FrmMainPlatform_FormClosing 一起使用。
  • 抱歉,我在堆栈溢出中找到了两个解决方案。我刚刚粘贴了该解决方案。现在我用第二个选项改变了我自己的代码。两种解决方案以相同的形式应用
【解决方案2】:

更新:您可以尝试关闭所有表单。我会把它放在主要的表单关闭事件中。

For each f as Form in My.Application.OpenForms
 f.Close()
Next

摆脱第一个代码块。这就是我所做的,它询问我是否只想关闭一次,当我点击是时它关闭,当我说不时它没有关闭。

Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Select Case MessageBox.Show("Are you sure you want to exit?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
    Case Windows.Forms.DialogResult.Yes
        'nothing to do here the form is already closing
    Case Windows.Forms.DialogResult.No
        e.Cancel = True 'cancel the form closing event
        'minimize to tray/hide etc here 
    End Select
End Sub

【讨论】:

  • 嗨 Roach,它只关闭当前表单,但应用程序仍在任务管理器中运行
  • @karihalanudhayasuriyan,你能检查一下你项目的启动表格吗?它设置为什么?
  • 您的项目是否包含多个表单?
  • yes.main 表单是一个,但我们有 15 个表单,它的小应用程序
  • 我会在您关闭整个应用程序的主表单中使用第二个代码块。它似乎成功了。
【解决方案3】:
Private Sub FrmMainPlatform_FormClosing(sender As Object, e As FormClosingEventArgs) _
    Handles Me.Closing

    Dim result As Integer
    result = MessageBox.Show("Are you want to close", "Exit", MessageBoxButtons.YesNo, MessageBoxIcon.None)
    If (result = DialogResult.No) Then
        e.Cancel = True
    Else
        Application.Exit()
    End If

End Sub

嗨, 我得到了这个问题的临时解决方案。 Exit 方法不会引发 Closed 和 Closing 事件,这些事件在 .NET Framework 2.0 中已过时

【讨论】:

    猜你喜欢
    • 2016-02-06
    • 2012-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多