Home > Articles > Programming > Windows Programming

  • Print
  • + Share This
This chapter is from the book

Adding Menus

Now that you've created a toolbar for your application, you might need to add a menu to enable users to easily select many of the common functions. In most applications, you usually have file functions that allow users to create, edit, and save files. You also have edit functions that allow users to move data around and those for specific tasks in your application. For example, an application I've written has functions for handling pilot information, aircraft data, and log entry functions—in other words, these functions provide lots of things that the users can do.

One of the most important things in any application is allowing users to easily access all its functions. Users are accustomed to accessing most functions with a single mouse click. Also, most users want all the functions located conveniently in one place; to handle this in your application, use toolbars and menus. Visual Basic allows you to quickly and easily create menus with the new MainMenu control, which you create using the actual menu bars located at the top of a form. Pop-up menus that users typically access by right-clicking are created by using the new ContextMenu Control.

NOTE

If you've been using Visual Basic for any length of time, you might be asking, "Where is the Menu Editor?" Well, it's gone! The two new controls, MainMenu and ContextMenu replace the editor, making it easier to work with the menus directly on a form.

Creating an Application Menu

When creating any type of menu system for an application, you must determine what functions you want or need to put on the menu and how you want to organize these functions. By looking at Visual Basic's main menu, you can see that certain functions are organized into groups of similar items (see Figure 3.9).

Figure 3.9 Organizing menu items into functional groups.

It is important to group similar menu items according to the application you're creating. In fact, to be consistent with other Windows applications, you should use groups that your users are already familiar with. This way, they have an idea of where to find a particular menu item, even if they've never used this application before. You might find the following standard menu groups in a Windows application:

  • File—This menu contains any functions related to the opening and closing of files used by your application. Some standard items included in this menu are New, Open, Close, Save, Save As, Print, and Page Setup. The File menu also is the location of the most recently used file list that many applications have. Finally, a File menu generally appears where the Exit command is located.

  • Edit—The functions on this menu pertain to editing text and documents. Some typical Edit items are Undo, Cut, Copy, Paste, and Clear.

  • View—This menu can be included if your program supports different views for the same document. For example, a word processor might include a normal view for editing text and a page layout view for positioning document elements.

  • Tools—This menu is a catchall for any optional programs that might be available from within the application. For example, a spelling checker might be included for a word processor.

  • Window—If your application supports working with multiple documents at the same time, you should have this menu included in your application. The Window menu is set up to let users arrange the multiple documents or switch rapidly between them.

  • Help—The Help menu allows users to access your application's help system. It usually includes menu items for a table of contents, an index, a search feature, and an About box.

Use these six menu types as a starting point when creating the menu system for your application. You can include any of them as you need them, but don't think you need to add all six. Also, if you need other menu groups for your application, you can add whatever groups you might need.

NOTE

When adding other menu groups, be careful not to confuse your users. Whenever possible, place as many of your menu functions in one of the standard menu groups.

Building the Menu Groups

After you decide what functions you want to include in the menu and how to group them, you can start building the menu. When creating a menu for your application, remember that every form in the application can have its own menu defined. To see how to create a menu, start with the project you created previously for the toolbar example. (If you didn't save it, don't panic—just start a new project.)

When creating a menu, you must display the form that you want to add the menu to. Double-click the MainMenu control in the Toolbox to add it to the form. You will see the first menu item on the form ready for you to enter the text (see Figure 3.10).

Figure 3.10 Adding menu items to the Main Menu has never been easier. Simply type where it says, "Type Here."

When the MainMenu control is on the form, you can start adding the items that you want in the menu. For each item you want on a menu, you will enter the Text property directly on the menu control at the top of the form. However, the Name property must be changed separately. The Text property is what users see on the menu when using your application; the Name property is what you use in your application code to access the menu item. After you enter a menu item, use the arrow keys to move the cursor and accept the value for the item, moving you to the next menu item (see Figure 3.11).

Figure 3.11 The entered menu items on the form.

NOTE

Remember to change the Name property for each menu item. Otherwise, all menu items will look almost the same in the code, except the number added to the control name.

After you finish entering the menu items for the application, click away from the menu. Your menu will appear on the form exactly as you've entered it.

Adding Menu Levels

If you created a menu as shown in Figure 3.12, it probably will be very clumsy to work with. Every item that you've entered appears on the main menu bar.

Figure 3.12 A menu with all the items displayed on the main menu bar.

If your application has only one or two menu options, this might be acceptable; however, if you have many different menu items, the menu bar will run out of space. What you really need to do is set up a menu that uses multiple levels to display multiple menu items. When you click the menu item on the menu bar, the first level of the menu drops down (see Figure 3.13). This level is called a submenu.

Figure 3.13 Multiple levels of a menu item allow for many options in a small amount of menu space.

Adding subitems to the menu is very easy to do: First select the menu item you want to add the subitem to. This will display a new item entry to the right of the select item (see Figure 3.13). Now click the new subitem displayed and enter the required text. If you want to insert a new item in the menu list, right-click the item below the point where you want to insert the new item and select Insert Item from the pop-up menu.

One other option in a menu is a separator bar, which is a line that allows you to group different menu items together with a single menu item's sublevel list (see Figure 3.14). These bars break up a long list of menu items without creating submenus.

Adding a separator bar used to be a bit tricky. However, in Visual Basic .NET, the process has become extremely simple. To add a separator, select the item below the position where you want to add the separator and right-click; then select Insert Separator from the pop-up menu (as in Figure 3.14).

Figure 3.14 With separator bars, you can group and organize menu items at the same level.

NOTE

Although separator bars are great to use, don't try to add one to the top level of the menu. You can use them only in submenus.

Enhancing the Menu

If you've been working with Windows applications for any length of time, you've probably noticed that you can access a menu item in several different ways. Usually you can do this using a combination of keystrokes. You can include two different types of access in your menu: hotkeys and shortcuts.

Adding Hotkeys

Hotkeys are something you already know about and use, probably without thinking about it. A hotkey is identified by an underscore beneath the letter in the item's text (for example, the E in Edit). To create a hotkey, place an ampersand (&) in the Text property immediately before the letter you want as the hotkey. For the File menu item, the value of the Text property would be &File. Hotkeys can be used for any item in your menu, including the top-level menu items.

NOTE

At any given level of a menu, only one unique value can be used as a hotkey. For example, the Visual Basic menu has File and Format at the same top level. If you look closely at them, you'll see that the File menu item has the F as the hotkey, but the Format menu has the o as the hotkey. If you used the F for both menu items, Windows wouldn't know which one you really wanted. However, the same letter can be used in items that appear in different groups, such as the File menu's Print option and the View menu's Page Layout option. For each group or level, you can have at most 36 hotkeys—one for each letter and one for each number.

After you include hotkeys in your menu, you can open a top-level menu simply by holding down the Alt key and pressing the hotkey of choice. When the menu appears, you can press the hotkey for the menu item you want to execute. For example, if you want to start a new project in Visual Basic, you could press Alt+F and then N for the File menu's New Project option.

TIP

If there's no conflict with letter selection, you should use the first letter of a menu item as the hotkey. This is what users expect; it also makes it easy to guess what the hotkey is for a particular function.

Adding Shortcuts

The other way to provide menu access is with shortcuts. Shortcut keys provide direct access to any function in a menu, no matter what level it's actually on. You can perform shortcuts with a key combination (such as Ctrl+C for a copy function) or with a single key (such as Delete for the delete function). If you use the default shortcut keys (listed in Table 3.3), users will already know how to perform certain common tasks.

Table 3.3  Standard Shortcut Keys Used by Windows Applications

Menu Item

Shortcut Key

Description

Edit Menu

Cut

Ctrl+X

Removes text or a control and copies it to the Clipboard

Copy

Ctrl+C

Copies the text or control to the Clipboard

Paste

Ctrl+V

Pastes the contents of the Clipboard to the selected area

Undo

Ctrl+Z

Undoes the last change

Find

Ctrl+F

Finds a string

File Menu

Open

Ctrl+O

Opens a file

Save

Ctrl+S

Saves the current file

Print

Ctrl+P

Prints the current data

Assigning shortcut keys is simply a process of selecting the key or keys that you want to use from the Shortcut drop-down list. Shortcut keys are displayed to the right of the menu item both in the selection list (see Figure 3.15) in the Menu Editor and in the actual menu in your application.

Figure 3.15 Shortcuts are displayed in the menu item selection list.

