VB.Net Tutorial/GUI/MenuItem — различия между версиями

Материал из VB Эксперт
Перейти к: навигация, поиск
м (1 версия)
 
м (1 версия)
 
(нет различий)

Текущая версия на 15:57, 26 мая 2010

Add Menu and MenuItem in your code

<source lang="vbnet">Imports System.IO Imports System.Windows.Forms public class MenuAddDynamically

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

End class

Public Class Form1

   Inherits System.Windows.Forms.Form
  Private ourMenu As MainMenu
  Private ourTop As MenuItem
  Private WithEvents ourItem As MenuItem
  Private Sub menuClick(ByVal sender As Object, ByVal e As System.EventArgs) 
     MessageBox.Show("You clicked " & sender.text & ".", _
     "Interactive Menu Creator!")
  End Sub
  1. 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 btnMainMenu As System.Windows.Forms.Button
  Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
  Friend WithEvents btnTopMnu As System.Windows.Forms.Button
  Friend WithEvents GroupBox2 As System.Windows.Forms.GroupBox
  Friend WithEvents btnAddItem As System.Windows.Forms.Button
  Friend WithEvents lstTopLevel As System.Windows.Forms.ListBox
  Friend WithEvents Label5 As System.Windows.Forms.Label
  Friend WithEvents txtItemText As System.Windows.Forms.TextBox
  Friend WithEvents txtTopLevel As System.Windows.Forms.TextBox
  "Required by the Windows Form Designer
  Private components As System.ruponentModel.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.
  <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
     Me.btnMainMenu = New System.Windows.Forms.Button()
     Me.GroupBox1 = New System.Windows.Forms.GroupBox()
     Me.txtTopLevel = New System.Windows.Forms.TextBox()
     Me.btnTopMnu = New System.Windows.Forms.Button()
     Me.GroupBox2 = New System.Windows.Forms.GroupBox()
     Me.Label5 = New System.Windows.Forms.Label()
     Me.lstTopLevel = New System.Windows.Forms.ListBox()
     Me.btnAddItem = New System.Windows.Forms.Button()
     Me.txtItemText = New System.Windows.Forms.TextBox()
     Me.GroupBox1.SuspendLayout()
     Me.GroupBox2.SuspendLayout()
     Me.SuspendLayout()
     "
     "btnMainMenu
     "
     Me.btnMainMenu.Location = New System.Drawing.Point(40, 16)
     Me.btnMainMenu.Name = "btnMainMenu"
     Me.btnMainMenu.Size = New System.Drawing.Size(128, 40)
     Me.btnMainMenu.TabIndex = 0
     Me.btnMainMenu.Text = "Create the MainMenu"
     "
     "GroupBox1
     "
     Me.GroupBox1.Controls.AddRange(New System.Windows.Forms.Control() {Me.txtTopLevel, Me.btnTopMnu})
     Me.GroupBox1.Location = New System.Drawing.Point(40, 80)
     Me.GroupBox1.Name = "GroupBox1"
     Me.GroupBox1.Size = New System.Drawing.Size(344, 104)
     Me.GroupBox1.TabIndex = 1
     Me.GroupBox1.TabStop = False
     Me.GroupBox1.Text = "Add top-level Menu text:"
     "
     "txtTopLevel
     "
     Me.txtTopLevel.Location = New System.Drawing.Point(24, 24)
     Me.txtTopLevel.Name = "txtTopLevel"
     Me.txtTopLevel.Size = New System.Drawing.Size(272, 20)
     Me.txtTopLevel.TabIndex = 5
     Me.txtTopLevel.Text = ""
     "
     "btnTopMnu
     "
     Me.btnTopMnu.Location = New System.Drawing.Point(200, 64)
     Me.btnTopMnu.Name = "btnTopMnu"
     Me.btnTopMnu.Size = New System.Drawing.Size(88, 24)
     Me.btnTopMnu.TabIndex = 4
     Me.btnTopMnu.Text = "Make it so!"
     "
     "GroupBox2
     "
     Me.GroupBox2.Controls.AddRange(New System.Windows.Forms.Control() {Me.Label5, Me.lstTopLevel, Me.btnAddItem, Me.txtItemText})
     Me.GroupBox2.Location = New System.Drawing.Point(40, 216)
     Me.GroupBox2.Name = "GroupBox2"
     Me.GroupBox2.Size = New System.Drawing.Size(344, 248)
     Me.GroupBox2.TabIndex = 2
     Me.GroupBox2.TabStop = False
     Me.GroupBox2.Text = "Add menu item text:"
     "
     "Label5
     "
     Me.Label5.Location = New System.Drawing.Point(32, 64)
     Me.Label5.Name = "Label5"
     Me.Label5.Size = New System.Drawing.Size(256, 16)
     Me.Label5.TabIndex = 6
     Me.Label5.Text = "Select menu the item goes on:"
     "
     "lstTopLevel
     "
     Me.lstTopLevel.Location = New System.Drawing.Point(16, 88)
     Me.lstTopLevel.Name = "lstTopLevel"
     Me.lstTopLevel.Size = New System.Drawing.Size(288, 108)
     Me.lstTopLevel.TabIndex = 5
     "
     "btnAddItem
     "
     Me.btnAddItem.Location = New System.Drawing.Point(216, 208)
     Me.btnAddItem.Name = "btnAddItem"
     Me.btnAddItem.Size = New System.Drawing.Size(88, 24)
     Me.btnAddItem.TabIndex = 4
     Me.btnAddItem.Text = "Add Item!"
     "
     "txtItemText
     "
     Me.txtItemText.Location = New System.Drawing.Point(56, 32)
     Me.txtItemText.Name = "txtItemText"
     Me.txtItemText.Size = New System.Drawing.Size(200, 20)
     Me.txtItemText.TabIndex = 3
     Me.txtItemText.Text = ""
     "
     "Form1
     "
     Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
     Me.ClientSize = New System.Drawing.Size(440, 510)
     Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.GroupBox2, Me.GroupBox1, Me.btnMainMenu})
     Me.Name = "Form1"
     Me.Text = "Interactive menu creator"
     Me.GroupBox1.ResumeLayout(False)
     Me.GroupBox2.ResumeLayout(False)
     Me.ResumeLayout(False)
  End Sub
  1. End Region
  Private Sub btnMainMenu_Click(ByVal sender As System.Object, _
      ByVal e As System.EventArgs) Handles btnMainMenu.Click
     ourMenu = New MainMenu()
     Me.Menu = ourMenu
  End Sub
  Private Sub btnTopMnu_Click(ByVal sender As System.Object, _
      ByVal e As System.EventArgs) Handles btnTopMnu.Click
     Dim itemString As String = txtTopLevel.Text
     ourTop = New MenuItem(itemString)
     ourMenu.MenuItems.Add(ourTop)
     lstTopLevel.Items.Add(itemString)
  End Sub
  Private Sub btnAddItem_Click(ByVal sender As System.Object, _
     ByVal e As System.EventArgs) Handles btnAddItem.Click
     Dim i As Integer
     i = 0
     ourItem = New MenuItem(txtItemText.Text, New System.EventHandler (AddressOf Me.menuClick))
     ourMenu.MenuItems(i).MenuItems.Add(ourItem)
  End Sub

