Export (0) Print
Expand All

How to: Use Smartphone Menus

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.

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

Community Additions

ADD
Show:
© 2015 Microsoft