Giving Users Choices: Creating Menus at Design Time
In this lesson, you will learn how to create menus and write code that executes when menu items are chosen. You will also learn how to add a set of standard menu items in one step.
Menus are an easy and familiar way for users to make choices regarding your program. Common uses for menus include exposing program options, adding shortcuts for common tasks such as cut and paste, or loading and saving files.
Visual Basic makes it easy to implement menus. You can use the MenuStrip control to create menus graphically. When dragged onto a form, the MenuStrip control appears as a box that contains the words "Type Here," located in the upper part of the form. You can click the box and type inside it to create the menu titles.
When the title for one menu item is set, additional menu items can be created underneath and to the right of the first. This allows you to extend the menu with as many additional items or sub-items as you want. When the look of your menu is complete, you can create event handlers to handle the Click events for each item.
To add a menu
On the File menu, click New Project.
In the New Project dialog box, in the Templates pane, click Windows Application.
In the Name box, type Menus and then click OK.
A new Windows Forms project opens.
From the Toolbox, drag a MenuStrip control onto the form.
Regardless of where you drop it, the MenuStrip control attaches itself to the uppermost part of the form.
You may also have noticed that there is a MenuStrip1 icon added in a gray area underneath the form—this area is called the component tray. If you click outside of the MenuStrip control, it will disappear; you can bring it back by clicking the MenuStrip1 icon.
In the form, click the MenuStrip control, type File, and then press Enter.
New boxes for additional menu entries appear underneath and to the right of the first menu item. These are spaces for additional menu items. You can continue to add menu items in either direction until your menu is finished.
In the box underneath the first box, type Exit, and then press Enter.
Double-click the Exit menu to open the Code Editor.
In the ExitToolStripMenuItem_Click event handler, type the following code.
Press F5 to run your program. Using the mouse, click the File menu, and then click Exit. Your application closes.
As a shortcut, you can add several standard menus and menu items in one step. The MenuStrip control has a MenuStrip Tasks pane that enables you to insert several standard menu items into the MenuStrip control.
To add a set of standard menu items to a menu strip
Select the MenuStrip control, click the smart task arrow at the upper-right corner of the control, and then click Insert Standard Items.
Several standard menus and menu items are added to the MenuStrip control.
Press F5 to run the program.
When the application starts, review the menu items on the new menus to become familiar with the standard items.
Close the application.
You can delete menu items that do not apply to your application. You will have to write code in the Click event handler for each menu item that you use.
In this lesson, you learned how to use the MenuStrip control to design menus. You also learned how to add a set of standard menus and menus items in one step. At this point, you can continue to the next lesson about timers, or you can explore more advanced ways to use menus in Closer Look: More About Menus, and then continue to the timers lesson.
Next Lesson: Using Timers to Perform Regular Actions