Chapter 5. Making Menus

In addition to buttons, labels, and textboxes, menus are one of the most common user interface elements in interactive programs.

This lesson explains how to add menus and context menus to forms, and catch their events so your program can take action when the user selects menu items.

CREATING MENUS

To create a menu, simply drop a MenuStrip control on a form. By default, the MenuStrip is docked to the top of the form so you don't really need to position it carefully. Just double-click the Toolbox's MenuStrip tool and you're set.

Unlike most controls, the MenuStrip appears in the Component Tray below the form in addition to on the form itself. Figure 5-1 shows the SimpleEdit program in the Form Designer. Below the form you can see the Component Tray containing a MenuStrip and a StatusStrip.

FIGURE 5-1

Figure 5.1. FIGURE 5-1

When you select a MenuStrip in the Form Designer, the menu bar at the top of the form displays a Type Here box. Click that box and type the menu's caption to create a main menu.

If you create a main menu entry and select it, the Form Designer displays a new Type Here box to let you create menu items below it, as shown in Figure 5-2.

The Form Designer also displays a new Type Here box to the right of the main menu item so you can create other main menus.

Continue entering text in the Type Here boxes to build the whole menu structure. Figure 5-3 shows the Format ...

Get Stephens' Visual Basic® Programming 24-Hour Trainer now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.