Public Class MainForm
    Inherits System.Windows.Forms.Form

#Region " Windows フォーム デザイナで生成されたコード "

    Public Sub New()
        MyBase.New()

        ' この呼び出しは Windows フォーム デザイナで必要です。
        InitializeComponent()

        ' InitializeComponent() 呼び出しの後に初期化を追加します。

    End Sub

    ' Form は、コンポーネント一覧に後処理を実行するために dispose をオーバーライドします。
    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

    ' Windows フォーム デザイナで必要です。
    Private components As System.ComponentModel.IContainer

    ' メモ : 以下のプロシージャは、Windows フォーム デザイナで必要です。
    'Windows フォーム デザイナを使って変更してください。  
    ' コード エディタを使って変更しないでください。
    Friend WithEvents MenuItem1 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem2 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem3 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem5 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem4 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem6 As System.Windows.Forms.MenuItem
    Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu
    Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.MenuItem1 = New System.Windows.Forms.MenuItem
        Me.MenuItem2 = New System.Windows.Forms.MenuItem
        Me.MenuItem3 = New System.Windows.Forms.MenuItem
        Me.MenuItem5 = New System.Windows.Forms.MenuItem
        Me.MenuItem4 = New System.Windows.Forms.MenuItem
        Me.MenuItem6 = New System.Windows.Forms.MenuItem
        Me.MainMenu1 = New System.Windows.Forms.MainMenu
        Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog
        '
        'MenuItem1
        '
        Me.MenuItem1.Index = 0
        Me.MenuItem1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem2, Me.MenuItem3, Me.MenuItem5, Me.MenuItem4})
        Me.MenuItem1.Text = "ファイル(&F)"
        '
        'MenuItem2
        '
        Me.MenuItem2.Index = 0
        Me.MenuItem2.Shortcut = System.Windows.Forms.Shortcut.CtrlN
        Me.MenuItem2.Text = "新規作成"
        '
        'MenuItem3
        '
        Me.MenuItem3.Index = 1
        Me.MenuItem3.Shortcut = System.Windows.Forms.Shortcut.CtrlO
        Me.MenuItem3.Text = "開く"
        '
        'MenuItem5
        '
        Me.MenuItem5.Index = 2
        Me.MenuItem5.Text = "-"
        '
        'MenuItem4
        '
        Me.MenuItem4.Index = 3
        Me.MenuItem4.Text = "終了(&X)"
        '
        'MenuItem6
        '
        Me.MenuItem6.Index = 1
        Me.MenuItem6.MdiList = True
        Me.MenuItem6.Text = "ウィンドウ(&W)"
        '
        'MainMenu1
        '
        Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem1, Me.MenuItem6})
        '
        'OpenFileDialog1
        '
        Me.OpenFileDialog1.Filter = "テキストファイル（*.txt）|*.txt|JPEGファイル（*.jpg）|*.jpg"
        Me.OpenFileDialog1.InitialDirectory = "c:\"
        '
        'MainForm
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 12)
        Me.ClientSize = New System.Drawing.Size(464, 366)
        Me.IsMdiContainer = True
        Me.Menu = Me.MainMenu1
        Me.Name = "MainForm"
        Me.Text = "MDIアプリケーション"

    End Sub

#End Region

    Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim NewMDIChild As New Form1

        NewMDIChild.MdiParent = Me
        NewMDIChild.Show()
    End Sub

    Dim cnt As Byte = 1

    Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem2.Click
        Dim NewForm As New Form
        Dim NewRtbox As New RichTextBox

        cnt += 1

        With NewRtbox
            .Dock = DockStyle.Fill
            .ShowSelectionMargin = True
        End With

        With NewForm
            .MdiParent = Me
            .Text = "新規文書" & cnt
            .Controls.Add(NewRtbox)
            .Show()
        End With
    End Sub

    Private Sub MenuItem3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem3.Click
        Dim Fname As String = ""
        Dim Extname As String = ""

        If Me.OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Fname = OpenFileDialog1.FileName

            Extname = Microsoft.VisualBasic.Mid(Fname, Len(Fname) - 2, 3)

            Select Case Extname
                Case "jpg", "JPG"
                    imageview(Fname)

                Case "txt", "TXT"
                    textview(Fname)
            End Select
        End If
    End Sub

    Private Sub textview(ByVal Fname As String)
        Dim NewForm As New Form
        Dim NewRtbox As New RichTextBox

        With NewRtbox
            .Dock = DockStyle.Fill
            .ShowSelectionMargin = True
            .LoadFile(Fname, RichTextBoxStreamType.PlainText)
        End With

        With NewForm
            .MdiParent = Me
            .Text = Fname
            .Controls.Add(NewRtbox)
            .Show()
        End With
    End Sub

    Private Sub imageview(ByVal Fname As String)
        Dim NewForm As New Form
        Dim NewPicbox As New PictureBox

        With NewPicbox
            .Dock = DockStyle.Fill
            .SizeMode = PictureBoxSizeMode.StretchImage
            .Image = Image.FromFile(Fname)
        End With

        With NewForm
            .MdiParent = Me
            .Text = Fname
            .Controls.Add(NewPicbox)
            .Show()
        End With
    End Sub

    Private Sub MenuItem4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem4.Click
        End
    End Sub
End Class
