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.Close Method

Manually closes a Window.

Namespace: System.Windows
Assembly: PresentationFramework (in presentationframework.dll)

public void Close ()
public void Close ()
public function Close ()
You cannot use methods in XAML.

A Window can be closed using one of several, well-known, system-provided mechanisms located in its title bar, including:

  • ALT-F4.

  • System Menu | Close.

  • Close button.

A Window can also be closed using one of several well-known mechanisms within the client area that are provided by developers, including:

  • File | Exit on a main window.

  • File | Close or a Close button on a child window.

NoteNote:

OK and Cancel buttons on a dialog box are also developer-provided, although will likely set DialogResult, which automatically closes a window that was opened by calling ShowDialog.

These mechanisms require you to explicitly call Close to close a window.

NoteNote:

If a window, opened by calling ShowDialog, and with a Button with its IsCancel property set to true, will automatically close when the button is either clicked, or ESC is pressed. If the window was opened using Show, however, Close must be explicitly called, such as from Click event handler for the Button.

Closing a window causes the Closing event to be raised. If the Closing event isn't cancelled, the following occurs:

  • The Window is removed from Windows.

  • The Window is removed from the owner Window if the owner/owned relationship was established before the owned Window was shown and after the owner Window was opened.

  • The Closed event is raised.

  • Unmanaged resources created by the Window are disposed.

  • If ShowDialog was called to show the Window, ShowDialog returns.

Closing a Window will cause any windows that it owns to be closed. Furthermore, closing a Window may cause the application to stop running, depending on how the ShutdownMode property of the Application object was configured.

NoteNote:

This method cannot be called when a window is hosted in a browser.

The following example shows a File | Exit menu being handled to explicitly call Close.

<MenuItem Name="fileExitMenuItem" Header="E_xit" Click="fileExitMenuItem_Click"></MenuItem>

void fileExitMenuItem_Click(object sender, RoutedEventArgs e)
{
    // Close this window
    this.Close();
}

  • UIPermission  for permission to use all windows and user input events without restriction. Associated enumeration: AllWindows

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