使用FormClosing事件VB.net关闭应用程序

使用FormClosing事件VB.net关闭应用程序,vb.net,formclosing,Vb.net,Formclosing,我试图退出我的申请表关闭事件,但确认消息框出现两次 这就是我所拥有的: 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", MessageBo

我试图退出我的申请表关闭事件,但确认消息框出现两次

这就是我所拥有的:

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

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

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

第二,尝试用MyBase.FormClosing替换Me.FormClosing。。。像这样:

Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
另外,请确保您没有订阅表单关闭事件两次,可能是使用Addhandler语句


希望这会有所帮助。

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

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
嗨,
我找到了这个问题的临时解决办法。退出方法不会引发关闭和关闭事件,这些事件在.NET Framework 2.0中已过时。如果单击“是”,它是否会显示两次?是,它会显示两次该代码对我来说很好,因此一定是其他地方导致了问题。你确定调用两次的正是那个代码吗?在
选择Case MessageBox.Show(“您确定要退出吗?”,“确认”,MessageBoxButtons.YesNo,MessageBoxIcon.Question)上放置断点
并运行代码。它击中它两次了吗?嗨,Azaz,我试过了,但同样的结果是两次出现,我想你们是在使用双句柄语句订阅FormClosing事件两次。一个使用Form1\u FormClosing方法,另一个使用FrmMainPlatform\u FormClosing方法。您需要订阅表单关闭一次。换句话说,将Handles语句与Form1\u FormClosing或FrmMainPlatform\u FormClosing一起使用。很抱歉,我在堆栈溢出中发现了两种解决方案。我只是贴在那个溶液上。现在我用第二个选项修改了自己的代码。两个解决方案应用于同一formHi Roach,其仅关闭当前表单,但应用程序仍在任务中运行manager@karihalanudhayasuriyan,你能检查一下你的项目启动表吗?它设置为什么?您的项目是否包含多个表单?是的。主表单是一个表单,但我们有15个表单它的小应用程序我将在主表单中使用第二个代码块,在主表单中关闭整个应用程序。这似乎奏效了。
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