Top-Level Forms Sample
This sample demonstrates an application with multiple top-level forms, similar to Microsoft Word.
To get samples and instructions for installing them
Do one or more of the following:
On the Help menu, click Samples.
The Readme displays information about samples.
Visit the Visual Studio 2008 Samples Web site. The most recent versions of samples are available there.
Locate samples on the computer on which Visual Studio is installed. By default, samples and a Readme file are installed in drive:\Program Files\Microsoft Visual Studio 9.0\Samples\lcid. For Express editions of Visual Studio, all samples are located online.
For more information, see Visual Studio Samples.
Security Note: |
---|
This sample code is intended to illustrate a concept, and it shows only the code that is relevant to that concept. It may not meet the security requirements for a specific environment, and it should not be used exactly as shown. We recommend that you add security and error-handling code to make your projects more secure and robust. Microsoft provides this sample code "AS IS" with no warranties. |
To run this sample
Press F5.
Create multiple document windows using the New command on the File menu and verify that all the documents appear in the taskbar.
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.