Sei sulla pagina 1di 3

Creating the MDI form

Start a new project and remove the existing Form1 (select Remove
Form1 from the Project menu).
Select Add MDI Form, New, from the Project menu. Click on MDI Form.
Click Open.
Change the Name property for this form to MDIEditor.
Adding the child form
Select Add Form, Existing, from the Project menu and add the form
you developed for the FileViewer application (frmFileViewer.frm).
Select the Fileviewer form. Change the MDIChild property to True.
Change the Name property to frmDoc. Save the form
as frmDoc.frm. Save the MDI form as mdiEditor.frm and the project
as MDI.vbp
You will write code to create a new instance of this form each time a new document
is opened.
Resizing the rich textbox
The child forms in an MDI application can be resized independently. The rich textbox
control should grow or shrink with the form. The resize event for the form can be
used to achieve this.
Add the following code to the Resize event for the frmDoc form
Private Sub Form_Resize()
rtbDisplay.Move 0,0,Me.Width 100, Me.Height - 300
End Sub
Setting up the menus
You have a choice of how you set up menus for an MDI project:

attach all the menu options to the MDI form and disable the options not
available until there is a child form open

attach menu options to the child form and use a different menu on the MDI
form the menu on the MDI form will automatically be replaced by the menu
on the child form as soon as a child form is opened

The child form that you have just added to the project, already has a menu attached
so the second option is preferable for this project.

Select code for the frmDoc form and amend


the mnuNew and mnuOpenFile options as shown
A new instance of a document form is created by declaring a variable as type New
frmDoc.
Private Sub mnuNew_Click()
Dim newDoc As New frmDoc
newDoc.Show
newDoc.Caption = "Untitled"
newDoc.rtbDisplay.Text = ""
End Sub

Private Sub mnuOpenFile_Click()


Dim strFileName As String
Dim newDoc As New frmDoc
newDoc.Show
newDoc.cdlViewer.Filter = "RTF|*.rtf|Text|*.txt|All|*.*"
newDoc.cdlViewer.ShowOpen
On Error GoTo OpenProblems
strFileName = newDoc.cdlViewer.filename
newDoc.rtbDisplay.LoadFile strFileName
newDoc.Caption = strFileName
Exit Sub
OpenProblems:
MsgBox "Cant open the file, select another.", vbCritical
Exit Sub
End Sub

The main MDI form can be given a simple menu to enable the first document to be
opened.
Select the MDIEditor form and use the Menu Editor to set up a menu
which looks like the following

Caption

Name

&File

mnuFile

&New

mnuNew

&Open

mnuOpen

mnuSep1

E&xit

mnuExit

Add a common dialog control to form MDIEditor. Change


its Name property to cdlViewer.

Copy the code from the frmDoc form New and Open options to the
New and Open options for the MDIEditor form.

Add code for the mnuExit option to the MDIEditor form


Private Sub mnuExit_Click()
End
End Sub
Save the project and files. Run the application to test the main menu options.