【发布时间】:2012-04-10 18:43:00
【问题描述】:
我有一个在系统托盘图标中运行的窗体应用程序。如果用户按下窗体的 X 按钮,则会显示一个消息框,其中包含是和否(是 -> 关闭窗体---否 -> 保留在系统托盘图标中运行的表单)。 我正在考虑防止当用户打开另一个应用程序实例时已经有一个实例正在运行,所以我使用了这段代码:
If Process.GetProcessesByName(Process.GetCurrentProcess.ProcessName).Length> 1 Then
MessageBox.Show("Another instance is running", "Error Window", MessageBoxButtons.OK,
MessageBoxIcon.Exclamation)
Application.Exit()
End If
问题是,当我想测试此消息时,会显示消息,但在我按下确定后,会出现一个新消息框(来自 Private Sub Form_FormClosing 的那个)。如果我选择否,我将不得不运行实例! 我已阅读 Application.Exit 触发 Form_FormClosing 事件。
有没有可能取消Form_FormClosing事件的触发,还是我做错了什么?
'这是合页程序
Private Sub Form_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Try
Dim response As MsgBoxResult
response = MsgBox("Are you sure you want to exit", CType(MsgBoxStyle.Question + MsgBoxStyle.YesNo, MsgBoxStyle), "Confirm")
'If the user press Yes the application wil close
'because the application remains in taskmanager after closing i decide to kill the current process
If response = MsgBoxResult.Yes Then
Process.GetCurrentProcess().Kill()
ElseIf response = MsgBoxResult.No Then
e.Cancel = True
Me.WindowState = FormWindowState.Minimized
Me.Hide()
NotifyIcon1.Visible = True
End If
PS:我不是程序员,所以请不要对我苛刻:)
【问题讨论】:
标签: vb.net formclosing