MenuStrip in Visual Basic .NET

Tags: VB.NET, VB 2008, VB 2010, VB 2012, VB 2013

This lesson describes the MenuStrip control


This control represents a form's menus, submenus, and menu items, and to make it working you have to set the Form's Menu property to the MenuStrip control.

When you first time add a MenuStrip control to the form, the form's Menu property is set automatically, so usually you do not worry much about it.

The MenuStrip control in design view is visible in the top of the form, and in the component tray.


component try

To add a new menu or edit existing one, just click the menu entry and type.

You can set a shortcut by placing an ampersand in front of the character you want to use as a keyboard accelerator e.g. E&xit. Now if type Alt+X at runtime, the program closes itself.

keyboard acceleration

You can automatically populate a MenuStrip control with standard menu items via MenuStrip Tasks. Click its smart-tag glyph and then when the MenuStrip Tasks dialog is displayed, select "Insert Standard Items".


Voila! Your MenuStrip control is populated with the standard items and you are ready to use them:

standard items

As you see we can add images or icons on the menu item's left side. To do that you use the Image property, browse for the appropriate image or do it in your code:

    ExitToolStripMenuItem.Image = My.Resources.exit

If you select a menu item, the item raises a Click event, which can take whatever action is appropriate. For example the following code demonstrates how do you exit the application if Exit menu item item is clicked:

Note: to create a menu item event handler, just double-click it in the design view.

    Private Sub MenuStripControl_Load(sender As Object, e As EventArgs) Handles Me.Load
        ExitToolStripMenuItem.ShortcutKeys = Keys.F4
    End Sub

    Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) _
            Handles ExitToolStripMenuItem.Click
    End Sub

Now you can close the application, either by selecting the Exit menu item, or pressing one of the keyboard shortcuts (Alt+X or F4)

Previous Lesson | Next Lesson

Share This

Home | About | Contact | Privacy Policy

Copyright 2018 - All Rights Reserved.