Add Menu and MenuItem in your code : MenuItem « GUI « VB.Net Tutorial






Add Menu and MenuItem in your code
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



#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.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.
   <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

#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








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