Hide a Window Without Closing Sample

.NET Framework 3.5

Updated: July 2008

This sample illustrates how to show and hide a window multiple times during the lifetime of an application while using only a single window instance.

The main window uses a class-scoped variable to store a reference to a child window that is instantiated the first time it is opened. When the child window is closed, its Closing event handler cancels closure and hides the window by calling the Hide method. When the window is hidden, the main window becomes active again. Rather than reinstantiating the child window to open it again, the Show method is called on the current child window instance.

This pattern can be useful from a performance perspective. For example, some dialog boxes (such as color-picker dialog boxes) are initialization intensive and consequently slower to load. To avoid the initialization and performance hit every time such a dialog box is opened, you can use this pattern to avoid the initialization that is usually performed during construction or by Loaded and ContentRendered event handlers.

This sample demonstrates a specific feature of the Windows Presentation Foundation and, consequently, does not follow application development best practices. For comprehensive coverage of Windows Presentation Foundation (WPF) and Microsoft .NET Framework application development best practices, refer to the following as appropriate:

Accessibility - Accessibility Best Practices

Localization - WPF Globalization and Localization Overview

Performance - Optimizing WPF Application Performance

Security - Windows Presentation Foundation Security

Download sample

  • Install the Windows Software Development Kit (SDK) and open its build environment command window. On the Start menu, point to All Programs, Microsoft Windows SDK, and then click CMD Shell.

  • Download the sample, usually from the software development kit (SDK) documentation, to your hard disk drive.

  • To build the sample from the build environment command window, go to the source directory of the sample. At the command prompt, type MSBUILD.

  • To build the sample in Microsoft Visual Studio, load the sample solution or project file and then press CTRL+SHIFT+B.

  • To run the compiled sample from the build environment command window, execute the .exe file in the Bin\Debug or Bin\Release folder contained under the sample source code folder.

  • To run the compiled sample with debugging in Visual Studio, press F5.

Date

History

Reason

July 2008

Added topic.

Information enhancement.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft