Zebra0.com

visual-basic forms

If you double click on the New icon to open the code view window for the MDIParent form, you will see that a lot of code has been written for you, with numerous "ToDo" notes. Modify this code as shown below:
Private Sub ShowNewForm(ByVal sender As Object, ByVal e As EventArgs) _
   Handles NewToolStripMenuItem.Click, NewToolStripButton.Click, _
   NewWindowToolStripMenuItem.Click
   ' Create a new instance of the child form.
   'Dim ChildForm As New System.Windows.Forms.Form REPLACED
    Dim ChildForm As New Form1 'ADDED
    ' Make it a child of this MDI form before showing it.
    ChildForm.MdiParent = Me
    m_ChildFormNumber += 1
    ChildForm.Text = "Window " & m_ChildFormNumber
    ChildForm.Show()
End Sub 'ShowNewForm
Next, we will modify the code for OpenFile so that it selects a picture and displays it in the picture box of the active child form. We will use the file name of the picture for the text of the child form.
Private Sub OpenFile(ByVal sender As Object, ByVal e As EventArgs) _
   Handles OpenToolStripMenuItem.Click, OpenToolStripButton.Click
   Dim OpenFileDialog As New OpenFileDialog
   OpenFileDialog.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
   'OpenFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*" REPLACED
   OpenFileDialog.Filter = _
     "Picture Files (Bitmaps|*.bmp|GIFS|*.gif|JPG|*.jpg|All Files (*.*)|*.*"
   If (OpenFileDialog.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK) Then
      Dim ActiveChild As Form1 = Me.ActiveMdiChild 'ADDED
      ActiveChild.Text = OpenFileDialog.FileName 'ADDED
      ActiveChild.Pic.Image = Image.FromFile(OpenFileDialog.FileName) 'ADDED
   End If
End Sub 'OpenFile

When you run the program, you can add a new child form, then click open to load a picture. However, if you click open before you have added a child form, you will get an error because there is no active child (or any child.) There are several ways to prevent this error from occurring. One way would be to make the menu options for open disabled, until a child is added. You would still produce an error if you closed all of the children and then pressed open. You would have to keep track of when the last child was closed and then disable buttons and menu choices.

NEXT: Form to Select Days