The ToolStripMenuItem class represents a menu command, at any level. If a command leads to a submenu, it’s still represented by a ToolStripMenuItem object, which has its own collection of ToolStripMenuItem objects: the DropDownItems collection, which is made up of ToolStripMenuItem objects. The ToolStripMenuItem class provides the following properties, which you can set in the Properties window at design time or manipulate from within your code:
Checked – Some menu commands act as toggles, and they are usually selected (checked) to indicate that they are on, or are deselected (unchecked) to indicate that they are off. To initially display a check mark next to a menu command, set its Checked property to True. You can also access this property from within your code to change the checked status of a menu command at runtime. For example, to toggle the status of a menu command called FntBold, use this statement:
FntBold.Checked = Not FntBold.CheckedCode language: VB.NET (vbnet)
Enabled – Some menu commands aren’t always available. The Paste command, for example, has no meaning if the Clipboard is empty (or if it contains data that can’t be pasted in the current application). To indicate that a command can’t be used at the time, you set its Enabled property to False. The command then appears grayed out in the menu, and although it can be highlighted, it can’t be activated. The following statements enable and disable the Undo command depending on whether the TextBox1 control can undo the most recent operation:
If TextBox1.CanUndo Then cmdUndo.Enabled = True Else cmdUndo.Enabled = False End IfCode language: VB.NET (vbnet)
cmdUndo is the name of the Undo command in the application’s Edit menu. The CanUndo property of the TextBox control returns a True/False value that indicates whether the last action can be undone or not.
IsOnDropDown – If the menu command, represented by a ToolStripMenuItem object, belongs to a submenu, its IsOnDropDown property is True; otherwise, it’s False. The IsOnDropDown property is read-only and False for the items on the first level of the menu.
Visible – To remove a command temporarily from the menu, set the command’s Visible property to False. The Visible property isn’t used frequently in menu design. In general, you should prefer to disable a command to indicate that it can’t be used at the time (some other action is required to enable it). Making a command invisible frustrates users, who might spend time trying to locate the command in another menu.
Programming Menu Commands
When a menu item is selected by the user, it triggers a Click event. To program a menu item, insert the appropriate code in the item’s Click event handler. The Exit command’s code would be something like the following:
Sub menuExit(...) Handles menuExit.Click End End SubCode language: VB.NET (vbnet)
If you need to execute any cleanup code before the application ends, place it in the CleanUp() subroutine and call this subroutine from within the Exit item’s Click event handler:
Sub menuExit(...) Handles menuExit.Click CleanUp() End End SubCode language: VB.NET (vbnet)
The same subroutine must also be called from within the FormClosing event handler of the application’s main form because some users might terminate the application by clicking the form’s Close button.
An application’s Open menu command contains the code that prompts the user to select a file and then open it. You will see many examples of programming menu commands in the following chapters. All you really need to know now is that each menu item is a ToolStripMenuItem object, and it fires the Click event every time it’s selected with the mouse or the keyboard. In most cases, you can treat the Click event handler of a ToolStripMenuItem object just like the Click event handler of a Button.
Another interesting event of the ToolStripMenuItem is the DropDownOpened event, which is fired when the user opens a menu or submenu (in effect, when the user clicks a menu item that leads to a submenu). In this event’s handler, you can insert code to modify the submenu. The Edit menu of just about any application contains the ubiquitous Cut/Copy/Paste commands.
These commands are not meaningful at all times. If the Clipboard doesn’t contain text, the Paste command should be disabled. If no text is selected, the Copy and Cut commands should also be disabled. Here’s how you could change the status of the Paste command from within the DropDownOpened event handler of the Edit menu:
If My.Computer.Clipboard.ContainsText Then PasteToolStripMenuItem.Enabled = True Else PasteToolStripMenuItem.Enabled = True End IfCode language: VB.NET (vbnet)
Likewise, to change the status of the Cut and Copy commands, use the following statements in the DropDownOpened event of the ToolStripMenuItem that represents the Edit menu:
If txtEditor.SelectedText.Trim.Length > 0 Then CopyToolStripMenuItem.Enabled = True CutToolStripMenuItem.Enabled = True Else CopyToolStripMenuItem.Enabled = False CutToolStripMenuItem.Enabled = False End IfCode language: VB.NET (vbnet)
Using Access and Shortcut Keys
Menus provide a convenient way to display a large number of choices to the user. They allow you to organize commands in groups, according to their functions, and are available at all times. Opening menus and selecting commands with the mouse, however, can be an inconvenience. When using a word processor, for example, you don’t want to have to take your hands off the keyboard and reach for the mouse. To simplify menu access, Windows forms support access keys and shortcut keys.
Access keys allow the user to open a menu by pressing the Alt key and a letter key. To open the Edit menu in all Windows applications, for example, you can press Alt+E. E is the Edit menu’s access key. After the menu is open, the user can select a command with the arrow keys or by pressing another key, which is the command’s access key, without holding down the Alt key.
Access keys are designated by the designer of the application and are marked with an underline character. To assign an access key to a menu item, insert the ampersand symbol (&) in front of the character you want to use as an access key in the ToolStripMenuItem’s Text property.
Because the & symbol has a special meaning in menu design, you can’t use it in a menu item’s caption. To actually display the & symbol in a caption, prefix it with another & symbol. For example, the caption &Drag produces a command with the caption Drag (the first character is underlined because it’s the access key). The caption Drag && Drop will create another command whose caption will be Drag & Drop. Finally, the string &Drag && Drop will create another command with the caption Drag & Drop.
Default Access Keys Are Based on Item Captions
If you don’t designate access keys, Visual Basic will use the first character in each top-level menu as its access key. The user won’t see the underline character under the first character, but can open the menu by pressing the first character of its caption while holding down the Alt key. If two or more menu captions begin with the same letter, the first (leftmost and topmost) menu will open.
Shortcut keys are similar to access keys, but instead of opening a menu, they run a command when pressed. Assign shortcut keys to frequently used menu commands, so that users can reach them with a single keystroke. Shortcut keys are combinations of the Ctrl key and a function or character key. For example, the usual access key for the Close command (after the File menu is opened with Alt+F) is C, but the usual shortcut key for the Close command is Ctrl+W.
To assign a shortcut key to a menu command, drop down the ShortcutKeys list in the ToolStripMenuItem’s Properties window and select a keystroke. Specify a modifier (Shift, Ctrl, or Alt) and a key. You don’t have to insert any special characters in the command’s caption, nor do you have to enter the keystroke next to the caption. It will be displayed next to the command automatically. When assigning access and shortcut keys, take into consideration the well-established Windows standards.Users expect Alt+F to open the File menu, so don’t use Alt+F for the Format menu. Likewise, pressing Ctrl+C universally performs the Copy command; don’t use Ctrl+C as a shortcut for the Cut command.