Visual Basic Expert Solutions

book coverQUE

Chapter 2

Multiple Document Interface (MDI)

By Steve Potts


The Multiple Document Interface (MDI) is a powerful feature of Windows programming that is fully supported in Visual Basic 4. MDI programs differ from ordinary programs in that a hierarchical relationship exists between one of the forms, the MDI form, and some or all of the other forms, instead of the common peer-to-peer form relationships in ordinary Visual Basic programs. These subordinate forms are called child forms.

In a MDI program, one form is designated as the MDI form, also known as the parent form. This form provides the workspace for the rest of the application's forms. The most common use of MDI is to provide multiple documents of the same type without having to decide how many of them to create in advance.

This chapter describes the distinguishing features of MDI applications and possible uses of this technology. The topics covered in this chapter are:

At the end of the chapter, you will see some of the uses of MDI technology in two sample applications.

MDI Application Behavior

Programs like Microsoft Word and Excel are MDI applications. When using Microsoft Word, a user creates a document. He may also want to create additional documents without closing the first one.

MDI child forms become windows at runtime and obey the following rules:

These rules allow for a tighter integration between child windows and their parent than would be possible using simple peer forms.

Using Visual Basic to Create MDI Applications

Visual Basic 4 fully supports the creation of MDI applications. The following example creates a very simple MDI program.

To create a MDI application, follow these steps:

1. Choose New Project from the File menu.

2. Choose MDI Form from the Insert menu to create the parent form. A new form appears with the word "MDIForm1" in the title bar. The Project Window will now contain the original form and one MDI form, as shown in figure 2.1.

Fig. 2.1 The Project Window shows MDI forms differently.

  1. Observe that the icon next to the original form Form1 is different from the MDI form MDIForm1 in the Project Window. The MDI icon has an extra little window beside the big one. This lets you differentiate at a glance between the two types of forms.
  2. Set the MDIChild property of Form1 to True. This establishes the parent/child relationship between MDIForm1 and Form1. This occurs because only one MDI parent form can exist in an application. Therefore, if a form's MDIChild property is set to True, it must be a child of the one and only MDI form in the application. Notice in figure 2.2 that Form1's icon in the Project Window has changed from what it was in figure 2.1.

Fig. 2.2 The icon for Form1 has changed.

  1. The icon for Form1 looks a lot like the icon for MDIForm1. The only difference is that the large part of the icon is grayed out in the child form, and the small part of the icon is grayed out in the parent form.
  2. Run the application and prove to yourself that Form1 is contained within MDIForm1 and cannot be moved outside of its boundaries, except when you are in development mode.
  3. Not all forms in a MDI application have to be child forms. Your application may include standard forms also. To illustrate this, create a new form by choosing Form from the Insert menu. This will change the appearance of the Project window to look like figure 2.3.

Fig. 2.3 Non-child forms have different icons in the Project window.

Notice that Form2 has a traditional icon beside it in the Project window. This indicates that no parent-child relationship exists between Form2 and MDIForm1. Run the application again to prove that Form2 behaves differently from Form1 in that you can move it freely outside the work area of MDIForm1. To do this, you will need to add a command button to Form1 and add the following code to its event procedure:

Private Sub Command1_Click()

Form2.Show

End Sub

Observe that Form1 displayed inside of MDIForm1 immediately when you started the application. The reason for this is that Form1 is listed as the Startup Form in the Project Options dialog box, as shown in figure 2.4.

Fig. 2.4 The Project Options dialog box specifies which form appears first.

When Windows obtains the command to load a MDI child form, it automatically loads the MDI parent form so that the MDI rules are observed. When the MDI parent form is loaded as the Startup form, Windows does not need to load Form1 until instructed to do so by the code.

Managing Icons in MDI Applications

