Implementing Shortcut Menus

Implementing Shortcut Menus

Unlike a main menu, which is displayed at all times, a shortcut menu is a special menu that’s displayed only when the user requests it by right-clicking a form or control or by pressing the shortcut menu key that’s found on some newer keyboards. This menu appears as a popup menu, usually near the insertion point or the mouse pointer. A shortcut menu typically provides access to specific, context-sensitive operations that can be performed on the object that was clicked, rather than accessing general tasks that are carried out by the application. For example, when you right-click in Internet Explorer, the shortcut menu changes based on the type of object that was clicked—the shortcut menu displayed for a hyperlink is different from the shortcut menu displayed for an image. In this section, we’ll look at how to create and use shortcut menus in an application.

Creating Basic Shortcut Menus

The ContextMenu class is used to create a shortcut menu. As with MainMenu objects, an instance of the ContextMenu class must contain one or more instances of the MenuItem class to be usable. And as with main menus, you can build shortcut menus programmatically or by using the ContextMenu control and the Menu Designer.

All controls and forms expose the ContextMenu property, which is used to associate the form or control with a shortcut menu. The following code creates an instance of a shortcut menu and assigns it to a list box control:

EventHandler helloHandler = new EventHandler(hello_Click);
EventHandler goodbyeHandler = new EventHandler(goodbye_Click);
ContextMenu listboxContextMenu = new ContextMenu();
MenuItem helloMenuItem = new MenuItem("&Hello", helloHandler);
MenuItem goodbyeMenuItem = new MenuItem("&Goodbye", goodbyeHandler);

MenuItem[] contextMenuItemArray = new MenuItem [] { helloMenuItem,
                                                    goodbyeMenuItem };
listboxContextMenu.MenuItems.AddRange(contextMenuItemArray);
listBox.ContextMenu = listboxContextMenu;

As this code shows, the ContextMenu class is used much like the MainMenu class. The menu items are represented by MenuItem objects, and the menu items are stored in a collection that’s accessed through the MenuItems property. Just as with items in a MainMenu object, the items stored in a shortcut menu will raise Click, Popup, and Selected events.

The ContextMenu control can be used much like the MainMenu control. The only difference you’ll notice is that the Menu Designer doesn’t allow you to add new menu items to the top level of the menu. Because shortcut menus are just popup menus, there’s no need to have any top-level menu items.

Programming with Shortcut Menus

Although you can associate a shortcut menu with a form, it’s most often associated with a control. For example, a text box control has a built-in shortcut menu that enables a user to interact with the Windows clipboard. Many controls can benefit greatly from the added functionality and user-friendliness that a shortcut menu provides. The ControlContextMenu project on the companion CD, shown in Figure 13-5, has a list box control that provides a shortcut menu.

Figure 13-5.
The ControlContextMenu example application.

The list box control on the main form of ControlContextMenu provides a shortcut menu that’s displayed when the user right-clicks the control. This shortcut menu enables the user to add, remove, clear, and sort items in the list box. Properties for the shortcut menu are listed in Table 13-16.

The following code contains the Click event handlers for the shortcut menu:

private void sortMenuItem_Click(object sender, System.EventArgs e)
{
    listBox.Sorted = !listBox.Sorted;
}
private void addMenuItem_Click(object sender, System.EventArgs e)
{
    AddItem();
}
private void removeMenuItem_Click(object sender, System.EventArgs e)
{
    listBox.Items.RemoveAt(listBox.SelectedIndex);
}
private void clearMenuItem_Click(object sender, System.EventArgs e)
{
    listBox.Items.Clear();
}
private void AddItem()
{
    
    

}

Most of the Click event handlers shown in this code are mapped directly to methods exposed by the ListBox class. The exception is the handler for adding a new menu item, which invokes another form and isn’t fully shown here.

Shortcut menus can also be updated dynamically. In this example, when the list box control is empty, the menu items that enable sorting or removal of the list box contents should be disabled. The Popup event handler method from the ControlContextMenu project is shown in the following code:

private void listBoxMenu_Popup(object sender, System.EventArgs e)
{
    if(listBox.Items.Count == 0)
    {
        sortMenuItem.Enabled = false;
        removeMenuItem.Enabled = false;
        clearMenuItem.Enabled = false;
    }
    else
    {
        if(listBox.SelectedIndex == -1)
            removeMenuItem.Enabled = false;
        else
            removeMenuItem.Enabled = true;

        sortMenuItem.Enabled = true;
        clearMenuItem.Enabled = true;
    }
    sortMenuItem.Checked = listBox.Sorted;
}

This code starts by determining the number of items in the list box control. If there’s at least one item in the control, the Sort and Clear All menu items are enabled. A further test is made before enabling the Remove menu item, which is used to remove individual items from the list box control; it’s enabled only if an item is selected. A check mark is provided for the Sort menu item that follows the list box control’s Sorted property and is set independently of whether the menu item is enabled or disabled. If the list box is sorted, the menu item is checked; if the control is unsorted, the menu item is unchecked.



Part III: Programming Windows Forms