使用 FormClosing 事件关闭应用程序 VB.net
Close Application with FormClosing Event VB.net
我尝试使用表单关闭事件退出我的应用程序,但确认消息框出现两次。
这是我的:
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,我相信,您首先需要确保Form1 确实是您应用程序的启动窗体。您可以从项目的属性中确认这一点。如果是这样,那么您甚至不需要调用 Application.Exit().
其次,尝试将 Me.FormClosing 替换为 MyBase.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
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 起已过时
也许您的应用程序有多个表单,每当您在启动表单上应用关闭事件时,它都会重复您拥有的表单数量。我遇到了同样的问题,并通过更改其他形式的事件句柄解决了这个问题,而不是启动形式,就像下面的例子一样:
发件人:
Handles Me.Closing
收件人:
Handles MyBase.Closing
我尝试使用表单关闭事件退出我的应用程序,但确认消息框出现两次。
这是我的:
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,我相信,您首先需要确保Form1 确实是您应用程序的启动窗体。您可以从项目的属性中确认这一点。如果是这样,那么您甚至不需要调用 Application.Exit().
其次,尝试将 Me.FormClosing 替换为 MyBase.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
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 起已过时
也许您的应用程序有多个表单,每当您在启动表单上应用关闭事件时,它都会重复您拥有的表单数量。我遇到了同样的问题,并通过更改其他形式的事件句柄解决了这个问题,而不是启动形式,就像下面的例子一样:
发件人:
Handles Me.Closing
收件人:
Handles MyBase.Closing