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.

Discussion

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:

<code>
   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
</code>

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.



Comments

  • Menus in vb.net

    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

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

Top White Papers and Webcasts

  • A majority of organizations are operating under the assumption that their network has already been compromised, or will be, according to a survey conducted by the SANS Institute. With many high profile breaches in 2013 occurring on endpoints, interest in improving endpoint security is top-of-mind for many information security professionals. The full results of the inaugural SANS Endpoint Security Survey are summarized in this white paper to help information security professionals track trends in endpoint …

  • Packaged application development teams frequently operate with limited testing environments due to time and labor constraints. By virtualizing the entire application stack, packaged application development teams can deliver business results faster, at higher quality, and with lower risk.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds