Events
Mar 17, 11 PM - Mar 21, 11 PM
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
In this article, you'll learn about the different ways to close a window or dialog box. A user can close a window by using the elements in the non-client area, including the following:
true
on a modal window.When designing a window, provide more mechanisms to the client area to close a window. Some of the common design elements on a window that are used to close it include the following:
Important
Once a window is closed, the same object instance can't be used to reopen the window.
For more information about the life of a window, see Overview of WPF windows: Window lifetime.
When closing a window that was opened with the ShowDialog method, set the DialogResult property to either true
or false
to indicate an "accepted" or "canceled" state, respectively. As soon as the DialogResult
property is set to a value, the window closes. The following code demonstrates setting the DialogResult
property:
private void okButton_Click(object sender, RoutedEventArgs e) =>
DialogResult = true;
private void cancelButton_Click(object sender, RoutedEventArgs e) =>
DialogResult = false;
Private Sub okButton_Click(sender As Object, e As RoutedEventArgs)
DialogResult = True
End Sub
Private Sub cancelButton_Click(sender As Object, e As RoutedEventArgs)
DialogResult = False
End Sub
You can also call the Close method. If the Close
method is used, the DialogResult property is set to false
.
Once a window has been closed, it can't be reopened with the same object instance. If you try to show the same window, a InvalidOperationException is thrown. Instead, create a new instance of the window and open it.
When closing a window that was opened with the Show method, use the Close method. The following code demonstrates closing a modeless window:
private void closeButton_Click(object sender, RoutedEventArgs e) =>
Close();
Private Sub closeButton_Click(sender As Object, e As RoutedEventArgs)
Close()
End Sub
The Button.IsCancel property can be set to true
to enable the ESC key to automatically close the window. This only works when the window is opened with ShowDialog method.
<Button Name="cancelButton" IsCancel="True">Cancel</Button>
Instead of closing a window, a window can be hidden with the Hide method. A hidden window can be reopened, unlike a window that has been closed. If you're going to reuse a window object instance, hide the window instead of closing it. The following code demonstrates hiding a window:
private void saveButton_Click(object sender, RoutedEventArgs e) =>
Hide();
Private Sub saveButton_Click(sender As Object, e As RoutedEventArgs)
Hide()
End Sub
If you've designed your buttons to hide a window instead of closing it, the user can still bypass this and close the window. The Close item of the system menu and the Close button of the non-client area of the window, will close the window instead of hiding it. Consider this scenario when your intent is to hide a window instead of closing it.
Caution
If a window is shown modally with ShowDialog, the DialogResult property will be set to null
when the window is hidden. You'll need to communicate state back to the calling code by adding your own property to the window.
When a window is closed, the Closing event is raised. The handler is passed a CancelEventArgs, which implements the Cancel property. Set that property to true
to prevent a window from closing. The following code demonstrates how to cancel the closure and instead hide the window:
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
// Cancel the closure
e.Cancel = true;
// Hide the window
Hide();
}
Private Sub Window_Closing(sender As Object, e As ComponentModel.CancelEventArgs)
' Cancel the closure
e.Cancel = True
' Hide the window
Hide()
End Sub
There may be times where you don't want to hide a window, but actually prevent the user from closing it. For more information, see Overview of WPF windows: Cancel window closure.
.NET Desktop feedback feedback
.NET Desktop feedback is an open source project. Select a link to provide feedback:
Events
Mar 17, 11 PM - Mar 21, 11 PM
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register now