Lesson 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. It also explains how to 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 appearing 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.

Image described by surrounding text.

Figure 5.1

When you select a MenuStrip in the Form Designer, either on the form's surface or in the Component Tray, 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 then click it to select it, the Form Designer displays a new Type Here box to let you create menu items. Figure 5.2 shows the top of the Form Designer after I created the top-level File menu.

Figure 5.2

You can continue clicking menu items to add submenus as ...

Get C# 24-Hour Trainer, 2nd Edition 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.