Because of the way shortcut keys work, only one shortcut key can use a given key combination. There also is a new property for each menu item that allows you to choose whether a shortcut key is displayed to the user at any point in the application.

TIP

Just like with hotkeys, the shortcut key should correspond with the first letter of the menu item whenever possible.

Adding the Menu Code

Adding code for the menu is the same as adding code for any other control in your application. There is a Click event for each menu item added to the form. For this reason, giving each menu item a unique name is very important. The Click event is triggered whenever you select the menu item by clicking it, by using the hotkey or the shortcut key. To display the Click event routine for a menu item, simply double-click the menu item you want to work with; its related Click event will appear in the Code Editor window.

NOTE

If you look at the code that you added to the dlgFileOpenCopy project on Day 2, notice that you had code in the mnuFileOpen_Click routine.

Optional Menu Settings

In addition to the two required properties, each menu item has several other optional properties that you can use to control the menu item:

  • Checked determines whether a check mark is displayed next to a menu item when it's selected. This is used to indicate that a particular option has been selected.

  • RadioCheck determines whether a dot is displayed instead of a check mark.

  • If Enabled is false (not selected), users can see the menu item but can't access it.

  • Visible allows you to hide menu items that aren't needed for a particular function or form.

  • MergeType determines whether the top-level menu items are displayed while another form is active and contains its own menu.

  • MergeOrder determines the actual position of the menu item when the MergeType is MergeItems.

  • MDIList is available only when with MDI applications. It displays a list of the current child windows displayed.

When you want to indicate the status of a particular function, use the Checked property to show a check mark in the menu drop-down list for the item selected. Visual Basic uses this property to show which toolboxes are displayed (see Figure 3.16). This property actually toggles back and forth between True and False within the application code itself.

Figure 3.16 Using the Checked property to display a function's status.

To use the Checked property, add the following code to the Click event of any menu item that with which you want to use it:

If <menu item>.Checked then
    <menu item>.Checked = True
Else
    <menu item>.Checked = False
End If

NOTE

If you try to use the Checked property for a top-level menu item, you'll get an error message. You can't toggle top-level menu items on and off.

The MDIList property specifies that a list of open MDI child forms will be displayed. When this property is set to True, the menu automatically adds items as child forms are opened and removes any closed child forms.

Creating and Using Context Menus

Menus generally are thought of as being visible and occupying the bar at the top of an application form. Visual Basic allows you to create another type of menu, which appears only when needed and directly next to the area you're working with. These context menus often are used to handle functions related to a specific area of the form. For an example of this type of menu, right-click anywhere in Visual Basic's Code Editor window for a code-related menu (see Figure 3.17). Context menus usually appear at the mouse pointer's current location. After you select an option from the menu, it disappears from the screen.

Figure 3.17 Context menus offer users many options directly related to the work they're performing.

Setting Up a Context Menu

To create a context menu, add the ContextMenu control to the form; then add all the required menu items using the same techniques as the MainMenu control.

NOTE

The context menu is new to Visual Basic .NET. It has been separated from the Main menu so it's handled independently in the code.

CAUTION

If you want a menu item to appear on the main menu and on the context menu, you must add the same item twice: once on the main menu and once on the context menu.

Displaying a Context Menu

To see how the context menu works, add one to your form and insert at least one menu item. The final step is changing the ContextMenu property for the control with which you want to associate the context menu. Once this is done, you can execute the application and right-click the control to display the context menu, as shown in Figure 3.18.

Figure 3.18 Using context menus in your application.

Merging Menus

What does merging menus really do for you? When you're creating an MDI application, you must take care to create your menus properly. If any of the child forms have menus of their own, you must decide which menu bar will be displayed by your application. Whenever a child form has a menu associated with it, its menu will replace the parent form menu when the child form is displayed.

You can deal with menus in an MDI application in one of the following MergeTypes:

  • Add  The menu item will be added to the existing menu displayed.

  • Replace  The menu item replaces an existing menu item at the same position in a merged menu.

  • MergeItems  All submenu items are merged with the existing menu items at the same position in a merged menu.

  • Remove  The menu item isn't included in the merged menu.

The MergeOrder property specifies the position a menu item would be placed in when merged with an existing menu on a parent form.

  • + Share This
  • 🔖 Save To Your Account