Opening a child form
The Open menu in the parent form should work much like the now hidden Open menu for the MainForm class. The handler for this menu should display an Open-FileDialog and create a new child window containing the selected album. To create the MainForm instance, we will create a new constructor that accepts an album file name with which to initialize the window.
The code required here is nothing new to us, so let's get to it.
|
IMPLEMENT HANDLER FOR OPEN MENU IN PARENT FORM | ||||||||||||||||||||||||||
|
ACTION |
RESULT | |||||||||||||||||||||||||
|
1 |
Add a using statement for our library at the start of the ParentForm.cs code window. |
using Manning.MyPhotoAlbum; | ||||||||||||||||||||||||
|
2 |
Add a Click handler for the Open menu in the ParentForm.cs [Design] window. |
private void menuOpen Click (object sender, System.EventArgs e) { | ||||||||||||||||||||||||
|
3 |
Implement this handler to display an OpenFile-Dialog instance from which to select an album. |
// Allow user to select a new album using (OpenFileDialog dlg = new OpenFileDialog()) { dlg.Title = "Open Album"; dlg.Filter = "abm files (*.abm)|" + "*.abm|All files (*.*)|*.*"; dlg.InitialDirectory = PhotoAlbum.DefaultDir; dlg.RestoreDirectory = true; if (dlg.ShowDialog() == DialogResult.OK) { | ||||||||||||||||||||||||
|
4 |
If an album is selected, try to open the file in a new window. How-to Use a not-yet-implemented constructor that accepts an album file. |
// Open new child window for the album MainForm form = new MainForm(dlg.FileName); form.MdiParent = this; form.Show();
The code displays an open file dialog and creates a child window using the selected album file. This code requires a new constructor for the MainForm class, namely one that accepts the file name of a photo album. In this new constructor, we would like to make use of the constructor code already present in the existing constructor. We can do this in C# by simply invoking the default constructor with the this keyword. The following table illustrates this syntax, and the changes required for our new constructor.
These changes permit the ParentForm class to create a new child window containing an open album. Compile and run the application to verify that this works as expected. The File menu from our two classes is now fully merged, and all menus are fully implemented. As can be seen from this discussion, the ability to merge menus provides a powerful mechanism for controlling the menu bar in MDI applications. They permit the exact placement of menu items, and control over which class, the parent or child, will process each item. While we only merged a single menu here, you may find in your own MDI applications that multiple menus must be merged. The principles and methods for doing this are identical to those utilized here. With our menus completed, the next item in the development of our MDI application is to tidy up other parts of the interface such as the toolbar and the pixel data dialog. This cleanup is our next topic. 16.4 MDI children Our MDI application is coming along nicely. So far we have a parent form that contains MainForm class instances as child forms. Each form displays a new or existing album, and the menu bars have been integrated to present a logical set of choices for the user. There are additional members of the Form class that are related to the creation of MDI applications. This section will examine a few of these members as we correct some issues with our MyPhotos MDI application. If you have experimented with the MyPhotos interface created in the previous section, you may have found the following three issues that do not behave as you might expect. • The toolbar control. The toolbar on the child form gives access to the New and Open menu in the MainForm class, which we are trying not to expose in the MDI version of our application. • The pixel data form. This dialog appears separate from the MDI application, rather than as a child form within it. In addition, when multiple album windows are open, each window opens its own separate PixelDlg form, which can get rather confusing. • Opening multiple albums. If you open the same album twice, you end up with two windows both showing the same album. Aside from the errors that can occur from having two instances operate on different versions of the same album simultaneously, it seems a bit strange to permit two copies of the same file to open in the same parent window. We will address each of these items separately, and make use of MDI-related members of the Form class as required. | ||||||||||||||||||||||||
Post a comment