You have now completed coding the multiple document text editor application. The following sections summarize the code for the two forms that make up the application. The Parent Form Listing 15.1 contains all the code for the parent form, frmParent. Listing 15.1 MDIDEMO.ZIP Code for the Parent Form Imports System.IO Public Class frmParent Inherits System.Windows.Forms.Form #Region " Windows Form Designer generated code " Public Sub New() MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() 'Add any initialization after the InitializeComponent() call NewChild() End Sub 'Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu Friend WithEvents mnuFile As System.Windows.Forms.MenuItem Friend WithEvents mnuFileNew As System.Windows.Forms.MenuItem Friend WithEvents mnuFileOpen As System.Windows.Forms.MenuItem Friend WithEvents MenuItem4 As System.Windows.Forms.MenuItem Friend WithEvents mnuFileExit As System.Windows.Forms.MenuItem Friend WithEvents mnuWindow As System.Windows.Forms.MenuItem Friend WithEvents mnuWindowTileH As System.Windows.Forms.MenuItem Friend WithEvents mnuWindowTileV As System.Windows.Forms.MenuItem Friend WithEvents mnuWindowCascade As System.Windows.Forms.MenuItem Friend WithEvents MdiClient1 As System.Windows.Forms.MdiClient Friend WithEvents dlgOpen As System.Windows.Forms.OpenFileDialog 'Required by the Windows Form Designer Private components As System.ComponentModel.Container 'NOTE: The following procedure is required by the Windows Form Designer 'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor. <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.mnuWindowTileV = New System.Windows.Forms.MenuItem() Me.mnuWindowTileH = New System.Windows.Forms.MenuItem() Me.mnuFile = New System.Windows.Forms.MenuItem() Me.mnuFileNew = New System.Windows.Forms.MenuItem() Me.mnuFileOpen = New System.Windows.Forms.MenuItem() Me.MenuItem4 = New System.Windows.Forms.MenuItem() Me.mnuFileExit = New System.Windows.Forms.MenuItem() Me.MdiClient1 = New System.Windows.Forms.MdiClient() Me.MainMenu1 = New System.Windows.Forms.MainMenu() Me.mnuWindow = New System.Windows.Forms.MenuItem() Me.mnuWindowCascade = New System.Windows.Forms.MenuItem() Me.dlgOpen = New System.Windows.Forms.OpenFileDialog() Me.SuspendLayout() ' 'mnuWindowTileV ' Me.mnuWindowTileV.Index = 1 Me.mnuWindowTileV.Text = "Tile Vertical" ' 'mnuWindowTileH ' Me.mnuWindowTileH.Index = 0 Me.mnuWindowTileH.Text = "Tile Horizontal" ' 'mnuFile ' Me.mnuFile.Index = 0 Me.mnuFile.MenuItems.AddRange(New System.Windows.Forms.MenuItem() _ {Me.mnuFileNew, Me.mnuFileOpen, Me.MenuItem4, Me.mnuFileExit}) Me.mnuFile.MergeType = System.Windows.Forms.MenuMerge.MergeItems Me.mnuFile.Text = "File" ' 'mnuFileNew ' Me.mnuFileNew.Index = 0 Me.mnuFileNew.Shortcut = System.Windows.Forms.Shortcut.CtrlN Me.mnuFileNew.Text = "New" ' 'mnuFileOpen ' Me.mnuFileOpen.Index = 1 Me.mnuFileOpen.Shortcut = System.Windows.Forms.Shortcut.CtrlO Me.mnuFileOpen.Text = "Open" ' 'MenuItem4 ' Me.MenuItem4.Index = 2 Me.MenuItem4.MergeOrder = 98 Me.MenuItem4.Text = "-" ' 'mnuFileExit ' Me.mnuFileExit.Index = 3 Me.mnuFileExit.MergeOrder = 99 Me.mnuFileExit.Text = "Exit" ' 'MdiClient1 ' Me.MdiClient1.Dock = System.Windows.Forms.DockStyle.Fill Me.MdiClient1.Name = "MdiClient1" Me.MdiClient1.TabIndex = 0 ' 'MainMenu1 ' Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() _ {Me.mnuFile, Me.mnuWindow}) ' 'mnuWindow ' Me.mnuWindow.Index = 1 Me.mnuWindow.MdiList = True Me.mnuWindow.MenuItems.AddRange(New System.Windows.Forms.MenuItem() _ {Me.mnuWindowTileH, Me.mnuWindowTileV, Me.mnuWindowCascade}) Me.mnuWindow.Text = "Window" ' 'mnuWindowCascade ' Me.mnuWindowCascade.Index = 2 Me.mnuWindowCascade.Text = "Cascade" ' 'dlgOpen ' Me.dlgOpen.DefaultExt = "txt" Me.dlgOpen.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*" ' 'frmParent ' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(576, 385) Me.Controls.AddRange(New System.Windows.Forms.Control() _ {Me.MdiClient1}) Me.IsMdiContainer = True Me.Menu = Me.MainMenu1 Me.Name = "frmParent" Me.Text = "MDI Text Editor" Me.ResumeLayout(False) End Sub #End Region Private Sub NewChild() Dim f As New frmChild() f.MdiParent = Me f.FileName = "" f.TextSaved = True f.Show() End Sub Private Sub mnuFileNew_Click(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles mnuFileNew.Click NewChild() End Sub Private Sub mnuFileOpen_Click(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles mnuFileOpen.Click dlgOpen.ShowDialog() End Sub Private Sub dlgOpen_FileOk(ByVal sender As Object, _ ByVal e As System.ComponentModel.CancelEventArgs) Handles dlgOpen.FileOk Dim f As New frmChild() Dim nTemp As Integer = FreeFile() Dim MyFile As StreamReader f.MdiParent = Me MyFile = File.OpenText(dlgOpen.FileName) f.txtMain.Text = MyFile.ReadToEnd f.txtMain.Select(0, 0) MyFile.Close() f.FileName = dlgOpen.FileName f.TextSaved = True f.Show() End Sub Private Sub mnuWindowCascade_Click(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles mnuWindowCascade.Click Me.LayoutMdi(MdiLayout.Cascade) End Sub Private Sub mnuWindowTileH_Click(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles mnuWindowTileH.Click Me.LayoutMdi(MdiLayout.TileHorizontal) End Sub Private Sub mnuWindowTileV_Click(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles mnuWindowTileV.Click Me.LayoutMdi(MdiLayout.TileVertical) End Sub End Class The Child Form Listing 15.2 contains all the code for the child form, frmChild. Listing 15.2 MDIDEMO.ZIP Code for the Child Form Imports System.IO Public Class frmChild Inherits System.Windows.Forms.Form #Region " Windows Form Designer generated code " Public Sub New() MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() 'Add any initialization after the InitializeComponent() call End Sub 'Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu Friend WithEvents txtMain As System.Windows.Forms.TextBox Friend WithEvents mnuFile As System.Windows.Forms.MenuItem Friend WithEvents mnuFileSave As System.Windows.Forms.MenuItem Friend WithEvents mnuFileSaveAs As System.Windows.Forms.MenuItem Friend WithEvents mnuFileClose As System.Windows.Forms.MenuItem Friend WithEvents mnuFileSep As System.Windows.Forms.MenuItem Friend WithEvents mnuFilePrint As System.Windows.Forms.MenuItem Friend WithEvents dlgSave As System.Windows.Forms.SaveFileDialog Friend WithEvents PrintDocument1 As System.Drawing.Printing.PrintDocument 'Required by the Windows Form Designer Private components As System.ComponentModel.Container 'NOTE: The following procedure is required by the Windows Form Designer 'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor. <System.Diagnostics.DebuggerStepThrough()> _ Private Sub InitializeComponent() Me.MainMenu1 = New System.Windows.Forms.MainMenu() Me.mnuFile = New System.Windows.Forms.MenuItem() Me.mnuFileSave = New System.Windows.Forms.MenuItem() Me.mnuFileSaveAs = New System.Windows.Forms.MenuItem() Me.mnuFileClose = New System.Windows.Forms.MenuItem() Me.mnuFileSep = New System.Windows.Forms.MenuItem() Me.mnuFilePrint = New System.Windows.Forms.MenuItem() Me.PrintDocument1 = New System.Drawing.Printing.PrintDocument() Me.txtMain = New System.Windows.Forms.TextBox() Me.dlgSave = New System.Windows.Forms.SaveFileDialog() Me.SuspendLayout() ' 'MainMenu1 ' Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() _ {Me.mnuFile}) ' 'mnuFile ' Me.mnuFile.Index = 0 Me.mnuFile.MenuItems.AddRange(New System.Windows.Forms.MenuItem() _ {Me.mnuFileSave, Me.mnuFileSaveAs, Me.mnuFileClose, _ Me.mnuFileSep, Me.mnuFilePrint}) Me.mnuFile.MergeType = System.Windows.Forms.MenuMerge.MergeItems Me.mnuFile.Text = "File" ' 'mnuFileSave ' Me.mnuFileSave.Index = 0 Me.mnuFileSave.Shortcut = System.Windows.Forms.Shortcut.CtrlS Me.mnuFileSave.Text = "Save" ' 'mnuFileSaveAs ' Me.mnuFileSaveAs.Index = 1 Me.mnuFileSaveAs.Text = "Save As..." ' 'mnuFileClose ' Me.mnuFileClose.Index = 2 Me.mnuFileClose.Text = "Close" ' 'mnuFileSep ' Me.mnuFileSep.Index = 3 Me.mnuFileSep.Text = "-" ' 'mnuFilePrint ' Me.mnuFilePrint.Index = 4 Me.mnuFilePrint.Shortcut = System.Windows.Forms.Shortcut.CtrlP Me.mnuFilePrint.Text = "Print" ' 'txtMain ' Me.txtMain.Dock = System.Windows.Forms.DockStyle.Fill Me.txtMain.Font = New System.Drawing.Font("Courier New", 9.75!, _ System.Drawing.FontStyle.Regular, _ System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.txtMain.Multiline = True Me.txtMain.Name = "txtMain" Me.txtMain.Size = New System.Drawing.Size(292, 273) Me.txtMain.TabIndex = 0 Me.txtMain.Text = "" ' 'frmChild ' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 273) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.txtMain}) Me.Menu = Me.MainMenu1 Me.Name = "frmChild" Me.Text = "frmChild" Me.ResumeLayout(False) End Sub #End Region Public TextSaved As Boolean Private sFileName As String Public Property FileName() As String Get Return sFileName End Get Set(ByVal Value As String) sFileName = Value If Trim(sFileName) = "" Then Me.Text = "Untitled" Else Me.Text = Mid(sFileName, InStrRev(sFileName, "\") + 1) End If End Set End Property Private Sub txtMain_TextChanged(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles txtMain.TextChanged Me.TextSaved = False End Sub Private Sub SaveFile() Dim MyFile As StreamWriter If sFileName = "" Then SaveFileAs() End If MyFile = File.CreateText(sFileName) MyFile.Write(txtMain.Text) MyFile.Close() Me.TextSaved = True End Sub Sub SaveFileAs() dlgSave.FileName = Me.FileName dlgSave.ShowDialog() End Sub Private Sub mnuFileSave_Click(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles mnuFileSave.Click If sFileName > "" Then SaveFile() Else SaveFileAs() End If End Sub Private Sub mnuFileSaveAs_Click(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles mnuFileSaveAs.Click SaveFileAs() End Sub Private Sub dlgSave_FileOk(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles dlgSave.FileOk If e.Cancel = False Then Me.FileName = dlgSave.FileName SaveFile() End If End Sub Private Sub frmChild_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing If Me.TextSaved = False Then Dim sMsg As String Dim nResult As Integer sMsg = "Save changes to " If Me.FileName = "" Then sMsg += "this untitled document" Else sMsg += Mid(sFileName, InStrRev(sFileName, "\") + 1) End If sMsg += " before closing it?" nResult = MessageBox.Show(sMsg, "System Message", _ MessageBoxButtons.YesNoCancel) Select Case nResult Case DialogResult.Cancel e.Cancel = True Case DialogResult.Yes SaveFile() Case Else 'Do nothing; let the window close. End Select End If End Sub Private Sub mnuFileClose_Click(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles mnuFileClose.Click Me.Close() End Sub Private Sub mnuFilePrint_Click(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles mnuFilePrint.Click PrintDocument1.Print() End Sub Private Sub PrintDocument1_PrintPage(ByVal sender As Object, _ ByVal e As System.Drawing.Printing.PrintPageEventArgs) _ Handles PrintDocument1.PrintPage.Graphics.DrawString(txtMain.Text, _ New Font("Courier New", 10, FontStyle.Regular), Brushes.Black, 0, 0) End Sub End Class |