
- •brief contents
- •about this book
- •The Windows Forms namespace
- •Part 1: Hello Windows Forms
- •Part 2: Basic Windows Forms
- •Part 3: Advanced Windows Forms
- •Who should read this book?
- •Conventions
- •Action
- •Result
- •Source code downloads
- •Author online
- •acknowledgments
- •about .NET
- •Casting the .NET
- •Windows Forms overview
- •about the cover illustration
- •Hello Windows Forms
- •1.1 Programming in C#
- •1.1.1 Namespaces and classes
- •1.1.2 Constructors and methods
- •1.1.3 C# types
- •1.1.4 The entry point
- •1.1.5 The Application class
- •1.1.6 Program execution
- •1.2 Adding controls
- •1.2.1 Shortcuts and fully qualified names
- •1.2.2 Fields and properties
- •1.2.3 The Controls property
- •1.3 Loading files
- •1.3.1 Events
- •1.3.2 The OpenFileDialog class
- •1.3.3 Bitmap images
- •1.4 Resizing forms
- •1.4.1 Desktop layout properties
- •1.4.2 The Anchor property
- •1.4.3 The Dock property
- •1.5 Recap
- •2.1 Programming with Visual Studio .NET
- •2.1.1 Creating a project
- •Action
- •Result
- •2.1.2 Executing a program
- •Action
- •Result
- •2.1.3 Viewing the source code
- •View the code generated by Visual Studio .NET
- •Action
- •Result
- •2.2 Adding controls
- •2.2.1 The AssemblyInfo file
- •Action
- •Results
- •2.2.2 Renaming a form
- •Action
- •Result
- •2.2.3 The Toolbox window
- •Action
- •Result
- •2.3 Loading files
- •2.3.1 Event handlers in Visual Studio .NET
- •Action
- •Result
- •2.3.2 Exception handling
- •Action
- •Result
- •Action
- •Results and Comments
- •2.4 Resizing forms
- •2.4.1 Assign the Anchor property
- •Action
- •Result
- •2.4.2 Assign the MinimumSize property
- •Action
- •Result
- •2.5 Recap
- •Basic Windows Forms
- •Menus
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •3.3 Click events
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •3.5 Context menus
- •Action
- •Result
- •Action
- •Result
- •3.6 Recap
- •Status bars
- •4.1 The Control class
- •4.2 The StatusBar class
- •Action
- •Result
- •Action
- •Result
- •4.3.1 Adding panels to a status bar
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •4.5 Recap
- •Reusable libraries
- •5.1 C# classes and interfaces
- •5.2 Class libraries
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •5.3 Interfaces revisited
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •5.4 Robustness issues
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Common file dialogs
- •Action
- •Results
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •6.3 Paint events
- •Action
- •Result
- •Action
- •Result
- •6.4 Context menus revisited
- •Action
- •Result
- •Action
- •Result
- •6.5 Files and paths
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •6.6 Save file dialogs
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •6.7 Open file dialogs
- •Action
- •Result
- •Action
- •Result
- •6.8 Recap
- •Drawing and scrolling
- •7.1 Form class hierarchy
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •7.4 Panels
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Dialog boxes
- •8.1 Message boxes
- •Action
- •Result
- •Action
- •Result
- •8.1.4 Creating A YesNoCancel dialog
- •Action
- •Result
- •Action
- •Result
- •8.2 The Form.Close method
- •8.2.1 The relationship between Close and Dispose
- •Action
- •Result
- •8.3 Modal dialog boxes
- •Action
- •Result
- •Action
- •Result
- •8.3.2 Preserving caption values
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Basic controls
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •9.1.2 Creating a derived form
- •Action
- •Result
- •9.2 Labels and text boxes
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •9.3.6 Adding AlbumEditDlg to our main form
- •Action
- •Result
- •Action
- •Result
- •9.4 Recap
- •List controls
- •10.1 List boxes
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •10.2 Multiselection list boxes
- •10.2.1 Enabling multiple selection
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •10.3 Combo boxes
- •Action
- •Result
- •Action
- •Result
- •10.4 Combo box edits
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •10.5 Owner-drawn lists
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •More controls
- •Action
- •Result
- •Action
- •Result
- •11.2 Tab pages
- •Action
- •Result
- •Action
- •Result
- •11.3.1 Dates and times
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •11.5 Recap
- •A .NET assortment
- •12.1 Keyboard events
- •Action
- •Result
- •Action
- •Result
- •12.2 Mouse events
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •12.3 Image buttons
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •12.4 Icons
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •12.5 Recap
- •Toolbars and tips
- •13.1 Toolbars
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •13.4.2 Creating tool tips
- •Action
- •Result
- •Action
- •Result
- •Advanced Windows Forms
- •List views
- •14.2 The ListView class
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •14.2.3 Populating a ListView
- •Action
- •Result
- •Action
- •14.3 ListView columns
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •14.6 Recap
- •Tree views
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •15.3 Dynamic tree nodes
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •15.4 Node selection
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •15.5 Fun with tree views
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Multiple document interfaces
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •16.3 Merged menus
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •16.4 MDI children
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •16.5 MDI child window management
- •Action
- •Result
- •Action
- •Result
- •16.6 Recap
- •Data binding
- •17.1 Data grids
- •Action
- •Result
- •Action
- •Result
- •17.2 Data grid customization
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Odds and ends .NET
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •18.2 Timers
- •Action
- •Result
- •Action
- •Result
- •18.3 Drag and drop
- •Action
- •Result
- •Action
- •Result
- •18.4 ActiveX controls
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •18.5 Recap
- •C# primer
- •A.1 C# programs
- •A.1.1 Assemblies
- •A.1.2 Namespaces
- •A.2 Types
- •A.2.1 Classes
- •A.2.2 Structures
- •A.2.3 Interfaces
- •A.2.4 Enumerations
- •A.2.5 Delegates
- •A.3 Language elements
- •A.3.1 Built-in types
- •A.3.2 Operators
- •A.3.3 Keywords
- •A.4 Special features
- •A.4.1 Exceptions
- •A.4.2 Arrays
- •A.4.3 Main
- •A.4.4 Boxing
- •A.4.5 Documentation
- •.NET namespaces
- •B.1 System.Collections
- •B.2 System.ComponentModel
- •B.3 System.Data
- •B.4 System.Drawing
- •B.5 System.Globalization
- •B.6 System.IO
- •B.7 System.Net
- •B.8 System.Reflection
- •B.9 System.Resources
- •B.10 System.Security
- •B.11 System.Threading
- •B.12 System.Web
- •B.13 System.Windows.Forms
- •B.14 System.XML
- •Visual index
- •C.1 Objects
- •C.2 Marshal by reference objects
- •C.3 Components
- •C.4 Common dialogs
- •C.7 Event data
- •C.8 Enumerations
- •For more information
- •bibliography
- •Symbols
- •Index

