Imports System.Windows.Forms

Public Class CustomMenu
    Inherits System.Windows.Forms.MainMenu

    'Clickイベントを定義するデリゲートを宣言
    Public Delegate Sub ClickEventHandler _
        (ByVal sender As Object, ByVal e As EventArgs)

    '各メニューのイベントの宣言
    Public Event File_Click As ClickEventHandler
    Public Event FileNew_Click As ClickEventHandler
    Public Event FileOpen_Click As ClickEventHandler
    Public Event FileClose_Click As ClickEventHandler
    Public Event FileOverSave_Click As ClickEventHandler
    Public Event FileSave_Click As ClickEventHandler
    Public Event FileQuit_Click As ClickEventHandler
    Public Event Edit_Click As ClickEventHandler
    Public Event EditCut_Click As ClickEventHandler
    Public Event EditCopy_Click As ClickEventHandler
    Public Event EditPaste_Click As ClickEventHandler
    Public Event EditDelete_Click As ClickEventHandler

    Public Sub New()

        MyBase.New()

        SetInitialMenuItem()

    End Sub

    Private Sub SetInitialMenuItem()

        Dim mi, smi As MenuItem

        'ファイル メニューを作成
        mi = Me.MenuItems.Add("ファイル(&F)", _
                New EventHandler(AddressOf Menu_Click))
        smi = mi.MenuItems.Add("新規作成(&N)", _
                New EventHandler(AddressOf Menu_Click))
        smi.Shortcut = Windows.Forms.Shortcut.CtrlN
        smi = mi.MenuItems.Add("開く(&O)...", _
                New EventHandler(AddressOf Menu_Click))
        smi.Shortcut = Windows.Forms.Shortcut.CtrlO
        mi.MenuItems.Add("閉じる(&C)", _
                New EventHandler(AddressOf Menu_Click))
        mi.MenuItems.Add("-")
        smi = mi.MenuItems.Add("上書き保存(&S)...", _
                New EventHandler(AddressOf Menu_Click))
        smi.Shortcut = Windows.Forms.Shortcut.CtrlS
        mi.MenuItems.Add("名前を付けて保存(&A)...", _
                New EventHandler(AddressOf Menu_Click))
        mi.MenuItems.Add("-")
        mi.MenuItems.Add("終了(&X)", _
                New EventHandler(AddressOf Menu_Click))

        '編集 メニューを作成
        mi = Me.MenuItems.Add("編集(&E)", _
                New EventHandler(AddressOf Menu_Click))
        smi = mi.MenuItems.Add("切り取り(&T)", _
                New EventHandler(AddressOf Menu_Click))
        smi.Shortcut = Windows.Forms.Shortcut.CtrlX
        smi = mi.MenuItems.Add("コピー(&C)", _
                New EventHandler(AddressOf Menu_Click))
        smi.Shortcut = Windows.Forms.Shortcut.CtrlC
        smi = mi.MenuItems.Add("貼り付け(&P)", _
                New EventHandler(AddressOf Menu_Click))
        smi.Shortcut = Windows.Forms.Shortcut.CtrlV
        mi.MenuItems.Add("-")
        mi.MenuItems.Add("削除(&D)", _
                New EventHandler(AddressOf Menu_Click))

    End Sub

    'メニューのClickイベントを一括して受け取るイベントモジュール
    Private Sub Menu_Click(ByVal sender As Object, ByVal e As EventArgs)

        'senderをMenuItemにキャスト
        Dim mi As System.Windows.Forms.MenuItem = _
            DirectCast(sender, System.Windows.Forms.MenuItem)

        'テキストで分岐し、個別のイベントを発生させる
        Select Case mi.Text
            Case "ファイル(&F)"
                RaiseEvent File_Click(sender, e)
            Case "新規作成(&N)"
                RaiseEvent FileNew_Click(sender, e)
            Case "開く(&O)..."
                RaiseEvent FileOpen_Click(sender, e)
            Case "閉じる(&C)"
                RaiseEvent FileClose_Click(sender, e)
            Case "上書き保存(&S)..."
                RaiseEvent FileOverSave_Click(sender, e)
            Case "名前を付けて保存(&A)..."
                RaiseEvent FileSave_Click(sender, e)
            Case "終了(&X)"
                RaiseEvent FileQuit_Click(sender, e)
            Case "編集(&E)"
                RaiseEvent Edit_Click(sender, e)
            Case "切り取り(&T)"
                RaiseEvent EditCut_Click(sender, e)
            Case "コピー(&C)"
                RaiseEvent EditCopy_Click(sender, e)
            Case "貼り付け(&P)"
                RaiseEvent EditPaste_Click(sender, e)
            Case "削除(&D)"
                RaiseEvent EditDelete_Click(sender, e)
        End Select

    End Sub

End Class
