Previous Section  < Day Day Up >  Next Section

6.4. Working with Menus

The previous section provided a solid introduction to menus. This section adds a checklist of MenuItem properties that affect an item's appearance and describes how to use the ContextMenu class.

MenuItem Properties

The .NET menu system is designed with the utilitarian philosophy that the value of a thing depends on its utility. Its menu item is not a thing of beauty, but it works. Here are some of its more useful properties:

Enabled. Setting this to false, grays out the button and makes it unavailable.

Checked. Places a checkmark beside the menu item text.

RadioCheck. Places a radio button beside the menu item text; Checked must also be true.

BreakBar or Break. Setting this to true places the menu item in a new column.

Shortcut. Defines a shortcut key from one of the Shortcut enum members. These members represent a key or key combination (such as Shortcut.AltF10) that causes the menu item to be selected when the keys are pressed. On a related matter, note that you can also place an & in front of a letter in the menu item text to produce a hot key that causes the item to be selected by pressing Alt-letter.

Context Menus

In addition to the MainMenu and MenuItem classes that have been discussed, there is a ContextMenu class that also inherits from the Menu class. This ContextMenu class is associated with individual controls and is used most often to provide a contextsensitive pop-up menu when the user right-clicks on a control.

The statements to construct a menu based on ContextMenu are the same as with a MainMenu. The only difference is that visually there is no top-level menu bar, and the menu is displayed near the control where it is invoked.

A menu can be associated with multiple controls, or each control can have its own menu. Typically, one menu is used for each control type. For example, you might have a context menu for all

TextBox controls, and another for buttons. To illustrate, let's create a menu that colors the background of a TextBox control (see Figure 6-14).

Figure 6-14. Context menu

Constructing a Context Menu

Creating a context menu is similar to creating a MainMenu. If using VS.NET, you drag the ContextMenu control to the form and visually add menu items. If coding by hand, you create an instance of the ContextMenu class and add menu items using the MenuItems.Add method. Following is a sampling of the code used to create the menu. Note that a single method handles the Click event on each menu item.

private ContextMenu contextMenu1;  //Context menu 

private TextBox txtSearch;         //Text box that will use menu

// Following is in constructor

contextMenu1 = new ContextMenu();

// Add menu items and event handler using Add method

contextMenu1.MenuItems.Add("Azure Background", 

      new System.EventHandler(this.menuItem_Click));

contextMenu1.MenuItems.Add("White Background", 

      new System.EventHandler(this.menuItem_Click));

contextMenu1.MenuItems.Add("Beige Background", 

      new System.EventHandler(this.menuItem_Click));

The completed menu is attached to a control by setting the control's ContextMenu property to the context menu:

//Associate text box with a context menu

this.txtSearch.ContextMenu = this.contextMenu1;

A right-click on txtSearch causes the menu to pop up. Click one of the menu items and this event handling routine is called:

private void menuItem_Click(object sender, System.EventArgs e) { //Sender identifies specific menu item selected MenuItem conMi = (MenuItem) sender; string txt = conMi.Text; //SourceControl is control associated with this event if(txt == "Azure Background") this.contextMenu1.SourceControl.BackColor = Color .Azure; if(txt == "White Background") this.contextMenu1.SourceControl.BackColor = Color .White; if(txt == "Beige Background") this.contextMenu1.SourceControl.BackColor = Color .Beige; }

The two most important things to note in this example are that the argument sender identifies the selected menu item and that the context menu property SourceControl identifies the control associated with the event. This capability to identify the control and the menu item enables one method to handle events from any control on the form or any menu item in the context menu.

    Previous Section  < Day Day Up >  Next Section