C H A P T E R |
1 6 |
|
|
Multiple document interfaces
16.1 |
Interface styles 526 |
16.4 |
MDI children 543 |
16.2 |
MDI forms 530 |
16.5 |
MDI child window management 557 |
16.3 |
Merged menus 535 |
16.6 |
Recap 563 |
The ListView and TreeView classes discussed in chapters 14 and 15 present a collection of objects within a single list or tree control. These are especially useful when creating an explorer-style interface such as our MyAlbumExplorer application, or the common Windows Explorer application. Another kind of interface is the multiple document interface, also called an MDI (normally pronounced em-dee-eye).
An MDI application presents a collection of forms within a single application window. We will discuss MDI applications through the following discussion areas:
•Understanding various interface styles.
•Creating an MDI container window.
•Converting an SDI application into an MDI application.
•Using MDI-related class members of various controls.
•Merging two menus into a single merged menu.
•Managing menus and forms in an MDI application.
These topics will be covered as we progress through the chapter, beginning with the concept of interface styles.
525

16.1INTERFACE STYLES
Before we discuss exactly how multiple document interfaces are created, let’s take a step back and consider the various types of application interfaces used for Windows applications. Most Windows applications fall into one of three interface categories:
•Single document interfaces.
•Explorer interfaces.
•Multiple document interfaces.
We will discuss each type of interface separately.
16.1.1SINGLE DOCUMENT INTERFACES
A single document interface, also called an SDI, is an interface that displays a single document or other encapsulated data within a single form. Our MyPhotos application, as shown in figure 16.1, is a good example of this style, in which a single photo album is displayed. The user can look at multiple photo albums only by examining one after another. The contents of two albums cannot be compared unless two copies of the program are running.
In the Windows operation system, the Notepad and WordPad applications provide additional examples of the SDI style.
Figure 16.1
Our single document interface displays one photo album at a time.
16.1.2EXPLORER INTERFACES
The MyAlbumExplorer application built in chapters 14 and 15 is an example of an explorer interface, and can be seen in figure 16.2. In this style, a hierarchy of information is presented to the user. Normally a TreeView control displays this hierarchy, typically on the left, with details on the selected node provided in a ListView control. Sometimes the TreeView control can be hidden, and sometimes it is always
526 |
CHAPTER 16 MULTIPLE DOCUMENT INTERFACES |

