How to: Use Smartphone Menus

[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]

To conform to the Smartphone user interface, the .NET Compact Framework enforces the following menu restrictions:

  • You can only have two top-level menu items.

  • Only the second top-level menu item, on the right side of the form, can have submenus.

Note that the .NET Compact Framework does not enforce these restrictions at design time, but does throw a NotSupportedException at run time if your code does not follow them.

At run time, you cannot delete a top-level menu item. However, you can set Enabled property of a MenuItem to an empty string ("") to make a menu item appear invisible.

Visual Studio automatically adds a MainMenu component to your form when you create Smartphone and Pocket PC applications, but does not add it to child forms. The MainMenu component operates the Smartphone soft keys, but you cannot program their functionality unless you remove the MainMenu component from the form. For more information about programming soft keys, see Using Smartphone Back Key and Soft Keys.

To associate a method with a menu selection, provide code for the Click event for a MenuItem.

This example defines a menu system for a scenario of selecting maps:

  • On the left is the Map Help menu item, which has event handling code that displays a message box.

  • On the right is the Maps menu item, which has two children: My Maps and Add and Remove. These children have, respectively, five and two children of their own.


Imports System
Imports System.Windows.Forms

Public Class Form1
    Inherits System.Windows.Forms.Form
    Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu
    Private WithEvents mi1 As New MenuItem
    Private mi2 As New MenuItem
    Private miChildA As New MenuItem
    Private miChildB As New MenuItem
    Private WithEvents miGrandChildA1 As New MenuItem
    Private WithEvents miGrandChildA2 As New MenuItem
    Private WithEvents miGrandChildA3 As New MenuItem
    Private WithEvents miGrandChildA4 As New MenuItem
    Private WithEvents miGrandChildA5 As New MenuItem
    Private WithEvents miGrandChildB1 As New MenuItem
    Private WithEvents miGrandChildB2 As New MenuItem

    Public Sub New()
        MyBase.New()

        InitializeComponent()

        'Define and add menu items.
        MainMenu1.MenuItems.Add(mi1)
        MainMenu1.MenuItems.Add(mi2)
        mi2.MenuItems.Add(miChildA)
        mi2.MenuItems.Add(miChildB)
        miChildA.MenuItems.Add(miGrandChildA1)
        miChildA.MenuItems.Add(miGrandChildA2)
        miChildA.MenuItems.Add(miGrandChildA3)
        miChildA.MenuItems.Add(miGrandChildA4)
        miChildA.MenuItems.Add(miGrandChildA5)
        miChildB.MenuItems.Add(miGrandChildB1)
        miChildB.MenuItems.Add(miGrandChildB2)
        mi1.Text = "Map Help"
        mi2.Text = "Maps"
        miChildA.Text = "My Maps"
        miChildB.Text = "Add and remove"
        miGrandChildA1.Text = "Manhattan"
        miGrandChildA2.Text = "Bronx"
        miGrandChildA3.Text = "Brooklyn"
        miGrandChildA4.Text = "Queens"
        miGrandChildA5.Text = "Staten Island"
        miGrandChildB1.Text = "Add map"
        miGrandChildB2.Text = "Delete map"

    End Sub

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

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        MyBase.Dispose(disposing)
    End Sub

    Private Sub InitializeComponent()
        Me.MainMenu1 = New System.Windows.Forms.MainMenu()
        Me.Menu = Me.MainMenu1
        Me.Text = "Form1"
    End Sub

    ' The following subroutine handles the 
    ' Click event for the mi1 MenuItem.
    Private Sub mi1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mi1.Click
        MessageBox.Show("This is just a test.")
    End Sub

End Class


This example requires references to the following namespaces:

Show: