Visual Basic .NET - Menu Control

The VB.NET Menu Control is used to create menus in a Windows form application. It provides various options to create hierarchical menus, shortcuts, submenus, and separators. Here are some of the basic properties, methods, and events of the VB.NET Menu Control:

Properties

  • Name : Gets or sets the name of the control.
  • Text: Gets or sets the text associated with the control.
  • MenuItems: Gets the collection of items contained in the menu.
  • Enabled: Gets or sets a value indicating whether the control can respond to user interaction.
  • Visible: Gets or sets a value indicating whether the control and all its child controls are displayed.

Methods

  • Add: Adds a new menu item to the menu.
  • Remove: Removes the specified menu item from the menu.
  • PerformClick: Raises the Click event for the menu item.

Events

  • Click: Occurs when the menu item is clicked.
  • Popup: Occurs when the menu is about to be displayed.
  • Select: Occurs when the menu item is selected.
Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' Create a new menu item
        Dim menuItem As New MenuItem("File")
        ' Add sub menu items to it
        Dim subMenuItem1 As New MenuItem("Open")
        Dim subMenuItem2 As New MenuItem("Save")
        menuItem.MenuItems.Add(subMenuItem1)
        menuItem.MenuItems.Add(subMenuItem2)
        ' Add the main menu item to the menu
        MainMenu1.MenuItems.Add(menuItem)
    End Sub
End Class

This code creates a menu with a "File" menu item, which has "Open" and "Save" as sub-menu items. The menu is added to the form using the MainMenu control.

Creating a drop-down menu:

Private Sub CreateDropDownMenu()
    ' Create a new menu item
    Dim fileMenuItem As New ToolStripMenuItem("File")
    ' Create sub-items and add them to the menu
    Dim newMenuItem As New ToolStripMenuItem("New")
    fileMenuItem.DropDownItems.Add(newMenuItem)
    Dim openMenuItem As New ToolStripMenuItem("Open")
    fileMenuItem.DropDownItems.Add(openMenuItem)
    Dim saveMenuItem As New ToolStripMenuItem("Save")
    fileMenuItem.DropDownItems.Add(saveMenuItem)
    ' Add the menu to the form
    MenuStrip1.Items.Add(fileMenuItem)
End Sub

Adding keyboard shortcuts to menu items:

Private Sub AddKeyboardShortcuts()
    ' Set the shortcut key for the "New" menu item
    Dim newMenuItem As ToolStripMenuItem = CType(MenuStrip1.Items("File").DropDownItems("New"), ToolStripMenuItem)
    newMenuItem.ShortcutKeys = CType((Keys.Control Or Keys.N), Keys)
    ' Set the shortcut key for the "Open" menu item
    Dim openMenuItem As ToolStripMenuItem = CType(MenuStrip1.Items("File").DropDownItems("Open"), ToolStripMenuItem)
    openMenuItem.ShortcutKeys = CType((Keys.Control Or Keys.O), Keys)
    ' Set the shortcut key for the "Save" menu item
    Dim saveMenuItem As ToolStripMenuItem = CType(MenuStrip1.Items("File").DropDownItems("Save"), ToolStripMenuItem)
    saveMenuItem.ShortcutKeys = CType((Keys.Control Or Keys.S), Keys)
End Sub

Creating a context menu:

Private Sub CreateContextMenu()
    ' Create a new context menu
    Dim contextMenu As New ContextMenuStrip()
    ' Create menu items and add them to the context menu
    Dim cutMenuItem As New ToolStripMenuItem("Cut")
    contextMenu.Items.Add(cutMenuItem)
    Dim copyMenuItem As New ToolStripMenuItem("Copy")
    contextMenu.Items.Add(copyMenuItem)
    Dim pasteMenuItem As New ToolStripMenuItem("Paste")
    contextMenu.Items.Add(pasteMenuItem)
    ' Associate the context menu with a control
    TextBox1.ContextMenuStrip = contextMenu
End Sub

Adding images to menu items:

Private Sub AddImages()
    ' Set the image for the "New" menu item
    Dim newMenuItem As ToolStripMenuItem = CType(MenuStrip1.Items("File").DropDownItems("New"), ToolStripMenuItem)
    newMenuItem.Image = My.Resources.NewIcon
    ' Set the image for the "Open" menu item
    Dim openMenuItem As ToolStripMenuItem = CType(MenuStrip1.Items("File").DropDownItems("Open"), ToolStripMenuItem)
    openMenuItem.Image = My.Resources.OpenIcon
    ' Set the image for the "Save" menu item
    Dim saveMenuItem As ToolStripMenuItem = CType(MenuStrip1.Items("File").DropDownItems("Save"), ToolStripMenuItem)
    saveMenuItem.Image = My.Resources.SaveIcon
End Sub