How to show messagebox one time only before closing window

1.7k views Asked by At

I have made a standalone application which does some engineering analyses for research purpose. It was made to show the graph displaying results in another window (I don't know what the right word is for it. Let me call it subwindow) linked to the main window. To remind end users to save the input files before closing the main window, I added a code behind for the notification as shown below:

    private void Window_Closing(object sender, CancelEventArgs e)
    {
        MessageBoxResult result = MessageBox.Show("Please Be Sure That Input & Output Files Are Saved.  Do You Want To Close This Program?", "Confirmation", MessageBoxButton.YesNo, MessageBoxImage.Warning);
        if (result == MessageBoxResult.Yes)
        {
            Application.Current.Shutdown();
        }
        else
        {
            e.Cancel = true;
        }
    } 

XAML Code is:

<Window x:Class="GMGen.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Icon="Icon1.ico"
Title="GMGen" WindowState="Maximized" Closing="Window_Closing" >
<DockPanel x:Name="RootWindow">
    <ContentControl Content="{Binding CurrentPage}" />
    <Grid >
    </Grid >
</DockPanel>

When I close the main window without opening any subwindows showing graphs, it works fine. The notification window pops up and clicking "yes", then the program get terminated. However, if I open & close subwindows before closing main window, things go little weird. The notification pops up. Click yes, the program is not terminated. Instead, another notification pops up. Click yes then another one pops up. This thing happens about same times as I opened and closed subwindows. i.e. If I opened and closed subwindows four times, the notification appears four to five times. I don't know what causes this problem. I just want to show the messagebox only one time. If you anyone know the solution, please let me know. I really appreciate your help.

1

There are 1 answers

2
loopedcode On BEST ANSWER

Most likely you are subscribing to the closing event on every window, as it is firing N number of times.

Without seeing your actual implementation, its hard to say what would be best option for to you resolve it. Here is one way you can handle it by having static flag for confirmation. Once you display confirmation, flag will prevent subsequent popup.

private static bool _isConfirmed = false;
private void Window_Closing(object sender, CancelEventArgs e)
{

    if (!_isConfirmed)
    {
        MessageBoxResult result = MessageBox.Show("Please Be Sure That Input & Output Files Are Saved.  Do You Want To Close This Program?", "Confirmation", MessageBoxButton.YesNo, MessageBoxImage.Warning);
        if (result == MessageBoxResult.Yes)
        {
            Application.Current.Shutdown();
        }
        else
        {
            e.Cancel = true;
        }
        _isConfirmed = true;
    }
}