As stated earlier, MDI child forms live entirely within the boundaries of their MDI parent forms. This is true even when the child form becomes an icon. Note that Form2's MDIChild property was changed to True before figure 2.5 was created. You can place these icons that represent child forms anywhere in the MDI parent's window, as shown in figure 2.5.

Fig. 2.5 Place MDI child icons anywhere in the MDI parent form's workspace.

The behavior of the MDI parent form is interesting when it becomes an icon. The entire application collapses into one icon that is placed at the bottom of the screen. All of the MDI child windows and icons disappear, leaving only an icon for the MDI parent form. This can give the illusion that the child windows or processes have terminated. When the MDI parent form is restored, however, all of the child forms and icons return to their places on the MDI parent form exactly as they were before.

Using Object Variables

Thus far, the MDI applications that we've shown have been fairly simple and offer little advantage over simple forms. By using an Object variable, you can create any number of MDI child forms at runtime. This is important because it lets your users decide how many MDI child windows they need to open without having to worry about some arbitrary limitations that you've placed on them. For example, suppose you create a MDI parent form to perform simple word processing. Each document will be contained in one MDI child form. If you allow few of them, you risk user frustration when users can't open as many forms as they really need. If you make the number huge, you bloat the size of your application and hurt performance (see Chapter 21, "Optimizing VB Code," for more on this).

Visual Basic 4 provides a solution to this problem through a special kind of variable called the Object variable. An Object variable can be either a Form object or Control object. In order to create a Form object at runtime, the syntax is as follows:

Dim Form1 as New Form1

The New Keyword

This New keyword is used to create an instance of a specific form type. Notice that, in the previous section, the Dim statement referred to Form1, and not to the generic form. The reason for this is to specify which controls, properties, event procedures, and subroutines will be created for this form. Generic forms are not useful at runtime because they have nothing associated with them. In the case of a word processing application, the new MDI child forms that the user creates need to be word processing application forms, not just blank forms with no code.

Likewise, you can create an array of Form1 by the following syntax:

Dim FormArr(5) As New Form1

This code creates five forms at runtime. The following application creates an array of forms at runtime and displays them when the MDI parent form is loaded at startup:

  1. Create a new project by choosing New Project on the File menu.
  2. Create the MDI parent form by choosing MDI Form on the Insert menu.
  3. Change the MDIChild property of Form1 to True.
  4. Add a command button to Form1 with the following code:
    Private Sub Command1_Click()
    Form1.Caption = "Form X"
    End Sub

    1. Add the following code to the Form_Load event procedure on the MDI parent form:

    Private Sub MDIForm_Load()
    Dim Frm_Array(5) As New Form1
    Dim I As Integer
    For I = 1 To UBound(Frm_Array)
    Frm_Array(I).Show
    Next I
    End Sub
  1. Notice the use of the UBound function, which takes an array as its argument and returns the largest valid subscript for that array.
  2. Run the program.
  3. Click on a command button on one of the five forms that are arranged in a cascaded fashion. Observe that the caption in the Form on the upper-left changes but that none of the other forms' captions changes at all. This occurs because all of the command button event procedures refer to Form1 by name. The original Form1 is known by this name at runtime.

The Me Keyword

Me is a reserved word in Visual Basic 4 and is known to every subroutine and procedure in a form. For C++ programmers, it is the same as the This operator. When several instances of the same form exist, it represents the instance that is currently being executed in code. We can modify the New keyword example to take advantage of the Me keyword.

The following application creates and array of forms at runtime, displays them when the MDI parent form is loaded at Startup, and employs the Me keyword to allow each form's code to apply to itself:

  1. Create a new project by choosing New Project on the File menu.
  2. Create the MDI parent form by choosing MDI Form on the Insert menu just as you did in the previous example.
  3. Change the MDIForm property of Form1 to True.
  4. Add a command button to Form1 with the following code:
Private Sub Command1_Click()
Me.Caption = "Form X"
End Sub
  1. Notice the Me keyword before the property caption.
  2. Add the following code to the Form_Load event procedure on the MDI parent form.