End Class</source>

Add submenu to menu

<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class OwnerDrawMenuAndSubmenu

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

End class Public Class Form1

   Inherits System.Windows.Forms.Form
  1. 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.ruponentModel.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 MenuItem1 As System.Windows.Forms.MenuItem
   Friend WithEvents MenuItem2 As System.Windows.Forms.MenuItem
   Friend WithEvents MenuItem3 As System.Windows.Forms.MenuItem
   Friend WithEvents MenuItem4 As System.Windows.Forms.MenuItem
   Friend WithEvents MenuItem5 As System.Windows.Forms.MenuItem
   Friend WithEvents MenuItem6 As System.Windows.Forms.MenuItem
   Friend WithEvents MenuItem7 As System.Windows.Forms.MenuItem
   Friend WithEvents MenuItem8 As System.Windows.Forms.MenuItem
   Friend WithEvents MenuItem9 As System.Windows.Forms.MenuItem
   <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
       Me.MainMenu1 = New System.Windows.Forms.MainMenu
       Me.MenuItem1 = New System.Windows.Forms.MenuItem
       Me.MenuItem2 = New System.Windows.Forms.MenuItem
       Me.MenuItem7 = New System.Windows.Forms.MenuItem
       Me.MenuItem8 = New System.Windows.Forms.MenuItem
       Me.MenuItem9 = New System.Windows.Forms.MenuItem
       Me.MenuItem3 = New System.Windows.Forms.MenuItem
       Me.MenuItem4 = New System.Windows.Forms.MenuItem
       Me.MenuItem5 = New System.Windows.Forms.MenuItem
       Me.MenuItem6 = New System.Windows.Forms.MenuItem
       Me.SuspendLayout()
       "
       "MainMenu1
       "
       Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem1})
       "
       "MenuItem1
       "
       Me.MenuItem1.Index = 0
       Me.MenuItem1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem2, Me.MenuItem3, Me.MenuItem4, Me.MenuItem5, Me.MenuItem6})
       Me.MenuItem1.Text = "&File"
       "
       "MenuItem2
       "
       Me.MenuItem2.Index = 0
       Me.MenuItem2.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem7, Me.MenuItem8, Me.MenuItem9})
       Me.MenuItem2.Text = "Item 1"
       "
       "MenuItem7
       "
       Me.MenuItem7.Index = 0
       Me.MenuItem7.OwnerDraw = True
       Me.MenuItem7.Text = "Item 5"
       "
       "MenuItem8
       "
       Me.MenuItem8.Checked = True
       Me.MenuItem8.Index = 1
       Me.MenuItem8.Text = "Item 6"
       "
       "MenuItem9
       "
       Me.MenuItem9.Index = 2
       Me.MenuItem9.Text = "Item 7"
       "
       "MenuItem3
       "
       Me.MenuItem3.Index = 1
       Me.MenuItem3.Text = "Item 2"
       "
       "MenuItem4
       "
       Me.MenuItem4.Index = 2
       Me.MenuItem4.Text = "Item 3"
       "
       "MenuItem5
       "
       Me.MenuItem5.Index = 3
       Me.MenuItem5.Text = "-"
       "
       "MenuItem6
       "
       Me.MenuItem6.Index = 4
       Me.MenuItem6.Shortcut = System.Windows.Forms.Shortcut.CtrlX
       Me.MenuItem6.Text = "E&xit"
       "
       "Form1
       "
       Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
       Me.ClientSize = New System.Drawing.Size(292, 273)
       Me.Menu = Me.MainMenu1
       Me.Name = "Form1"
       Me.Text = "Form1"
       Me.ResumeLayout(False)
   End Sub
  1. End Region
   Private Sub MenuItem6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem6.Click
       End
   End Sub
   Private Sub MenuItem9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem9.Click
       MsgBox("You clicked Item 7.")
   End Sub
   Private Sub MenuItem8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem8.Click
       MenuItem8.Checked = Not MenuItem8.Checked
   End Sub
   Private Sub MenuItem4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem4.Click
       MenuItem4.Visible = False
   End Sub
   Private Sub MenuItem3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem3.Click
       MenuItem3.Enabled = False
   End Sub
   Private Sub MenuItem7_MeasureItem(ByVal sender As Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs) Handles MenuItem7.MeasureItem
       e.ItemHeight = 15
       e.ItemWidth = 60
   End Sub
   Private Sub MenuItem7_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles MenuItem7.DrawItem
       Dim penRed As New Pen(Color.Red)
       e.Graphics.DrawEllipse(penRed, e.Bounds)
   End Sub

