结案事件

时间:2019-02-14 14:03:32

标签: wpf vb.net

我正在通过询问以下问题来关闭应用程序,但是它无法正常工作。

任何帮助都很棒!

Private Sub MainWindow_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing


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

End Sub

2 个答案:

答案 0 :(得分:2)

好的,这个问题已经有了一个可以接受的答案,但是我认为我只想添加一个单线,因为看来OP仅在进行MessageBox检查。我认为boolean事件中不需要Application.Exit()Environment.Exit(0) Closing

Private Sub MainWindow_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing
    If Not MessageBox.Show("Are you sure you want to close?", "Exit", MessageBoxButton.YesNo, MessageBoxImage.None) = MessageBoxResult.Yes Then e.Cancel = True
End Sub

答案 1 :(得分:1)

这应该有效:

Private _handle As Boolean = True
Private Sub MainWindow_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing
    If _handle = True Then
        e.Cancel = True

        Dim result = MessageBox.Show("Are you want to close?", "Exit", MessageBoxButton.YesNo, MessageBoxImage.None)
        If (result = MessageBoxResult.Yes) Then
            _handle = False
            Environment.Exit(0)
        End If
    End If
End Sub