Private Sub MDIForm_Load()
Dim Frm_Array(5) As New Form1
Dim I As Integer
For I = 1 To UBound(Frm_Array)
Frm_Array(I).Show
Next I
End Sub

1. Run the program and observe that each form's code now changes the caption of its own form and not Form1's caption as in the previous example. The output looks like figure 2.6.

Fig. 2.6 MDI child forms can use the Me keyword.

  1. Click on one of the command buttons on one of the five forms that are arranged in a cascaded fashion. Observe that the caption in the Form on the upper-left remains unchanged but that Form X appears on the other forms' captions as each command button is clicked.

Enhancing MDI Applications

Now that you understand how to create child forms, you will want to produce full-scale professional applications. This section will show you how to add additional features that add power to your applications. These features flatten the learning curve for new users.

Adding Menus

Most of the Windows applications that you buy contain a menu structure. The menu bar provides a compact way to provide numerous commands to the user.

When using MDI applications like Microsoft Word, you will notice some unique menu behavior. When no child Window is open, one set of menus is present as shown in figure 2.7.

Fig. 2.7 Microsoft Word has a small menu when no document is open.

When a document is opened, however, the menu changes to show a full complement of functionality. This kind of program behavior distinguishes professional applications from the ones written by a guy who just bought a computer. In fact, this is just another feature of MDI applications. Figure 2.8 shows the MDI child's menu bar.

Fig. 2.8 The MDI child's menu bar takes over.

Active Windows

When a MDI child window is active, its menu bar will appear on the MDI parent window instead of the MDI parent's own menu bar. The following example shows how this works:

  1. Create a new project by choosing New Project on the File menu.
  2. Create the MDI parent form by choosing MDI Form on the Insert menu just as you did in the previous example.
  3. Change the MDIChild property of Form1 to True.
  4. Add a Menu to Form1 with the following items on it:

1. Add the following menu to MDIForm1:

1. In design mode, your program will look like figure 2.9.

Fig. 2.9 In design mode, the menus appear on the MDI child forms.

1. Notice that both the MDI parent window and the MDI child window have normal menus on them.

2. Run the application and observe that when Form1 is loaded, it takes over the menu of the MDI parent with its own menu as shown in figure 2.10.

Fig. 2.10 The MDI child window menu takes over the MDI parent window menu when activated.

1. Next, add a second MDI child form, Form2, to the application by choosing Form from the Insert menu.

2. Add the following menu to Form2:

1. In development mode, your application now looks like figure 2.11.

Fig. 2.11 MDI child forms can use different menus.

1. Notice that all three forms have a different menu bar.

2. Run the program with the following code added to the MDI parent form:

Private Sub MDIForm_Load()
Form2.Show
End Sub

This code is necessary to display both forms at the same time. Notice that when Form1 had the focus, the screen looked like figure 2.10. When the focus changed to Form2, however, the menu on MDIForm1 changed to contain the menu from Form2.

This mapping of all menus to one location on the MDI parent form preserves precious window real estate. As applications become more ambitious, this becomes more important.

Window Lists

You have probably used the Window List provided by Microsoft Word or other MDI applications This is a location on a Menu that lists all of the MDI child windows currently in memory. Figure 2.12 shows the Window List for Microsoft Word 6.0.

Fig. 2.12 The Window List shows active MDI child windows.

This list gives your users a quick way to switch back and forth between active MDI child windows whenever they need to do so. As we've come to expect, there is an easy way to add this feature to MDI applications. You can add this feature to your application by following these simple steps:

1. Continue with the same application that you were working with in the previous exercise. Add a Window menu item to both Form1 and Form2.

2. While still in the Menu Editor, click on the check box labeled WindowList with &Window selected, as shown in figure 2.13.

Fig. 2.13 Check the WindowList box in the Menu Editor.