End Class</source>

Owner Draw MenuItem

<source lang="vbnet">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"}
  1. 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.ruponentModel.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
  1. 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</source>

Set MenuItem to RadioCheck/Checked

<source lang="vbnet">Imports System.Windows.Forms public class DynamicMenu

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

End class Public Class Form1

   Inherits System.Windows.Forms.Form
   Public Sub New()
       MyBase.New()
       InitializeComponent()
   End Sub
   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
   Private components As System.ruponentModel.IContainer
   Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu
   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 MenuItem4 As System.Windows.Forms.MenuItem
   Friend WithEvents MenuItem5 As System.Windows.Forms.MenuItem
   Friend WithEvents MenuItem6 As System.Windows.Forms.MenuItem
   Friend WithEvents Button1 As System.Windows.Forms.Button
   Friend WithEvents Button2 As System.Windows.Forms.Button
   Friend WithEvents Button3 As System.Windows.Forms.Button
   Friend WithEvents Button4 As System.Windows.Forms.Button
   <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
       Me.MainMenu1 = New System.Windows.Forms.MainMenu
       Me.MenuItem1 = New System.Windows.Forms.MenuItem
       Me.MenuItem2 = New System.Windows.Forms.MenuItem
       Me.MenuItem3 = New System.Windows.Forms.MenuItem
       Me.MenuItem4 = New System.Windows.Forms.MenuItem
       Me.MenuItem5 = New System.Windows.Forms.MenuItem
       Me.MenuItem6 = New System.Windows.Forms.MenuItem
       Me.Button1 = New System.Windows.Forms.Button
       Me.Button2 = New System.Windows.Forms.Button
       Me.Button3 = New System.Windows.Forms.Button
       Me.Button4 = New System.Windows.Forms.Button
       Me.SuspendLayout()
       "
       "MainMenu1
       "
       Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem1, Me.MenuItem3})
       "
       "MenuItem1
       "
       Me.MenuItem1.Index = 0
       Me.MenuItem1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem2})
       Me.MenuItem1.Text = "File"
       "
       "MenuItem2
       "
       Me.MenuItem2.Index = 0
       Me.MenuItem2.Text = "Open"
       "
       "MenuItem3
       "
       Me.MenuItem3.Index = 1
       Me.MenuItem3.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem4, Me.MenuItem5, Me.MenuItem6})
       Me.MenuItem3.Text = "Edit"
       "
       "MenuItem4
       "
       Me.MenuItem4.Index = 0
       Me.MenuItem4.Text = "Cut"
       "
       "MenuItem5
       "
       Me.MenuItem5.Index = 1
       Me.MenuItem5.Text = "Copy"
       "
       "MenuItem6
       "
       Me.MenuItem6.Index = 2
       Me.MenuItem6.Text = "Paste"
       "
       "Button1
       "
       Me.Button1.Location = New System.Drawing.Point(200, 8)
       Me.Button1.Name = "Button1"
       Me.Button1.Size = New System.Drawing.Size(72, 24)
       Me.Button1.TabIndex = 3
       Me.Button1.Text = "Select"
       "
       "Button2
       "
       Me.Button2.Location = New System.Drawing.Point(200, 48)
       Me.Button2.Name = "Button2"
       Me.Button2.Size = New System.Drawing.Size(72, 24)
       Me.Button2.TabIndex = 4
       Me.Button2.Text = "Disable"
       "
       "Button3
       "
       Me.Button3.Location = New System.Drawing.Point(200, 88)
       Me.Button3.Name = "Button3"
       Me.Button3.Size = New System.Drawing.Size(72, 24)
       Me.Button3.TabIndex = 5
       Me.Button3.Text = "RadioButton"
       "
       "Button4
       "
       Me.Button4.Location = New System.Drawing.Point(200, 136)
       Me.Button4.Name = "Button4"
       Me.Button4.Size = New System.Drawing.Size(72, 24)
       Me.Button4.TabIndex = 6
       Me.Button4.Text = "Hide"
       "
       "Form1
       "
       Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
       Me.ClientSize = New System.Drawing.Size(328, 225)
       Me.Controls.Add(Me.Button4)
       Me.Controls.Add(Me.Button3)
       Me.Controls.Add(Me.Button2)
       Me.Controls.Add(Me.Button1)
       Me.Menu = Me.MainMenu1
       Me.ResumeLayout(False)
   End Sub
   Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       MenuItem4.RadioCheck = False
       MenuItem5.RadioCheck = False
       MenuItem6.RadioCheck = False
       MenuItem6.Checked = MenuItem4.Checked
       MenuItem4.Checked = Not MenuItem4.Checked
       MenuItem5.Checked = Not MenuItem5.Checked
       MenuItem6.Checked = Not MenuItem6.Checked
   End Sub
   Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
       MenuItem4.RadioCheck = False
       MenuItem5.RadioCheck = False
       MenuItem6.RadioCheck = False
       MenuItem6.Checked = MenuItem4.Checked
       MenuItem4.Enabled = Not MenuItem4.Enabled
       MenuItem5.Enabled = Not MenuItem5.Enabled
       MenuItem6.Enabled = Not MenuItem6.Enabled
   End Sub
   Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
       MenuItem4.Checked = False
       MenuItem5.Checked = False
       MenuItem6.Checked = True
       MenuItem6.RadioCheck = True
   End Sub
   Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
       MenuItem6.Visible = Not MenuItem6.Visible
   End Sub

End Class</source>

Set MenuItem visible and invisible

<source lang="vbnet">Imports System.Windows.Forms public class DynamicMenu

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

End class Public Class Form1

   Inherits System.Windows.Forms.Form
   Public Sub New()
       MyBase.New()
       InitializeComponent()
   End Sub
   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
   Private components As System.ruponentModel.IContainer
   Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu
   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 MenuItem4 As System.Windows.Forms.MenuItem
   Friend WithEvents MenuItem5 As System.Windows.Forms.MenuItem
   Friend WithEvents MenuItem6 As System.Windows.Forms.MenuItem
   Friend WithEvents Button1 As System.Windows.Forms.Button
   Friend WithEvents Button2 As System.Windows.Forms.Button
   Friend WithEvents Button3 As System.Windows.Forms.Button
   Friend WithEvents Button4 As System.Windows.Forms.Button
   <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
       Me.MainMenu1 = New System.Windows.Forms.MainMenu
       Me.MenuItem1 = New System.Windows.Forms.MenuItem
       Me.MenuItem2 = New System.Windows.Forms.MenuItem
       Me.MenuItem3 = New System.Windows.Forms.MenuItem
       Me.MenuItem4 = New System.Windows.Forms.MenuItem
       Me.MenuItem5 = New System.Windows.Forms.MenuItem
       Me.MenuItem6 = New System.Windows.Forms.MenuItem
       Me.Button1 = New System.Windows.Forms.Button
       Me.Button2 = New System.Windows.Forms.Button
       Me.Button3 = New System.Windows.Forms.Button
       Me.Button4 = New System.Windows.Forms.Button
       Me.SuspendLayout()
       "
       "MainMenu1
       "
       Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem1, Me.MenuItem3})
       "
       "MenuItem1
       "
       Me.MenuItem1.Index = 0
       Me.MenuItem1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem2})
       Me.MenuItem1.Text = "File"
       "
       "MenuItem2
       "
       Me.MenuItem2.Index = 0
       Me.MenuItem2.Text = "Open"
       "
       "MenuItem3
       "
       Me.MenuItem3.Index = 1
       Me.MenuItem3.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem4, Me.MenuItem5, Me.MenuItem6})
       Me.MenuItem3.Text = "Edit"
       "
       "MenuItem4
       "
       Me.MenuItem4.Index = 0
       Me.MenuItem4.Text = "Cut"
       "
       "MenuItem5
       "
       Me.MenuItem5.Index = 1
       Me.MenuItem5.Text = "Copy"
       "
       "MenuItem6
       "
       Me.MenuItem6.Index = 2
       Me.MenuItem6.Text = "Paste"
       "
       "Button1
       "
       Me.Button1.Location = New System.Drawing.Point(200, 8)
       Me.Button1.Name = "Button1"
       Me.Button1.Size = New System.Drawing.Size(72, 24)
       Me.Button1.TabIndex = 3
       Me.Button1.Text = "Select"
       "
       "Button2
       "
       Me.Button2.Location = New System.Drawing.Point(200, 48)
       Me.Button2.Name = "Button2"
       Me.Button2.Size = New System.Drawing.Size(72, 24)
       Me.Button2.TabIndex = 4
       Me.Button2.Text = "Disable"
       "
       "Button3
       "
       Me.Button3.Location = New System.Drawing.Point(200, 88)
       Me.Button3.Name = "Button3"
       Me.Button3.Size = New System.Drawing.Size(72, 24)
       Me.Button3.TabIndex = 5
       Me.Button3.Text = "RadioButton"
       "
       "Button4
       "
       Me.Button4.Location = New System.Drawing.Point(200, 136)
       Me.Button4.Name = "Button4"
       Me.Button4.Size = New System.Drawing.Size(72, 24)
       Me.Button4.TabIndex = 6
       Me.Button4.Text = "Hide"
       "
       "Form1
       "
       Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
       Me.ClientSize = New System.Drawing.Size(328, 225)
       Me.Controls.Add(Me.Button4)
       Me.Controls.Add(Me.Button3)
       Me.Controls.Add(Me.Button2)
       Me.Controls.Add(Me.Button1)
       Me.Menu = Me.MainMenu1
       Me.ResumeLayout(False)
   End Sub
   Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       MenuItem4.RadioCheck = False
       MenuItem5.RadioCheck = False
       MenuItem6.RadioCheck = False
       MenuItem6.Checked = MenuItem4.Checked
       MenuItem4.Checked = Not MenuItem4.Checked
       MenuItem5.Checked = Not MenuItem5.Checked
       MenuItem6.Checked = Not MenuItem6.Checked
   End Sub
   Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
       MenuItem4.RadioCheck = False
       MenuItem5.RadioCheck = False
       MenuItem6.RadioCheck = False
       MenuItem6.Checked = MenuItem4.Checked
       MenuItem4.Enabled = Not MenuItem4.Enabled
       MenuItem5.Enabled = Not MenuItem5.Enabled
       MenuItem6.Enabled = Not MenuItem6.Enabled
   End Sub
   Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
       MenuItem4.Checked = False
       MenuItem5.Checked = False
       MenuItem6.Checked = True
       MenuItem6.RadioCheck = True
   End Sub
   Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
       MenuItem6.Visible = Not MenuItem6.Visible
   End Sub

End Class</source>