Programming Menu Commands VB Help

Menu commands are similar to controls. They have certain properties that you can manipulate from within your code, and they recognize a single event, the Click event. If you select a menu command at design: tiine, Visual Basic opens the code for the Click event in the Code window. The name of the event handler for the Click event is composed of the command’s name followed by an underscore character and the event’s name, as with all other ¢ontrols. You can also manipulate the menu command’s properties from within your code. These properties are the ones you can set at design time, through the Menu Editor window. Menu commands don’t have methods you can call. Most menu object properties are toggles. To change the Checked property of the FontBold command, for instance, use the following statement:

FontBold.Check~d – Not FontBold.Checked

If the command is checked, the checkmark will be removed. If the command is unchecked, the checkmark will be inserted in front of its name. You can also change the command’s caption a~runtime, although this practice isn’t common. The-Caption property is manipulated only when you create dynamic menus.by adding and removing commands at runtime, as you will see in the section “Adding Commands at Runtime.”

Using A~cess and Shortcut Keys

Menus are a convenient way of displaying a large number of choices to the user. They allow you to organize commands in groups, according to their function, and they 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, Visual Basic supports access keys and shortcut keys.

Access 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. Once the menu is open, the user can select a command with the arrow keys or by pressing another key, ,which is the command’s shortcut key. For example, with the Edit menu open, you can press P to invoke the Paste command or C to copy the selected text.

Access keys are designated by the designer of the application, and they are marked with an underline character. The underline under the character E in the Edit menu denotes that E is the menu’s access key and that the keystroke Alt+E opens the Edit command. To assign an access key to a menu command; insert the ampersand symbol (&) in front of the character you want to use as an access key in the menu’s caption

Because the & symbol has a special meaning in menu design, you can’t use it as Is. 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 stri ~g “&Drag && Drop” will create another command with the caption Drag & Drop.

Shortcut Keys

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 Control key and a function or character key. To assign’a shortcut key to a menu command, drop down the Shortcut list in the Menu Editor and select a keystroke. You don’t have to insert any special char- -acters 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, Figure 4.8 shows some of the possible shortcut keys you can assign to your menus:

Capture

Manipulating Menus at Runtime·

Dynamic menus change at runtime and display more or fewer command depending on the current status of the program. This section explores tw. niques for implementing dynamic menus:
• Creating short and long versions of the same menu
• Adding and removing menu commands at runtime

Creating Short and Long Menus

A common technique in menu design is to create long and short versioru menu. If a menu contains many commands, and most of the time only a them are needed, you can create one menu with all the-commands and a with the most common ones. The first menu is the long one, and the seen.

short one. The last command in the long menu should be Short Menu, and when selected, it should display the short menu. The last command in the short menu should be Long Menu, and when selected, it should display the long menu. Figure 4.9 shows a long and a short version of the same menu. The short version omits the infrequently used formatting commands and is easier to handle.

Capture

To implement the Long Menu command, start a new project and create a menu that has the structure

Capture

Following is the code that shows/hides the long menu in the MenuSize command’s Click event

The MenuSize Menu Item’s Click Event

Capture

This subroutine doesn’t do much. It simply toggles the Visible property of certain menu commands and changes the command’s caption to Short Menu or Long Men.u., depending on the menu’s current status

Adding and Removing Commands at Run.time

We’ll conclude our discussion of menu design with a technique for building dynamic menus, whichgrow and shrink at runtime. Many applications maintain a list of the most recently opened files in their File menu. When you first start the application, this list is empty, and as you open and close files, it starts to grow. Tocreate a dynamic menu, you first create a control array of menu commands. In the Menu Editor window, add a me~u option and set its Index property to O. Youcan then add commands with the same name and consecutive Index values. At design time, you don’t have to add more than one option. One command with its Index property set to 0 is adequate to create the menu control array. You can use this array’s name and an Index value to add new optionsat runtime. Figure 4.10 shows the RTMenu application, which demonstrates how to add to and remove items from a menu at runtime

Capture

Initially, the form’s menu contains the following items:

File
Open
Save Exit
Edit
Copy
Cut
Paste
Run Tim eMenu

Commands grouped in submenus are indented from the left. The last command’s name is RunTlIDeOptions and its index is O.Once a command option is specified as a control array, you can easily add commands to the RunTimeOptions array with the Load method. After alii menu commands are very similar to controls. The two buttons at the bOttom of the Form add commands to and remove commands from the Run Tune Menu. Each new command is appended at the end of the menu, and the commands are removed from the bottom of the menu (the most recently added commands). To change this order, and display the most recent command at the beginning of the menu, use a large initial index value (like 99) and increase it with every new command you add to the menu, as shown in Code 4.9.

The RTMenu application

Capture

New menu commands are appended to the existing menu with the Load method. The.argument of the Load method is the name of a menu command with a new Index value. The variable RTMenu keeps track of the number of commands under the Run Time Menu object. Each time a new command is added to this menu, RTMenu increases by an increment of 1. The Remove Run Time Option button uses the Unload method to remove commands. from the same menu. The argument to the Unload method -isthe index of the’ command to be removed. The s.ample application above removes commands from the bottom of the menu, but you could remove any co~and by specifying its order in the menu.

Creating Pop-Up Menus  ,

Nearly every Windows application provides a context menu (or shortcut menu, as it’ssometimes called) that the user can invoke by right-clicking a Form or a control. This pop-up menu (or floating menu) is a regular menu, but it’s not anchored ‘ on the Form. It can be displayed anywhere on the Form. Pop-up menus are invoked with the Popup Menu method. First, you create a menu as ,usual. Suppose you have designed the basic File and Edit menus for an application, and they are displayed on:the Form as usual. To make the application a bit easier to use, you can also display the Edit menu as a pop-up menu. If the Edit menu’s name is EditMenu, you can insert the following line in a control’s MouseUp event:

(We use the MouseUp event because, unlike the Click event, it reports which button was pressed.)

If the right mouse button is pressed, the code calls.the Form’s PopupMenu method to display’Ihe Edit menu. The PopupMenu method is usually called’ from within TextBox and Picture Box controls, because these controls can carry out editing operations.

The PopupMenu method has the following syntax:

popupMenu menuname., f1 ags, X” y, bo 1dcommand

Only the first argument is required, and it’s the menu’s name, as shown in the  Menu Editor window. The other arguments are optional. The x and y arguments are the coordinates of a point on the Form (or control) where the menu will be displayed. If you don’t specify the x and y arguments, the pop-up menu will appear at the pointer’s location. The flags argument defines the location and behavior of a pop-up menu and-can have one of the values The last argument, BoldCom1J:Ulnd, specifies the name of a menu command that should appear in bold. Only one command in the menu can be bold, and bold is commonly used to denote the default (or suggested) option.

Capture

To specify both a behavior and location constant, combine them with the Or operator. The following code displays a ‘pop-up menu with its top border centere on the Form, which triggers Click events for menu items when either button’ pressed.

Capture

Posted on November 4, 2015 in Working with Forms

Share the Story

Back to Top