1. Notice that the WindowList box is checked. You must do this for each MDI child window's menu bar. This is because any of these menus could be in control of the MDI parent window's menu bar when your user wants to use this feature.

2. Run the program and choose Window. Your program now contains a list of all active MDI child windows, as shown in figure 2.14.

Fig. 2.14 You can change MDI child window by using the WindowList on the menu.

The menu selection chosen to hold the WindowList may any other menu selections that you choose to provide. The following exercise will demonstrate how this works:

1. Modify the previous example's Forms. De-select the Windowlist check box, as shown in figure 2.15.

Fig. 2.15 The WindowList menu can contain other selections.

1. Run the program and observe that the Window menu now contains other selections as well as the WindowList, as shown in figure 2.16.

Fig. 2.16 The Window menu has other menu selections as well as the WindowList.

Adding this WindowList to you applications menus will make your programs look and feel more professional.

Adding Toolbars

The addition of a Toolbar to your application will also enhance its appearance and utility for your users. Toolbars provide a quick invocation of a subset of the items in the menu bar. They use icons instead of words to help the users learn how to use your application more quickly.

When deciding which icons to use in your Toolbars, be sure to look at some of the more popular applications like Word, Excel, Borland C++, and Visual Basic itself for ideas. One of the current problems with Toolbars is that each vendor picks icons independent of the other vendors. Thus, users must learn many more icons. You can do your part by making your applications look like one of the other major vendor's applications whenever practical.

To risk stating the obvious, make sure your icons also have some association to the action that will occur. This seems fundamental, but a surprising number of icons confuse users by their lack of intuitive quality. Another must for Toolbars are Tooltips. These are the little yellow flags that magically appear over icons in Visual Basic 4 and other leading software packages. In the section "Adding Tooltips" later in this chapter, we will create a set of Tooltips to show you how.

MDI parent forms place severe restrictions on what kind of controls you can place on its workspace. Only those controls, stock or custom, that support the Align property can be used in this way. Of the stock properties of Visual Basic 4, only the Picture control qualifies. Because the Picture control can serve as a container for other controls, we will place one of them on the MDI parent form and then place the other controls on it. Follow these steps to accomplish this:

1. Create a MDI application with a parent form, MDIForm1, and three child forms Form1, Form2, and Form3.

2. Add a Picture Box control to MDIForm1 named Toolbar and set its Align property to 1 – Align Top.

3. Add the following 4 Image controls to the Toolbar:

Image1 1 – Fixed Single True HANDSHAK.ICO
Image2 1 – Fixed Single True NET01.ICO
Image3 1 – Fixed Single True PHONE.ICO
Image4 1 – Fixed Single True NET09A.ICO

1. All of the icons are in the C:\VB\ICONS\COMM directory in which C: is the drive where you installed Visual Basic 4.

2. Add the Listing 2.1 code to each Image control (buttons 1-4):

Listing 2.1 Picbox1.bas Adding a Toolbar

‘ The code for each image control
Private Sub Image1_Click()
Form1.Show
End Sub
Private Sub Image2_Click()
Form2.Show
End Sub
Private Sub Image3_Click()
Form3.Show
End Sub
Private Sub Image4_Click()
Form1.Hide
Form2.Hide
Form3.Hide
End Sub

1. Run the program and prove to yourself that these image controls now behave like buttons. When you click on them, their corresponding child form receives the focus. When you click on the fourth one, all of the child windows are hidden. Figure 2.17 shows the program running with Form3 displayed.

Fig. 2.17 The icons on the Toolbar act as buttons.

Child windows can also contain working Toolbars. To create one, follow the steps from the previous example to create an application with a MDI parent that contains a Toolbar. Add a Toolbar to the MDI child form. The result will be a form that contains a working Toolbar, as shown in figure 2.18.

Fig. 2.18 MDI child forms may also contain Toolbars.

Adding a Status Bar

