Adding Menus to a Form

Windows Forms supports menus and context menus. Main menus are displayed on a menu bar that is located immediately below the title bar of a form. The menu bar contains top-level menu items that are used to group related submenu items. For example, by clicking a File top-level menu item, you can display menu items that are related to file operations. Menu items typically appear as commands for your application (such as New and Open), but they can also appear as separator bars and submenu items. You can display a check mark next to a menu item to display the state of a command or a the state of a feature in your application. In Windows Forms, main menus are represented by the MainMenu control.

Context menus can be displayed for a specific control or area of your form. They are typically accessed by clicking the right mouse button. In Windows Forms, context menus are represented by the ContextMenu control.

ContextMenu and MainMenu derive from Menu. They share many properties, methods, and events.

Adding a MainMenu to a Form

The following code demonstrates how to add a MainMenu to a form.


Dim mainMenu As New MainMenu
Me.Menu = mainMenu
VB

Adding a Context Menu to a Control

The following code demonstrates how to create a ContextMenu and assign it to a control.


Dim label1ContextMenu As New ContextMenu
Dim label1 As New Label
label1.ContextMenu = label1ContextMenu
VB

Adding Menu Items

In the following example, a File menu item is added to the MainMenu. The File menu item contains submenu items called Open and Exit.


'Add File Menu
Dim miFile As MenuItem = mainMenu.MenuItems.Add("&File")
miFile.MenuItems.Add(new MenuItem("&Open..."))
miFile.MenuItems.Add("-")     ' Gives us a seperator
miFile.MenuItems.Add(new MenuItem("E&xit"))
VB


The following code demonstrates how to handle the Click event for both the Open and Exit menu items created in the previous code example.


....
'Add File Menu
Dim miFile As MenuItem = mainMenu.MenuItems.Add("&File")
miFile.MenuItems.Add(new MenuItem("&Open...", new EventHandler(AddressOf Me.FileOpen_Clicked)))
miFile.MenuItems.Add("-")     ' Gives us a seperator
miFile.MenuItems.Add(new MenuItem("E&xit", new EventHandler(AddressOf Me.FileExit_Clicked)))
....

'File->Exit Menu item handler
Private Sub FileExit_Clicked(sender As object, e As System.EventArgs)
    Me.Close
End Sub

'File->Open Menu item handler
Private Sub FileOpen_Clicked(sender As object, e As System.EventArgs)
    MessageBox.Show("And why would this open a file?")
End Sub

....
VB


The following example demonstrates how to define shortcut keys for the menu items created in the previous example.


....
'Add File Menu
Dim miFile As MenuItem = mainMenu.MenuItems.Add("&File")

miFile.MenuItems.Add(new MenuItem("&Open...", _
        New EventHandler(AddressOf Me.FileOpen_Clicked), Shortcut.CtrlO))

miFile.MenuItems.Add("-")     ' Gives us a seperator

miFile.MenuItems.Add(new MenuItem("E&xit", _
        New EventHandler(AddressOf Me.FileExit_Clicked), Shortcut.CtrlX))
....
VB

Adding Submenus

The following example demonstrates how to create submenus.


'Add Format Menu
Dim miFormat As MenuItem = mainMenu.MenuItems.Add("F&ormat")

'Font Face sub-menu
mmiSansSerif = New MenuItem("&1. " & sansSerifFontFamily.Name, New EventHandler(AddressOf Me.FormatFont_Clicked))
mmiSerif = New MenuItem("&2. " & serifFontFamily.Name, New EventHandler(AddressOf Me.FormatFont_Clicked))
mmiMonoSpace = New MenuItem("&3. " & monoSpaceFontFamily.Name, New EventHandler(AddressOf Me.FormatFont_Clicked))

miFormat.MenuItems.Add("Font &Face" _
                      , (New MenuItem() {mmiSansSerif, mmiSerif, mmiMonoSpace}))

'Font Size sub-menu
mmiSmall = New MenuItem("&Small", AddressOf Me.FormatSize_Clicked)
mmiMedium = New MenuItem("&Medium", AddressOf Me.FormatSize_Clicked)
mmiLarge = New MenuItem("&Large", AddressOf Me.FormatSize_Clicked)

miFormat.MenuItems.Add( "Font &Size" _
                      , New EventHandler(AddressOf Me.FormatSize_Clicked) _
                      , (New MenuItem(){ mmiSmall, mmiMedium, mmiLarge }) _
                      )
VB


Adding Default Menu Items

The following example demonstrates how to specify a default menu item.


'Font Face sub-menu
mmiSansSerif = New MenuItem("&1. " & sansSerifFontFamily.Name, New EventHandler(AddressOf Me.FormatFont_Clicked))
mmiSansSerif.DefaultItem = True
VB


Adding Check Marks to Menu Items

The following example demonstrates how to display a check mark next to a menu item. The code also demonstrates how to track which item is checked.


' Add Format Menu
Dim miMedium As MenuItem = new MenuItem("&Medium", AddressOf Me.FormatSize_Clicked)
miMedium.Checked = True

....


Private Sub FormatSize_Clicked(sender As object, e As System.EventArgs)
    Dim miClicked As MenuItem = CType(sender, MenuItem)

    ' Uncheck old selection
    miMainFormatSizeChecked.Checked = False

    ' Do Menu item action and make a new selection
    ....
    miMainFormatSizeChecked = miSmall
    ....

    ' Check the new selection
    miMainFormatSizeChecked.Checked = True
End Sub
VB


Cloning Menus

In many cases, the context menu for a control is a subset of the main menu. You cannot add the same menu items to multiple menus, but you can clone a menu item or set of menu items. The following code demonstrates how to clone the Format menu created previously and add it to the context menu of a Label.


'Add Format to label context menu
'Note have to add a clone because menus can't belong to 2 parents

label1ContextMenu.MenuItems.Add(miFormat.CloneMenu)
VB


Menu Merging

In an MDI application, the main menu of each MDI child form is merged into the main menu of the MDI parent form. See the MDI QuickStart for an example of menu merging.

Menu Sample

The following sample illustrates the features mentioned previously.

 
VB Menus and Context Menus

[Run Sample] | [View Source]


Copyright 2001 Microsoft Corporation. All rights reserved.