present. Alternate information may appear on the list side of the window as well, such as the photographic image we displayed in chapter 15 for a selected photograph in the MyAlbumExplorer application.
In Windows, of course, the Windows Explorer application is another example of this style.
Figure 16.2 Our explorer interface presents the collection of photo albums in list form.
16.1.3MULTIPLE DOCUMENT INTERFACES
A multiple document interface (MDI) allows multiple views of one or more documents or other encapsulated data to be displayed at the same type. This permits alternate views of the same data, or separate presentations of the same style of data, within a single window. For example, a stock market MDI application might present different historical or graphical views of a single portfolio, each as a separate window. Alternately, such an application might present multiple portfolios, each as its own window within a containing application window.
In the original conception of this style, a single window acted as a container for other windows, where each contained window displayed a specific instance or view of a type of data. More recently, well-known MDI applications such as Microsoft Word and Excel have taken the approach of displaying all of their windows directly on the desktop, each within a separate application window, while still preserving an MDI look and feel from the menu bar and other parts of the interface. This relatively new style, the Multiple Single Document Interface, or MSDI, is consistent with the manner in which Web browsers have typically worked. While an MSDI interface can be created in Visual Studio.NET, it is not necessarily an easy task.
INTERFACE STYLES |
527 |

Also note that Visual Studio .NET, while providing an MDI-like interface, uses more of a TabControl look and feel for the set of displayed windows, or what might be called a Multiple Tabbed Documents Interface, or MTDI. In this style, multiple sets of windows are displayed as horizontal or vertical groups of tabs. Both the MSDI and MTDI approaches can be created using the .NET Framework as an alternative to the traditional MDI interface, although there is not really any direct support for these newer interfaces. As a result, implementing such interfaces requires much more effort from the developer.
For our purposes, a traditional MDI application provides the means to discuss and demonstrate the manner in which the .NET Framework supports such applications. We will convert the existing MyPhotos application into the MDI application shown in figure 16.3. As you can see, this application will incorporate the Form classes we have created in part 2 of this book.
Figure 16.3 Our multiple document interface, created in this chapter, displays a selected set of photo albums within a single window.
The reuse of our existing classes is possible because of the manner in which the Form class in general and MDI support in particular is integrated into the Windows Forms hierarchy. As we discussed in chapter 7, a Form object is a Control instance that happens to display an application window. For MDI applications, Form controls are contained by a parent Form. Of course, the contained forms can be resized and moved within their container, and can still display menus, toolbars, status bars, and other controls. As we shall see, the relationship between MDI parent and child forms is different than the relationship between control containers and controls.
528 |
CHAPTER 16 MULTIPLE DOCUMENT INTERFACES |
16.1.4SUPPORT IN WINDOWS FORMS
To provide some insight and perhaps some perspective on MDI applications, the following table lists a number of class members specific to the implementation of MDI applications in the .NET Framework. Of course, these members can be used for other purposes, and additional properties, methods, and events are certainly used in MDI applications. These events highlight many of the MDI-specific tasks that are often performed in this style interface. The table provides a short description of each member and a reference to the section in this chapter where more information on each item may be found.
Class members often used in MDI applications
Class |
Member |
Member name |
Description |
See |
|
type |
section |
||||
|
|
|
|||
|
|
|
|
|
|
|
Properties |
ActiveMdiChild |
Gets the MDI child window that is currently |
16.4.1 |
|
|
|
|
active. |
|
|
|
|
IsMdiChild |
Gets whether the form is an MDI child. |
16.3.2 |
|
|
|
IsMdiContainer |
Gets whether the form is an MDI container |
16.2.1 |
|
|
|
|
form. |
|
|
|
|
MdiChildren |
Gets the set of MDI children contained by |
16.4.3 |
|
|
|
|
this form as an array of Form objects. |
|
|
|
|
MdiParent |
Gets or sets the MDI container for this form. |
16.2.2 |
|
Form |
|
|
If set, then this form is an MDI child form. |
|
|
|
|
|
|
||
|
|
MergedMenu |
Gets the MainMenu object representing the |
16.3 |
|
|
|
|
current merged menu for an MDI container |
|
|
|
|
|
form. |
|
|
|
Methods |
LayoutMdi |
Arranges the MDI children within this form |
16.5.1 |
|
|
|
|
using a given layout style. |
|
|
|
Events |
MdiChildActivate |
Occurs when an MDI child form is activated |
16.4.4 |
|
|
|
|
or deactivated within an MDI application. |
|
|
|
|
|
Note that MDI children do not receive the |
|
|
|
|
|
Activated and Deactivate events. |
|
|
|
|
|
|
|
|
|
Properties |
MdiListItem |
Gets the MenuItem object contained by this |
16.5.2 |
|
|
|
|
menu that displays a list of MDI child forms |
|
|
Menu |
|
|
for the associated form object. |
|
|
|
|
|
|
||
|
Methods |
MergeMenu |
Merges the MenuItem objects in a given |
16.3 |
|
|
|
|
menu with those contained by this menu. |
|
|
|
|
|
|
|
|
|
Properties |
MdiList |
Gets or sets whether this menu should be |
16.5.2 |
|
|
|
|
populated with a list of MDI child forms |
|
|
|
|
|
contained by the associated form. |
|
|
MenuItem |
|
MergeOrder |
Gets or sets the relative position of this menu |
16.3.2 |
|
|
|
item when it is merged with another menu. |
|
||
|
|
|
|
||
|
|
MergeType |
Gets or sets how this menu should be |
16.3.1 |
|
|
|
|
merged with other menus. The default is |
|
|
|
|
|
MergeType.Add. |
|
|
|
|
|
|
|
INTERFACE STYLES |
529 |

Also note that the behaviors of desktop-related actions within an MDI child form are modified. For example, the Minimize and Maximize buttons on the title bar work within the parent window, rather than on the desktop itself.
In the rest of this chapter we will enhance our MyPhotos application to support a multiple document interface. We begin with the MDI container form.
16.2MDI FORMS
So let’s convert our existing MyPhotos application into an MDI application. This initial work is not as difficult as you might think. Generally, we need one Form to act as the top-level container, and the ability to create other forms as children within this container. Here, we will do this via the following tasks:
1Create a new parent form for the application to act as the MDI container.
2Add a menu bar and New menu item to create MDI child forms.
3Define a new Main method in the parent as the entry point for the application.
Of course, there will be other work to perform to clean up the behavior of our application. These steps will get us going, and subsequent sections will deal with other required changes. Figure 16.4 shows how our application will look by the end of this section. Note in particular the two File menus. We will address this issue in the next section while discussing Merged Menus.
Figure 16.4 Note the two File menus for this window. The menus from both our ParentForm and MainForm classes appear separately on the menu bar. We will address this in section 16.3.
530 |
CHAPTER 16 MULTIPLE DOCUMENT INTERFACES |