Naucle Naucle - 16 days ago 7
Vb.net Question

Handle Closing WPF window with [X] button and a normal button in one single sub

I'm handling window closing using the red [X] button in WPF/VB.NET like this :

Private Sub Window_Closing(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
If MessageBox.Show("Voulez-vous vraiment fermer l'outil ?", "FT_SOFT", MessageBoxButton.YesNo) = MessageBoxResult.No Then
e.Cancel = True
End If
End Sub


And I have a "Close" button inside the window that is handled in this sub :

Private Sub MenuItem_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles mniQuitter.Click
If MessageBox.Show("Voulez-vous vraiment fermer l'outil ?", "FT_SOFT", MessageBoxButton.YesNo) = MessageBoxResult.Yes Then
RemoveHandler MyBase.Closing, AddressOf Window_Closing
MyBase.Close()
End If
End sub


The line
RemoveHandler MyBase.Closing, AddressOf Window_Closing
prevents the first sub from being called when the close button is clicked, because otherwise, you would get two MessageBoxes.

I'm looking for a cleaner way to do this, by calling the first sub inside the second sub, but the
e
argument type doesn't match (obviously). How can this be done ?

ASh ASh
Answer

only call MyBase.Close() from MenuItem_Click

Private Sub MenuItem_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles mniQuitter.Click
    MyBase.Close()
End sub

the next event will be Window_Closing and MessageBox will appear from there

Comments