Parent and Child Forms

148

4.4.1 Parent and Child Forms

MDI applications consist of a main form, which does not itself display any data, and one or more child forms, which appear only within the main form and are used for displaying documents. The main form is called the MDI parent, and the child forms are called the MDI children. The Form class has two properties that control whether a given form is an MDI parent, MDI child, or neither. The Boolean IsMdiContainer property determines whether a form behaves as an MDI parent. The MdiParent property which is of type Form controls whether a form behaves as an MDI child. Setting the MdiParent property of a form to reference the applications MDI parent form makes the form an MDI child form. Ex am ple 4- 6 shows the minimum amount of code required to display an MDI parent form containing a single MDI child form. Example 4-6. A minimal MDI application Imports System Imports System.Windows.Forms Public Module AppModule Public Sub Main Application.RunNew MainForm End Sub End Module Public Class MainForm Inherits Form Public Sub New Set the main window caption. Text = My MDI Application Set this to be an MDI parent form. IsMdiContainer = True Create a child form. Dim myChild As New DocumentFormMy Document, Me myChild.Show End Sub End Class Public Class DocumentForm Inherits Form Public Sub NewByVal name As String, ByVal parent As Form Set the document window caption. Text = name Set this to be an MDI child form. MdiParent = parent End Sub End Class Assuming that the code in Ex am ple 4- 6 is saved in a file named MyApp.vb, it can be compiled from the command line with this command: vbc MyApp.vb r:System.dll,System.Windows.Forms.dll Running the resulting executable produces the display shown in Figur e 4- 7 . 149 Figure 4-7. A minimal MDI application the output of the code in Ex a m ple 4 - 6 The Form class has two read-only properties related to MDI behavior. The IsMdiChild property returns a Boolean value that indicates whether the form is an MDI child. The MdiChildren property of a parent form contains a collection of references to the forms child forms. The IsMdiChild and MdiChildren properties are both automatically maintained in response to setting the child forms MdiParent properties.

4.4.2 Creating a Window Menu