Another common feature of commercial systems is the status bar at the bottom of the window. This feature is so common that many people assume it is part of the window like the minimize button. The status bar can be a very useful device. For example, the status bar in Microsoft Word 6.0 tells the user the page number, section number, number of pages, line number, column number, the time, and the status of several Word 6.0 flags. To add a status bar to a form, follow these steps:

1. Create a new project by choosing New from the File menu.

2. Add a Picture Box control to the form.

3. Set the Align property of the Picture Box to 2 – At Bottom.

4. Add a text box that completely covers the Picture Box. Set the Backcolor property to the same gray used in Windows 95 for the window border (&H000000C&) to make it the same color as the rest of the window.

5. Set the Borderstyle property to 0 – None. This will blend the status bar's sides into the window seamlessly.

6. Add three forms to the application: Form1, Form2, and Form3. Notice that the status bar on the MDIForm has nothing on it.

7. Add the Listing 2.2 code to the application:

Listing 2.2 STatbar1.bas A Status Bar Program

Private Sub MDIForm_Load()
Form2.Show
Form3.Show
End Sub
Private Sub Form_Load()
MDIForm1.Text1 = "Form1 Now Active"
End Sub
Private Sub Form_GotFocus()
MDIForm1.Text1 = "Form1 Now Active"
End Sub
Private Sub Form_GotFocus()
MDIForm1.Text1 = "Form2 Now Active"
End Sub
Private Sub Form_GotFocus()
MDIForm1.Text1 = "Form3 Now Active"
End Sub

1. Run the application and see that whenever a form is loaded, a message to that effect appears on the MDIForm status bar, as shown in figure 2.19.

Fig. 2.19 The status bar displays messages to the user.

As in a Microsoft Word 6.0, many different pieces of information are on the same status bar. You can accomplish this by placing several Text Box controls on the same Picture Box control. Add three additional Text Box controls (Text2, Text3, and Text4) to the application. Then add the following code to the Picture Box control:

The code for MDIForm1:

Private Sub MDIForm_Load()
Form2.Show
Form3.Show
End Sub

The code for Form1:

Private Sub Form_GotFocus()
MDIForm1.Text1 = "Form1 Now Active"
End Sub
Private Sub Form_Load()
MDIForm1.Text1 = "Form1 Now Active"
MDIForm1.Text2 = "Form1 is Loaded"
End Sub

The code for Form2:

Private Sub Form_GotFocus()
MDIForm1.Text1 = "Form2 Now Active"
End Sub
Private Sub Form_Load()
MDIForm1.Text3 = "Form2 is Loaded"
End Sub

The code for Form3:

Private Sub Form_GotFocus()
MDIForm1.Text1 = "Form3 Now Active"
End Sub
Private Sub Form_Load()
MDIForm1.Text4 = "Form3 is Loaded"
End Sub

Running this program displays the status of all forms at all times at the bottom of the screen, along with information about which form is active or has focus. Figure 2.20 shows this application running.

Fig. 2.20 The status bar can display numerous messages.

Take care not to turn the status bar into another piece of clutter on the screen. However, judicious use of status bars to display important information can really make your software valuable to your users.

Changing Form Captions

Several improvements to this application are needed. First, the caption on all of the MDI child forms is Form1. Showing the filename would be more useful instead. To make this happen, just change the following code in Form1:

Private Sub Form_Load()
CommonDialog1.Action = 1
Me.Caption = CommonDialog1.FileName
Me.Image1.Picture = LoadPicture(CommonDialog1.FileName)
End Sub

The value returned by the Open dialog box is assigned to the caption of the Me form. In both cases where Me is used in the Form_Load event procedure, the program behaves the same way whether the Me keyword is present or not. Many programmers prefer to insert it to avoid any confusion over which Caption (or Image1) control that this line of code is really affecting. Figure 2.21 shows the result of this change, which enhances the usefulness of the program.

Fig. 2.21 The filename is displayed in the form's title bar.

