Click to Rate and Give Feedback

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:
Visual Basic Language Concepts
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.

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 containing 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 below and to the right of the first, allowing 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.

Try It!

To add a menu

  1. On the File menu, choose New Project.

  2. In the New Project dialog box, in the Templates pane, click Windows Application.

  3. In the Name box, type Menus and then click OK.

    A new Windows Forms project opens.

  4. 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 below 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 on the MenuStrip1 icon.

  5. In the form, click the MenuStrip control, type File, and then press Enter.

    New boxes for additional menu entries appear below 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 complete.

  6. In the box beneath the first box, type Exit, and then press Enter.

  7. Double-click the Exit menu to open the Code Editor.

  8. In the ExitToolStripMenuItem_Click event handler, type the following code.

    Visual Basic
    Application.Exit()
    
  9. Press F5 to run your program. Using the mouse, click the File menu, and then choose Exit. Your application closes.

In this lesson, you learned how to use the MenuStrip control to design menus. 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 proceed to the timers lesson.

Next Lesson: On Time: Using Timers to Perform Regular Actions

See Also

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker