Home > Articles > Programming > User Interface (UI)

This chapter is from the book

Using a ContextMenu Control in an Application

The ContextMenu control is almost exactly like the MainMenu control except that the context menus are associated with other controls, whereas the MainMenu is associated with the application's form. ContextMenus are also known as pop-up menus that appear when you tap-and-hold a control on Pocket PC or right-click a control on Windows CE. Tap-and-hold can be simulated on the Pocket PC emulator by pressing and holding the left mouse button.

To add a ContextMenu to an application, drag the control from the ToolBox onto the application. The new ContextMenu control will appear in the panel at the bottom of the Form Designer. When the ContextMenu is selected, the Form Designer paints the control in the same place as the MainMenu control. This is done only for design purposes. At runtime the ContextMenu will appear above its respective control. To add MenuItem to the ContextMenu, click the ContextMenu on the Form or click the Edit Menu link in the Properties window. The Edit Menu link is visible only when the ContextMenu is selected.

You can also create ContextMenu controls at runtime. First, a ContextMenu control must be instantiated. Next add MenuItem objects to it, and then set the ContextMenu property of a control to the ContextMenu instance. The following code demonstrates how to add a ContextMenu at runtime:

C#
ContextMenu cMenu = new ContextMenu();
MenuItem menuItem1 = new MenuItem();
MenuItem menuItem2 = new MenuItem();

menuItem1.Text = "Default Item 1";
menuItem2.Text = "Default Item 2";

// Add menuItem2 as a child of menuItem1
menuItem1.MenuItems.Add(this.menuItem2);

// Add menuItem1 to the context menu
cMenu.MenuItems.Add(this.menuItem1);

// Add the context menu to a label control
label1.ContextMenu = cMenu;
VB
Dim cMenu = new ContextMenu()
Dim menuItem1 = new MenuItem()
Dim menuItem2 = new MenuItem()

menuItem1.Text = "Default Item 1"
menuItem2.Text = "Default Item 2"

' Add menuItem2 as a child of menuItem1
menuItem1.MenuItems.Add(menuItem2)

' Add menuItem1 to the context menu
cMenu.MenuItems.Add(menuItem1)

' Add the context menu to a label control
label1.ContextMenu = cMenu

When a ContextMenu is invoked, a Popup event is fired. This event can be handled to customize the ContextMenu before it is shown to the user. When a MenuItem is selected from the ContextMenu, a Click event is fired for the MenuItem. Listing 3.3 demonstrates how to handle both events:

Listing 3.3

C#
private void contextMenu1_Popup(object sender, System.EventArgs e) {
 this.contextMenu1.MenuItems.Clear();
 if(this.checkBox1.Checked)
  this.contextMenu1.MenuItems.Add(this.menuItem1);
 if(this.checkBox2.Checked)
  this.contextMenu1.MenuItems.Add(this.menuItem2);
 if(this.checkBox3.Checked)
  this.contextMenu1.MenuItems.Add(this.menuItem3);

 // Always add the default menu 
 this.contextMenu1.MenuItems.Add(this.menuItem4);
}

private void menuItem1_Click(object sender, System.EventArgs e) {
 MessageBox.Show("You selected MenuItem 1");
}

private void menuItem2_Click(object sender, System.EventArgs e) {
 MessageBox.Show("You selected MenuItem 2");
}

private void menuItem5_Click(object sender, System.EventArgs e) {
 MessageBox.Show("You selected MenuItem 3");
}

private void menuItem3_Click(object sender, System.EventArgs e) {
 MessageBox.Show("You selected MenuItem 3");
}

private void menuItem5_Click_1(object sender, System.EventArgs e) {
 MessageBox.Show("You selected Default Item 2");
}
VB
Private Sub _
    contextMenu1_Popup(ByVal sender As Object,
    ByVal e As System.EventArgs) _Handles contextMenu1.Popup
 contextMenu1.MenuItems.Clear()

 If checkBox1.Checked Then
  contextMenu1.MenuItems.Add(menuItem1)

  If checkBox2.Checked Then
   contextMenu1.MenuItems.Add(menuItem2)
  End If

  If checkBox3.Checked Then
   contextMenu1.MenuItems.Add(menuItem3)
  End If

  ' Always add the default menu 
  contextMenu1.MenuItems.Add(menuItem4)
 End If
End Sub

Private Sub menuItem1_Click(ByVal sender As Object,
    ByVal e As System.EventArgs) _Handles menuItem1.Click
 MessageBox.Show("You selected MenuItem 1")
End Sub

Private Sub menuItem2_Click(ByVal sender As Object,
    ByVal e As System.EventArgs) _Handles menuItem2.Click
 MessageBox.Show("You selected MenuItem 2")
End Sub

Private Sub menuItem3_Click(ByVal sender As Object,
    ByVal e As System.EventArgs) _Handles menuItem3.Click
 MessageBox.Show("You selected MenuItem 3")
End Sub

Private Sub menuItem4_Click(ByVal sender As Object,
    ByVal e As System.EventArgs) _Handles menuItem4.Click
 MessageBox.Show("You selected MenuItem 4")
End Sub

Private Sub menuItem5_Click(ByVal sender As Object,
    ByVal e As System.EventArgs) _Handles menuItem5.Click
 MessageBox.Show("You selected MenuItem 5")
End Sub

Figure 3.20 shows an application running that uses a ContextMenu control. The MenuItems on the control are configured when it is activated. The CheckBox controls determine which MenuItems will be displayed. The Default Item 1 menu is always displayed. You will find the code for this sample in the code listings for this book.

Figure 3.20Figure 3.20 An application that showcases the ContextMenu control running on the Pocket PC 2002 emulator.

  • + Share This
  • 🔖 Save To Your Account