Owner Draw MenuItem : MenuItem « GUI « VB.Net Tutorial






Owner Draw MenuItem
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms

public class OwnerDrawMenu
   public Shared Sub Main
        Application.Run(New Form1)
   End Sub
End class


Public Class Form1
    Inherits System.Windows.Forms.Form

    Private files() As String = {"yourfile.jpg", "yourfile.jpg"}


#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

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    '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.
    Friend WithEvents mainMenu1 As System.Windows.Forms.MainMenu
    Friend WithEvents mnuFile As System.Windows.Forms.MenuItem
    Friend WithEvents mnuNew As System.Windows.Forms.MenuItem
    Friend WithEvents mnuFileOpen As System.Windows.Forms.MenuItem
    Friend WithEvents mnuFileClose As System.Windows.Forms.MenuItem
    Friend WithEvents mnuFileSave As System.Windows.Forms.MenuItem
    Friend WithEvents mnuFileSaveAs As System.Windows.Forms.MenuItem
    Friend WithEvents mnuEdit As System.Windows.Forms.MenuItem
    Friend WithEvents mnuEditCopy As System.Windows.Forms.MenuItem
    Friend WithEvents mnuEditPaste As System.Windows.Forms.MenuItem
    Friend WithEvents mnuOptions As System.Windows.Forms.MenuItem
    Friend WithEvents mnuOption1 As System.Windows.Forms.MenuItem
    Friend WithEvents mnuOption2 As System.Windows.Forms.MenuItem
    Friend WithEvents mnuOption3 As System.Windows.Forms.MenuItem
    Friend WithEvents mnuRadioOptions As System.Windows.Forms.MenuItem
    Friend WithEvents mnuRadioOption1 As System.Windows.Forms.MenuItem
    Friend WithEvents mnuRadioOption2 As System.Windows.Forms.MenuItem
    Friend WithEvents mnuRadioOption3 As System.Windows.Forms.MenuItem
    Friend WithEvents mnuWindow As System.Windows.Forms.MenuItem
    Friend WithEvents mnuMenu1 As System.Windows.Forms.MenuItem
    Friend WithEvents mnuMenu11 As System.Windows.Forms.MenuItem
    Friend WithEvents mnuMenu12 As System.Windows.Forms.MenuItem
    Friend WithEvents mnuMenu13 As System.Windows.Forms.MenuItem
    Friend WithEvents mnuMenu14 As System.Windows.Forms.MenuItem
    Friend WithEvents mnuMerge As System.Windows.Forms.MenuItem
    Friend WithEvents mnuMenu2 As System.Windows.Forms.MenuItem
    Friend WithEvents mnuMenu21 As System.Windows.Forms.MenuItem
    Friend WithEvents mnuMenu22 As System.Windows.Forms.MenuItem
    Friend WithEvents mnuMenu23 As System.Windows.Forms.MenuItem
    Friend WithEvents mnuMenu24 As System.Windows.Forms.MenuItem
    Friend WithEvents mnuSpecial As System.Windows.Forms.MenuItem
    Friend WithEvents mnuODVote As System.Windows.Forms.MenuItem
    Friend WithEvents mnuODShazam As System.Windows.Forms.MenuItem
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.mainMenu1 = New System.Windows.Forms.MainMenu()
        Me.mnuFile = New System.Windows.Forms.MenuItem()
        Me.mnuNew = New System.Windows.Forms.MenuItem()
        Me.mnuFileOpen = New System.Windows.Forms.MenuItem()
        Me.mnuFileClose = New System.Windows.Forms.MenuItem()
        Me.mnuFileSave = New System.Windows.Forms.MenuItem()
        Me.mnuFileSaveAs = New System.Windows.Forms.MenuItem()
        Me.mnuEdit = New System.Windows.Forms.MenuItem()
        Me.mnuEditCopy = New System.Windows.Forms.MenuItem()
        Me.mnuEditPaste = New System.Windows.Forms.MenuItem()
        Me.mnuOptions = New System.Windows.Forms.MenuItem()
        Me.mnuOption1 = New System.Windows.Forms.MenuItem()
        Me.mnuOption2 = New System.Windows.Forms.MenuItem()
        Me.mnuOption3 = New System.Windows.Forms.MenuItem()
        Me.mnuRadioOptions = New System.Windows.Forms.MenuItem()
        Me.mnuRadioOption1 = New System.Windows.Forms.MenuItem()
        Me.mnuRadioOption2 = New System.Windows.Forms.MenuItem()
        Me.mnuRadioOption3 = New System.Windows.Forms.MenuItem()
        Me.mnuWindow = New System.Windows.Forms.MenuItem()
        Me.mnuMenu1 = New System.Windows.Forms.MenuItem()
        Me.mnuMenu11 = New System.Windows.Forms.MenuItem()
        Me.mnuMenu12 = New System.Windows.Forms.MenuItem()
        Me.mnuMenu13 = New System.Windows.Forms.MenuItem()
        Me.mnuMenu14 = New System.Windows.Forms.MenuItem()
        Me.mnuMerge = New System.Windows.Forms.MenuItem()
        Me.mnuMenu2 = New System.Windows.Forms.MenuItem()
        Me.mnuMenu21 = New System.Windows.Forms.MenuItem()
        Me.mnuMenu22 = New System.Windows.Forms.MenuItem()
        Me.mnuMenu23 = New System.Windows.Forms.MenuItem()
        Me.mnuMenu24 = New System.Windows.Forms.MenuItem()
        Me.mnuSpecial = New System.Windows.Forms.MenuItem()
        Me.mnuODVote = New System.Windows.Forms.MenuItem()
        Me.mnuODShazam = New System.Windows.Forms.MenuItem()
        '
        'mainMenu1
        '
        Me.mainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuFile, Me.mnuEdit, Me.mnuOptions, Me.mnuRadioOptions, Me.mnuWindow, Me.mnuMenu1, Me.mnuMenu2, Me.mnuSpecial})
        '
        'mnuFile
        '
        Me.mnuFile.Index = 0
        Me.mnuFile.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuNew, Me.mnuFileOpen, Me.mnuFileClose, Me.mnuFileSave, Me.mnuFileSaveAs})
        Me.mnuFile.Text = "File"
        '
        'mnuNew
        '
        Me.mnuNew.Index = 0
        Me.mnuNew.Shortcut = System.Windows.Forms.Shortcut.CtrlN
        Me.mnuNew.Text = "&New"
        '
        'mnuFileOpen
        '
        Me.mnuFileOpen.Index = 1
        Me.mnuFileOpen.Text = "Open"
        '
        'mnuFileClose
        '
        Me.mnuFileClose.Index = 2
        Me.mnuFileClose.Text = "Close"
        '
        'mnuFileSave
        '
        Me.mnuFileSave.Index = 3
        Me.mnuFileSave.Text = "Save"
        '
        'mnuFileSaveAs
        '
        Me.mnuFileSaveAs.Index = 4
        Me.mnuFileSaveAs.Text = "Save&As"
        '
        'mnuEdit
        '
        Me.mnuEdit.Index = 1
        Me.mnuEdit.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuEditCopy, Me.mnuEditPaste})
        Me.mnuEdit.Text = "Edit"
        '
        'mnuEditCopy
        '
        Me.mnuEditCopy.Index = 0
        Me.mnuEditCopy.Text = "&Copy"
        '
        'mnuEditPaste
        '
        Me.mnuEditPaste.Index = 1
        Me.mnuEditPaste.Text = "Paste"
        '
        'mnuOptions
        '
        Me.mnuOptions.Index = 2
        Me.mnuOptions.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuOption1, Me.mnuOption2, Me.mnuOption3})
        Me.mnuOptions.Text = "Options"
        '
        'mnuOption1
        '
        Me.mnuOption1.Index = 0
        Me.mnuOption1.Text = "Option1"
        '
        'mnuOption2
        '
        Me.mnuOption2.Index = 1
        Me.mnuOption2.Text = "Option2"
        '
        'mnuOption3
        '
        Me.mnuOption3.Index = 2
        Me.mnuOption3.Text = "Option3"
        '
        'mnuRadioOptions
        '
        Me.mnuRadioOptions.Index = 3
        Me.mnuRadioOptions.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuRadioOption1, Me.mnuRadioOption2, Me.mnuRadioOption3})
        Me.mnuRadioOptions.Text = "Radio Options"
        '
        'mnuRadioOption1
        '
        Me.mnuRadioOption1.Index = 0
        Me.mnuRadioOption1.RadioCheck = True
        Me.mnuRadioOption1.Text = "Radio Option 1"
        '
        'mnuRadioOption2
        '
        Me.mnuRadioOption2.Index = 1
        Me.mnuRadioOption2.RadioCheck = True
        Me.mnuRadioOption2.Text = "Radio Option 2"
        '
        'mnuRadioOption3
        '
        Me.mnuRadioOption3.Index = 2
        Me.mnuRadioOption3.RadioCheck = True
        Me.mnuRadioOption3.Text = "Radio Option 3"
        '
        'mnuWindow
        '
        Me.mnuWindow.Index = 4
        Me.mnuWindow.MdiList = True
        Me.mnuWindow.MergeOrder = 99
        Me.mnuWindow.Text = "&Window"
        '
        'mnuMenu1
        '
        Me.mnuMenu1.Index = 5
        Me.mnuMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuMenu11, Me.mnuMenu12, Me.mnuMenu13, Me.mnuMenu14, Me.mnuMerge})
        Me.mnuMenu1.Text = "Menu 1"
        '
        'mnuMenu11
        '
        Me.mnuMenu11.Index = 0
        Me.mnuMenu11.MergeOrder = 1
        Me.mnuMenu11.Text = "Menu 1.1"
        '
        'mnuMenu12
        '
        Me.mnuMenu12.Index = 1
        Me.mnuMenu12.MergeOrder = 2
        Me.mnuMenu12.Text = "Menu 1.2"
        '
        'mnuMenu13
        '
        Me.mnuMenu13.Index = 2
        Me.mnuMenu13.MergeOrder = 3
        Me.mnuMenu13.Text = "Menu 1.3"
        '
        'mnuMenu14
        '
        Me.mnuMenu14.Index = 3
        Me.mnuMenu14.MergeOrder = 4
        Me.mnuMenu14.Text = "Menu 1.4"
        '
        'mnuMerge
        '
        Me.mnuMerge.Index = 4
        Me.mnuMerge.MergeOrder = 99
        Me.mnuMerge.Text = "Merge!"
        '
        'mnuMenu2
        '
        Me.mnuMenu2.Index = 6
        Me.mnuMenu2.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuMenu21, Me.mnuMenu22, Me.mnuMenu23, Me.mnuMenu24})
        Me.mnuMenu2.Text = "Menu 2"
        '
        'mnuMenu21
        '
        Me.mnuMenu21.Index = 0
        Me.mnuMenu21.MergeOrder = 1
        Me.mnuMenu21.Text = "Menu 2.1"
        '
        'mnuMenu22
        '
        Me.mnuMenu22.Index = 1
        Me.mnuMenu22.MergeOrder = 2
        Me.mnuMenu22.MergeType = System.Windows.Forms.MenuMerge.Replace
        Me.mnuMenu22.Text = "Menu 2.2"
        '
        'mnuMenu23
        '
        Me.mnuMenu23.Index = 2
        Me.mnuMenu23.MergeOrder = 3
        Me.mnuMenu23.MergeType = System.Windows.Forms.MenuMerge.Remove
        Me.mnuMenu23.Text = "Menu 2.3"
        '
        'mnuMenu24
        '
        Me.mnuMenu24.Index = 3
        Me.mnuMenu24.MergeOrder = 5
        Me.mnuMenu24.Text = "Menu 2.4"
        '
        'mnuSpecial
        '
        Me.mnuSpecial.Index = 7
        Me.mnuSpecial.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuODVote, Me.mnuODShazam})
        Me.mnuSpecial.Text = "Special"
        '
        'mnuODVote
        '
        Me.mnuODVote.Index = 0
        Me.mnuODVote.OwnerDraw = True
        Me.mnuODVote.Text = "Vote"
        '
        'mnuODShazam
        '
        Me.mnuODShazam.Index = 1
        Me.mnuODShazam.OwnerDraw = True
        Me.mnuODShazam.Text = "Shazam"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(456, 106)
        Me.IsMdiContainer = True
        Me.Menu = Me.mainMenu1
        Me.Name = "Form1"
        Me.Text = "Form1"

    End Sub

