Top-Level Forms Sample
This sample demonstrates an application with multiple top-level forms, similar to Microsoft Word.
This sample code is provided to illustrate a concept and should not be used in applications or Web sites, as it may not illustrate the safest coding practices. Microsoft assumes no liability for incidental or consequential damages should the sample code be used for purposes other than as intended.
To open the sample file in Solution Explorer
Click Download Sample.
The File Download message box appears.
Click Open, and on the left column of the zip folder window, click Extract all files.
The Extraction Wizard opens.
Click Next. You can change the directory that the files will be extracted to, and then click Next again.
Make sure that the Show extracted files check box is selected, and click Finish.
Double-click the sample's .sln file.
The sample solution is displayed in Solution Explorer. You might get a security warning that says the solution location is not trusted. Click OK to continue.
To run this sample
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.
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 callsto 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 aevent 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.