Working with Menus in VB.NET

Menus are often necessary in applications and it is essential that developers have an ability to manipulate them in design-time. In this article, you will take a look at creating menu items and see an example of the code to work with menus in VB.NET.


The MainMenu component was not present in VB6; it was added in VB.NET. But, as in previous versions of VB, you can add and modify menu items either in design-time or in run-time by using methods and properties of the Menu component.

Working with Menus in Design-Time

To add menus to your VB.NET application in design-time, you need to add a MainMenu component to your form. The MainMenu control allows you to create, add, and modify menus and menu bars and set their properties in the Properties window. To add a MainMenu component, open the Forms Toolbox and add the MainMenu component to your form. Once you have added the control to your form, you quickly can add menus to your VB.NET windows forms.

The MenuItem class provides properties that allow you to configure the appearance and the functionality of menu items. For example, if you need to display a check mark next to a menu item, you can use the Checked property. This usually is done to identify a menu item that is selected in a list of multiple, mutually exclusive menu items. Additionally, you can use a shortcut property to set keyboard shortcuts.

The MainMenu control represents a container for the whole menu setup structure on the form. A menu consists of MenuItem objects that represent individual menu commands. Each Menuitem can be either a parent for the sub menu items or a command. To bind MainMenu, you need to assign the MainMenu to the Menu property of the Form. After that, you can add additional MenuItems to the MainMenu by using the Add method.

Working with Menus in Run-Time

VB.NET allows run-time menu manipulation. This may be required whenever certain menu items are to be displayed after the user takes a certain action. Additionally, you may want to display menu items depending on the user's permission in the application and an ability to hide or disable certain menu items would come in handy.

The Example

You will add the MainMenu component to your form. The form would look like this:

You will also add the following code to your form:

   Private Sub Form1_Load(ByVal sender As System.Object, _
   ByVal e As System.EventArgs) Handles MyBase.Load

      MainMenu1.MenuItems.Add("Item 1", _
         New EventHandler(AddressOf ClickHandler))
      MainMenu1.MenuItems.Add("Item 2", _
         New EventHandler(AddressOf ClickHandler))
      MainMenu1.MenuItems.Add("Item 3", _
         New EventHandler(AddressOf ClickHandler))

   End Sub

   Public Sub ClickHandler(ByVal sender As Object, _
                           ByVal e As System.EventArgs)

      MessageBox.Show("Menu '" & sender.Text.ToString() & "' clicked")

   End Sub

The Result

Running the example results in a form that looks like this:

Clicking Item 2 would result in a message box showing up:

How It Works

In your example, you added three menu items to the MainMenu1 component on your form. To add the menu items, you specify their names and the click event handler. The code for the click event checks the text of the sender to determine which menu item has been clicked and shows a MessageBox with the name of that menu item.

Author Bio

Irina Medvinskaya has been involved in technology since 1996. Throughout her career, she as developed many client/server and web applications, mainly for financial services companies. She works as a Development Manager at Citigroup.


  • Menus in

    Posted by mafunga nyasha on 04/23/2014 06:30am

    i did do understand the above explanation but my only problem is i would like a source code for example on how to open a file,copy ,paste or exit the application during runtime of my code using the menus i have created... thanx in advance

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Lenovo recommends Windows 8 Pro. "I dropped my laptop getting out of the taxi." This probably sounds familiar to most IT professionals. If your employees are traveling, you know their devices are in for a rough go. Whether it's a trip to the conference room or a convention out of town, any time equipment leaves a user's desk it is at risk of being put into harm's way. Stay connected at all times, whether at the office or on the go, with agile, durable, and flexible devices like the Lenovo® …

  • As the mobile enterprise marketplace expands and customer needs grow more diverse, Samsung recognizes that solution partners and developers play an essential role by continually innovating to meet their customers' needs. Samsung works to provide these developers and partners with the latest tools and resources needed to create these solutions. Read this program guide to learn how the Samsung Enterprise Alliance Program provides partners and developers with Samsung enterprise software development kits (SDKs) …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date