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

时间:2017-06-09 09:43:14

标签: 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", 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

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

3 个答案:

答案 0 :(得分:0)

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

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

    <!DOCTYPE html>
    <html>
    <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script>
    $(document).ready(function(){
        $("div").click(function(){
            var clnm=$(this).attr("class");
            var idSel=$(this).attr("id");
        });
    });
    </script>
    </head>
    <body>

    <div class="test_class" id="testId">
    Example
    </div>
    </body>
    </html>

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

希望这会有所帮助。

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

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

之后已过时