Top-Level Forms Sample

To get samples and instructions for installing them, see the following:

  • Click Samples on the Visual Studio Help menu.

    For more information, see Locating Sample Files.

  • The most recent versions and the complete list of samples are available on the Visual Studio 2008 Samples Web site.

  • You can also locate samples on your computer's hard disk. By default, samples and a Readme file are copied to a folder under \Program Files\Visual Studio 9\Samples\. For Visual Studio Express Editions, all samples are located on the Internet.

To run this sample

  1. Press F5.

  2. Create multiple document windows using the New command on the File menu and verify that all the documents appear in the taskbar.

  3. Use the Task Manager to verify that only one process is running for the application.

Demonstrates

Many document-based applications support multiple windows, one per open document, which are displayed in the taskbar. The ALT+TAB key combination can be used to switch between the documents, but without the overhead incurred by loading each document in a separate process. This sample implements this behavior.

The startup object for the application is the Sub Main method inside a class named FormsManager. The Main method creates a new form instance by calling the NewForm method of the FormsManager class and then adds the new form to a list object. Once the first form is created, the code calls Application..::.Run to start the main application thread so that closing the first form does not shut down the process. Each document form exposes a New menu item. Clicking this menu item calls NewForm method again, which opens a new document form just as the Sub Main did as the application loaded.

Each form also provides Close and Exit menu items. Choosing Close starts the closing process. Each form has a Closing event which allows the form's code to check whether the document contents have been modified and, if so, to ask the user whether to save or not.

If you try to close a changed document by closing the form or exiting the application, you will see a dialog box asking you to save the form's contents. If you click Yes, the code calls the form's Save method and closes the form. If you click No, the form simply closes. If you click Cancel, the form does not close and a custom event is raised telling the application to stop shutting down. This application does not actually perform any file I/O to save the file.