Adding Tooltips

Adding Tooltips to your Toolbars is almost a requirement in a world with no standards for Toolbar icons. Tooltips normally pop-up after the mouse pointer has rested over an icon for one second or more. You have probably been pleased to find those little yellow hints popping up at least once in the Visual Basic 4 user interface. Windows 95 makes good use of these aids also.

The following steps demonstrate how to add Tooltips to a Toolbar:

  1. Create a MDI application that has a Toolbar with three icons.
  2. These icons are found in the directory C:\VB\ICONS\INDUSTRY. Their names are FACTORY.ICO, PLANE.ICO, ROCKET.ICO.
  3. Add three label controls (Label1, Label2, and Label3).
  4. Add the Listing 2.3 code to MDIForm1:

Listing 2.3 tooltip1.bas Adding Tooltips to a Program

Dim intButton As Integer
'When the mouse pointer is positioned over the factory
Private Sub Image1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
intButton = 1
Timer1.Enabled = True
End Sub
'When the mouse pointer is positioned over the plane
Private Sub Image2_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
intButton = 2
Timer1.Enabled = True
End Sub
'When the mouse pointer is positioned over the rocket
Private Sub Image3_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
intButton = 3
Timer1.Enabled = True
End Sub
'When the mouse moves off of a button, clear all Tooltips
Private Sub Picture1_MouseMove(Button As Integer,
Shift As Integer, X As Single, Y As Single)
TipClear
Timer1.Enabled = False
End Sub
'When the timer goes off, call the sub which displays
Private Sub Timer1_Timer()
DisplayToolTip
End Sub
Sub DisplayToolTip()
' Clear Tooltips so two don't show at once
TipClear
'Display the right Tooltip based on the mouse position
Select Case intButton
Case Is = 1
Label1.Visible = True
Case Is = 2
Label2.Visible = True
Case Is = 3
Label3.Visible = True
End Select
End Sub
' Clear all Tooltips from the screen
Sub TipClear()
Label1.Visible = False
Label2.Visible = False
Label3.Visible = False
End Sub

The strategy behind this code is to react to all mouse events and display a Tooltip if the mouse sits over a button for one second or more. In a production system, you would probably use one label for the whole screen and change both its screen position and caption whenever necessary. The principle is the same with either strategy. The running program looks like figure 2.22.

Fig. 2.22 Tooltips are displayed because of MouseMove events.

Tooltips add another professional touch to your programs. They also flatten the learning curve for new users by allowing them a quick way to discover what the icons on the Toolbar mean.

Creating an Icon Selection Program

Now that we have covered the fundamental approach to creating MDI applications and some enhancements, we are ready for some almost real examples of useful MDI applications. The first of these is an icon selection program. It is "almost real" because it contains the code to work well, but it does not have any of the error handling code that a commercial application would require. Start by following these steps:

  1. Create a new project by choosing New Project from the File menu.
  2. Add a MDI form by choosing MDI Form from the Insert menu.
  3. Make Form1 a MDI child form by changing the MDIChild property to True.
  4. Make MDIForm1 the Startup Form by Choosing Project Options from the Tools menu.
  5. Set the AutoShowChild property of MDIForm1 to False. This property tells Visual Basic to load in Form1 in automatically. In this case, we don't want to load any forms until we are ready.
  6. Add the following menu structure to MDIForm1:

1. Add the following code to MDIForm1:

' When the New menu selection is selected, create a new form and show it.
Private Sub MnuFileNew_Click()
Dim NewIconForm As New Form1
NewIconForm.Show
End Sub
' When the End menu selection is selected, terminate the application
Private Sub mnuFileEnd_Click()
End
End Sub

1. The MDI parent form simply creates a new form and then displays it. The MDI child forms do the rest of the work. MDIForm1 now has a File New and End.

2. The next task will be to add the MDI child form, menu, and code.

