- Investigating Unsupported Controls in the .NET Compact Framework
- Investigating Unsupported System.Windows.Forms Functionality in the .NET Compact Framework
- Working with the Visual Studio .NET Form Designer
- Understanding the Different Windows Forms Target Platforms
- Working with the Form Control
- Programming the Button Control
- Using the TextBox Control
- Using the Label Control
- Working with RadioButton Controls
- Using the CheckBox Control
- Using the ComboBox Control
- Using the ListBox Control
- Using the NumericUpDown Control
- Using the DomainUpDown Control
- Programming the ProgressBar Control
- Using the StatusBar Control
- Using the TrackBar Control
- Using the ToolBar Control
- Adding Menus with the MainMenu Control
- Using a ContextMenu Control in an Application
- Using the Timer Control
- Using the OpenFileDialog and SaveFileDialog Controls
- Using the Panel Control
- Using the HScrollBar and VScrollBar Controls
- Using the ImageList Control
- Using the PictureBox Control
- Using the ListView Control
- Using the TabControl Control
- Using the TreeView Control
- Working with the DataGrid Control
- In Brief
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.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.