#End Region

    Private Sub mnuODVote_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) _
        Handles mnuODVote.DrawItem, mnuODShazam.DrawItem

        Dim img As Image
        img = Image.FromFile(files(e.Index))
        Dim r As Rectangle
        r = e.Bounds
        Dim p As Pen = New Pen(e.BackColor, 2)
        r.Inflate(-6, -6)
        e.Graphics.DrawRectangle(p, r)
        e.Graphics.DrawImage(img, r)
    End Sub

    Private Sub mnuODVote_MeasureItem(ByVal sender As Object, _
        ByVal e As System.Windows.Forms.MeasureItemEventArgs) _
        Handles mnuODVote.MeasureItem, mnuODShazam.MeasureItem

        Dim img As Image
        img = Image.FromFile(files(e.Index))
        e.ItemHeight = img.Height
        e.ItemWidth = img.Width

    End Sub

    Private Sub mnuODDraw_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) _
        Handles mnuODVote.Click, mnuODShazam.Click
        Dim item As MenuItem = CType(sender, MenuItem)
        Dim choice As String = item.Text
        MessageBox.Show("You clicked " & choice,"Menu Event Tester", MessageBoxButtons.OK, _
            MessageBoxIcon.Asterisk)
    End Sub
End Class








14.18.MenuItem
14.18.1.Add submenu to menuAdd submenu to menu
14.18.2.Set MenuItem visible and invisibleSet MenuItem visible and invisible
14.18.3.Set MenuItem to RadioCheck/CheckedSet MenuItem to RadioCheck/Checked
14.18.4.Add Menu and MenuItem in your codeAdd Menu and MenuItem in your code
14.18.5.Owner Draw MenuItemOwner Draw MenuItem