BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Windows Presentation Foundation
.NET 4.0+

WPF Controls - Window - Display and Activation

The one hundred and sixty-ninth part of the Windows Presentation Foundation Fundamentals tutorial continues to describe the Window class. This article looks at the members related to opening, closing, activating and deactivating windows.

Closing Windows

You could rely on the user to close windows using the [X] at the top corner, pressing Alt-F4, or other methods. However, it is common to wish to close a window programmatically. This may be for a simple reason, such as responding to the user clicking a Close or Exit menu option, or be a less usual action, such as closing one window from another's code.

To close a window from code, you call its Close method. Let's demonstrate by calling the method when the "Close" button in the secondary window is clicked. We'll make the window close itself, leaving any other windows unaffected.

First, modify the XAML for the Close button to capture clicks:

<Button Click="Close_Click">Close</Button>

Add the following code to the window. This calls Close against the current instance. The "this" keyword is optional, as Close is an inherited method of Window. I've included "this" in this code sample for readability.

private void Close_Click(object sender, RoutedEventArgs e)
{
    this.Close();
}

If you run the program and open some secondary windows you should find that you can now close them using the Close button in the window's content area.

Detecting a Window Closing

You can detect when a window is closing, either because of user actions or after calling Close, using two events. The Closing event is raised first, after the process of closing has started but whilst the window is still open and visible. This allows you to perform actions before the window closes, such as retrieving information or asking the user if they are sure they wish to exit. The event is raised with event arguments in a CancelEventArgs object. This includes the Cancel property. If you set Cancel to true, the process is cancelled and the window does not close.

The Closed event is raised after Closing. At this point, the closing process cannot be stopped.

Let's capture the Closing event of the second window from within the code of the main window. We'll detect when a secondary window closes, read the text from the text box and display it in the main window's text block. Start my modifying the existing Open_Click method to connect to the event.

private void Open_Click(object sender, RoutedEventArgs e)
{
    var secondWindow = new SecondWindow();
    secondWindow.Closing += new CancelEventHandler(secondWindow_Closing);
    secondWindow.Show();
}

Now create the code to run in response to the event. Note that the window that raises the event is passed into the method using the sender parameter. After this is converted to a SecondWindow, we can access the Information text box and extract the Text property.

void secondWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
    var secondWindow = sender as SecondWindow;
    Information.Text = secondWindow.EnteredText.Text;
}

Run the program to try it. Open a second window and type some text into the text box. When you click the "Close" button, the window will close and the entered text will be transferred to the main window.

Let's modify the event handling code to try out the cancellation process. We'll again obtain the text entered into the text box. This time, we'll check if the returned string is empty. If it is, we'll set the Cancel property of the event arguments to true to prevent the window from closing.

void secondWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
    var secondWindow = sender as SecondWindow;
    string enteredText = secondWindow.EnteredText.Text;

    if (enteredText.Length > 0)
    {
        Information.Text = enteredText;
    }
    else
    {
        e.Cancel = true;
    }
}

Run the program again. Open a secondary window and type some text into the text box. Closing the window will copy the text to the main window's text block. Open another secondary window and try to close it whilst the text box is empty. The window will not close.

Hiding a Window

Once a window is closed, it is disposed and no longer accessible. In many cases this is acceptable. In situations where a window takes a long time to initialise, is reusable and only one instance can be shown at a time, you may decide instead to hide it. A hidden window has its Visibility set to Hidden. It is removed from the screen but is not disposed, so can later be displayed again using the Show method.

To hide a window in this manner, call its Hide method.

30 June 2015