Window.Closing Event

Definition

Occurs directly after Close() is called, and can be handled to cancel window closure.

public event System.ComponentModel.CancelEventHandler Closing;

Event Type

Exceptions

Visibility is set, or Show(), ShowDialog(), or Close() is called while a window is closing.

Examples

The following example demonstrates a Window that determines whether it needs user intervention to close.

<Window 
  x:Class="CSharp.DataWindow"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Closing="DataWindow_Closing"  
    >
  <Grid>
    <TextBox Name="documentTextBox" AcceptsReturn="True" AcceptsTab="True" TextChanged="documentTextBox_TextChanged"></TextBox>
  </Grid>
</Window>
using System; // EventArgs
using System.ComponentModel; // CancelEventArgs
using System.Windows; // window

namespace CSharp
{
    public partial class DataWindow : Window
    {
        // Is data dirty
        bool isDataDirty = false;

        public DataWindow()
        {
            InitializeComponent();
        }

        void documentTextBox_TextChanged(object sender, EventArgs e)
        {
            this.isDataDirty = true;
        }

        void DataWindow_Closing(object sender, CancelEventArgs e)
        {
            MessageBox.Show("Closing called");

            // If data is dirty, notify user and ask for a response
            if (this.isDataDirty)
            {
                string msg = "Data is dirty. Close without saving?";
                MessageBoxResult result = 
                  MessageBox.Show(
                    msg, 
                    "Data App", 
                    MessageBoxButton.YesNo, 
                    MessageBoxImage.Warning);
                if (result == MessageBoxResult.No)
                {
                    // If user doesn't want to close, cancel closure
                    e.Cancel = true;
                }
            }
        }
    }
}

Remarks

Closing can be handled to detect when a window is being closed (for example, when Close is called). Furthermore, Closing can be used to prevent a window from closing. To prevent a window from closing, you can set the Cancel property of the CancelEventArgs argument to true.

The Closing event is raised when Close is called, if a window's Close button is clicked, or if the user presses ALT+F4.

If an owned window was opened by its owner window using Show, and the owner window is closed, the owned window's Closing event is not raised. If the owner of a window is closed (see Owner), Closing is not raised on the owned window.

If Shutdown is called, the Closing event for each window is raised. However, if Closing is canceled, cancellation is ignored.

If a session ends because a user logs off or shuts down, Closing is not raised; handle SessionEnding to implement code that cancels application closure.

If you want to show and hide a window multiple times during the lifetime of an application, and you don't want to reinstantiate the window each time you show it, you can handle the Closing event, cancel it, and call the Hide method. Then, you can call Show on the same instance to reopen it.

Applies to

Product Versions
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

See also