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.
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>
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.
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.