3. Add the following controls to Form1 adding the property values on the right to the control named on the left:

Listing 2.4 Iconsel1.bas Selecting an Icon

' As soon as the form loads, find out which icon the user
wants in it
Private Sub Form_Load()
' Action = 1 means show the open common dialog
CommonDialog1.Action = 1
' Load the icon into the Image1 control using the result
of the common dialog session
' The LoadPicture function retrieves a picture file from
disk and puts it on the control
' The keyword Me refers to the instance of Form1 that is
under execution
Me.Image1.Picture = LoadPicture(CommonDialog1.FileName)
End Sub
'End this program
Private Sub mnuFileEnd_Click()
End
End Sub
'Create a new form
Private Sub MnuFileNew_Click()
' The new form allocates memory dynamically for a form
Dim NewIconForm As New Form1
' Showing the form causes it to be created and loaded
onto the screen
NewIconForm.Show
End Sub
' Unload the active form from memory
Private Sub mnuDelete_Click()
Unload Me
End Sub
' When the Select menu selection is chosen, display the
filename of the chosen icon in a message box
Private Sub mnuSelect_Click()
x = MsgBox("You Selected " + CommonDialog1.FileName)
End Sub

1. Run the program and observe its behavior. When New is chosen from the File menu, a dialog box appears as shown in figure 2.23.

Fig. 2.23 The Open dialog box is used to choose an icon.

The icon directory displayed is located at C:\VB\ICONS\MISC where C: is the installation drive for Visual Basic 4. Pick an icon that you want to display and click the Open button. This will load an icon into the Image1 control on Form1, or more exactly the Me form. The result of this is displayed in figure 2.24.

Fig. 2.24 The selected icon is loaded into the Image1 control.

The advantage of the MDI application is its capability to create a user-defined number of MDI child forms that hold enlarged icons in this application. This is operationally superior to preallocating a certain number of them. Figure 2.25 shows the application with three MDI child forms open at the same time. This is the normal usage of this application, and it gives the user a way of looking at several icons in order to choose one of them.

Fig. 2.25 The user can examine several icons to choose the one that he wants.

Finally, the user selects one of the icons. In this case, a stub is used that displays the name of the icon file which the user selected in a message box, as shown in figure 2.26.

Fig. 2.26 The name of the selected icon file is available for use in your application.

Matching Icon and Window Size

Another problem with this program is that the MDI child window size doesn't match the size of the icon. The reason for this is that the BorderStyle property of Form1 is set to 2 – Sizeable. This means that Windows sets the size of the window and that the Height and Width properties of the MDI child form are ignored. Change the BorderStyle of Form1 to 1 – Fixed Single. Set Form1's Height property to 3,000 and the Width to 2,000. Figure 2.27 shows the result.

Fig. 2.27 The size of the MDI child window now matches the size of the enlarged icons.

This improves the appearance of the application significantly. For systems with so many windows, users need a mechanism for arranging icons, tiling, and cascading the windows. To add this functionality to the program, you need to add a Window menu to the MDI parent form that has the following selections:

Just for laughs, check the WindowList selection for the Window menu. Now add the following code to the Form1 code window:

Private Sub mnuWindowArrangeIcons_Click()
MDIForm1.Arrange vbArrangeIcons
End Sub
Private Sub mnuWindowCascade_Click()
MDIForm1.Arrange vbCascade
End Sub
Private Sub mnuWindowTile_Click()
MDIForm1.Arrange vbTileVertical
End Sub

All three of these event procedures use the same method: arrange. The words vbTileVertical, vbCascase, and VbArrangeIcons are all constants defined by the system. The Window menu provides a convenient place to invoke this method. Figure 2.28 shows the result when you tile the MDI application.

Fig. 2.28 You can tile the application using the Arrange method.

Figure 2.29 shows many interesting features of this application. Notice the WindowList at the bottom of the Window menu. This resulted from checking the WindowList check box on the Menu Editor dialog box. Notice also that the cascaded MDI child windows look like they do normally. In other words, the child windows are cascaded, by default, upon creation.

Fig. 2.29 Cascading the MDI child windows can also be done with the Arrange method.

Finally, when you iconify the child windows by clicking the iconify button (the one next to the X), the icons appear on the MDI parent form along the bottom of the window and not on your Windows workspace.

Creating a Text Editor

Another type of application that often needs several identical window types is a word processing system, in which a user commonly cuts text from one document and pastes it to another on the same screen.

The next application we'll create will be a MDI word processor also, but with far fewer features. To create this application, follow these steps:

  1. Choose New Project from the File menu.
  2. Choose MDI Form from the Insert menu.
  3. Turn Form1 into a child form by changing the value of the MDIChild property to True.
  4. Create a text box on Form1 called Text1. Set its Text property to blanks, its MultiLine property to True, and its position (Top,Left) to 0,0.
  5. Set the Caption property of both forms to blank.
  6. Add a menu to MDIForm1 that has the following items:
&File mnuFile
---&New mnuFileNew

1. The application now looks like figure 2.30.

Fig. 2.30 The application has one MDI child form.

1. Add the following code to the application:

' This event procedure creates a new instance of Form1
Private Sub mnuFileNew_Click()
Dim NewForm As New Form1
NewForm.Show
End Sub
' This event procedure causes the text box to fill the
whole window upon creation
Private Sub Form_Resize()
Text1.Height = ScaleHeight
Text1.Width = ScaleWidth
End Sub

1. Run the program, create three new forms, and type something different on each of them. You can see how this application is starting to look like a real word-processing system. Figure 2.31 shows what the system should look like at this point.

Fig. 2.31 The text box acts like a word-processing document when it fills the form.

One feature of a word-processing system is it's capability to copy text to a clipboard. We can add this capability to this application via a menu selection.

1. Add the following menu to the &File menu on your form:

----&Copy mnuFileCopy

1. Add the following code to this menu selection:

Private Sub mnuFileCopy_Click()
Clipboard.SetText ActiveForm.ActiveControl.SelText
End Sub

Now, your user can highlight a piece of text and copy it to the Clipboard by choosing Edit, Copy or by pressing Ctrl+C to copy and Ctrl+V to Paste. This is illustrated in figure 2.32.

Fig. 2.32 You can copy text to the Clipboard using the Copy menu selection.

This application shows what is possible with MDI technology.

From Here...

In this chapter, you were introduced to concepts and strategies associated with the Multiple Document Interface. You learned how to add a MDI parent form to an application. You also learned how to specify that another form in the application is a child of the MDI parent form.

The concepts of object variables were introduced along with the New and Me keywords. Using these devices, you can create an indefinite number of MDI child forms.

We also covered the topic of menus in MDI applications. You learned that all menus display on the MDI parent's menu bar during program execution. You also learned how to add a list of all child windows to the menu bar.

We then covered the creation of a Toolbar. In this section, you learned how Toolbars are created and what code is required to implement them.

The Status bar and Tooltips were explained next. These features make your programs more desirable to a user. The status bar allows your applications to send information to the screen on a continuous basis. Tooltips are indispensable when a Toolbar is added. These little messages assist the user in learning the meaning of the icons in your application.

We created two applications that combined many of the concepts learned earlier into a useful program. The icon finder application made use of child forms to display icons to the user for selection. In the final example, you saw how some of the feature of a text editor are created.


| Previous Chapter | Next Chapter | Search | Table of Contents | Book Home Page |

| Buy This Book | Que Home Page | Digital Bookshelf | Disclaimer |


To order books from QUE, call us at 800-716-0044 or 317-361-5400.

For comments or technical support for our books and software, select Talk to Us.

© 1996, QUE Corporation, an imprint of Macmillan Publishing USA, a Simon and Schuster Company.