Home > Articles

This chapter is from the book

Adding Menus with the MainMenu Control

Menus are an easy and efficient way to present options to your users. Menus are handled differently, depending on whether the application is running on the Pocket PC OS or the Windows CE OS. By default a Pocket PC application will contain a MainMenu control. A Pocket PC application displays menus at the bottom of the application. Windows CE applications have no default menu, and menus are displayed at the top of the application, like standard desktop applications.

The MainMenu control is a container control that holds all of the MenuItem controls in the application. Menus with sub-items can be created by adding multiple submenu items to a top-level MenuItem.

You can add menus to your application at design time through the menu designer or at runtime. The following code shows how to add a menu item with two submenu items to the MainMenu control named mainMenu1. The code also demonstrates how to add a separator to a menu. This is done by creating a new MenuItem object and setting its text to a string containing a single hyphen character, "-".

C#
MenuItem fileMenu = new MenuItem();
MenuItem newItem = new MenuItem();
MenuItem sepItem = new MenuItem();
MenuItem exitItem = new MenuItem();

fileMenu.Text = "File";
newItem.Text = "New";
sepItem.Text = "-";
exitItem.Text = "Exit";

fileMenu.MenuItems.Add(newItem);
fileMenu.MenuItems.Add(sepItem);
fileMenu.MenuItems.Add(exitItem);
mainMenu1.MenuItems.Add(fileMenu);
VB
Dim fileMenu = new MenuItem()
Dim newItem = new MenuItem()
Dim sepItem = new MenuItem()
Dim exitItem = new MenuItem()

fileMenu.Text = "File"
newItem.Text = "New"
sepItem.Text = "-"
exitItem.Text = "Exit"

fileMenu.MenuItems.Add(newItem)
fileMenu.MenuItems.Add(sepItem)
fileMenu.MenuItems.Add(exitItem)
mainMenu1.MenuItems.Add(fileMenu)

Figure 3.19 shows the menu being displayed on the Pocket PC 2002 emulator.

Figure 3.19Figure 3.19 A sample application that showcases the MainMenu control running on the Pocket PC 2002 emulator.

When the user selects a menu item, a Click event is fired. You would handle this event just as you handled the Button control's Click event. See the "Handling a ToolBar's ButtonClick Event" section for details.

  • + Share This
  • 🔖 Save To Your Account