Creating a Visual Basic MDI Form
Previous | Table of Contents | Next |
Hiding and Showing Forms in Visual Basic | Creating Top-Level Menus in Visual Basic |
Purchase and download the fully updated Visual Basic 2010 edition of this eBook in PDF and ePub for only $9.99 |
Many of today's applications are designed using an MDI. Given this popularity it is not surprising that Visual Basic provides extensive support for developing MDI based applications. In this chapter of Visual Basic Essentials we will explain what an MDI is, and provide a detailed, step by step guide to developing MDI applications with Visual Basic.
What is an MDI?
The acronym MDI stands for Multi Document Interface. If you have ever used Microsoft Word or Excel the chances are you have used a multi-document interface. Multi-document interfaces consist of a parent form (also called a container) which contains other forms. For example, when more than one document is open in some versions of Microsoft Word, each document appears in its own form, contained within the parent form. The forms within the parent can be moved to any position within the parent form, but typically cannot be moved outside the confines of the parent.
The term MDI is actually misleading. A more accurate name would be multi-form interface or multi-window interface. The reason for this is that there is nothing that restricts the forms to containing just documents. In fact, a form in an MDI interface can contain anything that a form in a non-MDI interface can contain. In fact, recent versions of Microsoft Office (specifically Office 2007) have moved away from the MDI approach. When multiple documents are opened in the Word 2007, for example, each document appears in an independent window. That said the MDI approach to user interface design is extremely useful and popular and a wide range of commercial applications are MDI based.
The following figure shows an application implemented using an MDI. Notice that all the forms are contained in the parent window:
Creating an MDI Parent and Child Forms in Visual Studio
The first step in creating an MDI form is to create the parent form. Begin by launching Visual Studio and creating a new Windows Application project named vbMDI. The new project will contain a single form. Change the name of this form in the Properties panel to MDIparent. Also in the Properties panel, change the IsMdiContainer property to True. You will notice that the form background changes to dark grey, the default for MDI containers. This form is now ready to act as the container for the child forms.
Add two more forms to the project using the Visual Studio Add New Item toolbar button:
Edit the properties of the two new forms so that the title texts are Child Form 1 and Child Form2 respectively. Also, use the properties panel to name the forms MDIchild1 and MDIchild2.
Writing the Visual Basic Code to Add the Children to the MDI Parent
The next step is to add the two new forms (MDIchild1 and MDIchild2) to the parent form (MDIparent). To do this click on the tab for the parent form in Visual Studio and double click on the form to display the event procedures. We will now write Visual Basic code to add the two child forms to the container parent form. To do this we will set the MdiParent property of each child to reference the MDIparent form. Note that because this is the Load event of the actual parent form, we refer to it with the keyword Me rather than by the form name. Having set the Mdiparent of each child we then need to display the form using the form Show() method:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load MDIchild1.MdiParent = Me MDIchild1.Show() MDIchild2.MdiParent = Me MDIchild2.Show() End Sub
After adding the above code to the parent form's Load event, press F5 to build and run the example. When the application runs the child forms will both appear in the parent form:
Summary
This chapter has covered the topic of creating MDI based user interfaces in Visual Basic.
Purchase and download the fully updated Visual Basic 2010 edition of this eBook in PDF and ePub for only $9.99 |