共用方式為


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.

Example

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
using System;
using System.Windows.Forms;

namespace SmartphoneMenus
{
    public class Form1 : System.Windows.Forms.Form
    {
        private System.Windows.Forms.MainMenu mainMenu1;

        private MenuItem mi1 = new MenuItem();
        private MenuItem mi2 = new MenuItem();
        private MenuItem miChildA = new MenuItem();
        private MenuItem miChildB = new MenuItem();
        private MenuItem miGrandChildA1 = new MenuItem();
        private MenuItem miGrandChildA2 = new MenuItem();
        private MenuItem miGrandChildA3 = new MenuItem();
        private MenuItem miGrandChildA4 = new MenuItem();
        private MenuItem miGrandChildA5 = new MenuItem();
        private MenuItem miGrandChildB1 = new MenuItem();
        private MenuItem miGrandChildB2 = new MenuItem();

        public Form1()
        {
            InitializeComponent();


            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);

            // Event handler for the top left menu.
            mi1.Click +=new EventHandler(mi1_Click);

           // Event handlers for grandchild menu items. This code is commented out
           // because this example does not define their event handling methods.
            // miGrandChildA1.Click +=new EventHandler(miGrandChildA1_Click);
            // miGrandChildB1.Click +=new EventHandler(miGrandChildB1_Click);
            // miGrandChildB2.Click +=new EventHandler(miGrandChildB2_Click);
            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 = "Remove map";

        }
        protected override void Dispose( bool disposing )
        {
            base.Dispose( disposing );
        }

        private void InitializeComponent()
        {
            this.mainMenu1 = new System.Windows.Forms.MainMenu();
            this.Menu = this.mainMenu1;
            this.Text = "Form1";

        }

        static void Main()
        {
            Application.Run(new Form1());
        }

        // The following method handles the
        // Click event for the mi1 MenuItem.
        private void mi1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("This is just a test.");
        }
    }
}

Compiling the Code

This example requires references to the following namespaces:

See Also

Other Resources

Smartphone Development and the .NET Compact Framework