Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Window.Closing Event

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

Namespace: System.Windows
Assembly: PresentationFramework (in presentationframework.dll)
XML Namespace:  http://schemas.microsoft.com/winfx/2006/xaml/presentation

public event CancelEventHandler Closing
/** @event */
public void add_Closing (CancelEventHandler value)

/** @event */
public void remove_Closing (CancelEventHandler value)

In JScript, you can handle the events defined by a class, but you cannot define your own.
<object Closing="CancelEventHandler" .../>

Exception typeCondition

InvalidOperationException

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

  • 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.

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

Closing provides a mechanism by which a window can detect when Close is called, and to optionally stop the window from closing. To cancel window closure, you can set Cancel property of the CancelEventArgs object that is passed to the Closing event handler to true.

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

If the owner of a window is closed (see Owner), Closing is not raised on the owned window.

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.

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"  
    >

	...

</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;

	...

        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;
                }
            }
        }
    }
}

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft