Adding a Separator

You don't add a separator bar to a menu item as you do a shortcut key, access key, or check mark. Instead, you add a shortcut key as a menu item. This also can be done at design time or runtime. When setting the Text property in the Properties window, the Menu Designer, or in code, enter a hyphen to make that menu item a separator bar. Alternatively in the Menu Designer, right-click the location where you want a separator bar, and choose New Separator.

Disabling Items on Windows Forms Menus

Microsoft Word contains the menu items Cut and Copy under the Edit menu. Initially, Cut and Copy are grayed out (disabled). They are grayed out because no text is selected, therefore, there is nothing to cut or copy. However, once you select text, Cut and Copy are no longer grayed out, or in other words, are enabled.

A menu item should not be enabled when the command it represents should not be available. It would be frustrating for the application user to click Cut or Copy and see nothing happen. The application user might be misled into thinking there is something wrong with your application. When you gray out, or disable, the menu item, the application user is given a visual cue that the menu item is not available.

Disabling a menu item that should not be available has an additional advantage, error prevention. The code for cutting text may understandably assume there is selected text. If there is no selected text, executing th e code for cutting text may cause an error. By disabling the menu item when no text is selected, the code for cutting text cannot be executed when no text is selected, thus avoiding the error.

Menu items are enabled by default when they are created. However, you can disable a menu item by setting its Enabled property to False. You can do this at design time, when the menu item is selected in the Menu Designer, and through the Properties window. You also can do this programmatically:

mnuItemNew.Enabled = False

Disabling the first or top-level menu item in a menu, such as the File menu item in a traditional File menu, disables all the menu items contained within the menu. Similarly, disabling a menu item that has submenu items disables the submenu items.

If all the commands on a given menu are unavailable to the user, you should hide as well as disable the entire menu. You hide the menu by setting the Visible property of the topmost menu item to False. This presents a cleaner user interface by not cluttering up your menu structure with disabled items.

Caution Hiding the menu alone is not sufficient. You must also disable the menu, because hiding alone does not prevent access to a menu command via a shortcut key.

1-Minute Drill

■ What class do you use to create a menu? You use the MainMenu class to create a menu

■ How do you add a menu item to a menu programmatically?

You add the menu item using the Add method of the MenuItems collections of a MainMenu or MenuItem object.

■ What enhancements can you add to a menu?

Check marks, shortcut keys, access keys, and separator bars.

■ Does hiding a menu disable it?

No. You can still access the menu using a shortcut key.

Was this article helpful?

0 0

Post a comment