Home and Learn: VB Net Course

Right-Click Context Menu in VB Net

You can add a right-click menu to forms and the controls you place on your form. The process is quite easy.

Right-click menus are called Context Menus, in VB Net. In the Toolbox on the left of Visual Studio, expand the Menus & Toolbars section. Double-click the ContextMenuStrip item to add one to your form:

The ContextMenuStrip control in the Visual Studio Toolbox

You should see two things happen: one, a new item will be added to at the bottom of your screen, just below your form; two, you should see this appear in the top left of your form:

Context Strip Menu Item in VB Net

This is the context menu that will appear when you right-click. You fill in the items just like you did for the menu items, by typing in the boxes.

Type something into the textbox, where it says, Type Here. Anything will do, Item One, for example. Press the enter key on your keyboard and a second text box will appear, ready for you to add another item on your right-click menu.

Add two more items and your right-click menu will look like this:

Context Strip Menu Item in VB Net with three options.

Don't worry that it says, ContextMenuStrip at the top. That won't appear when your menu is displayed.

But have a look at the text box, where it says, Type Here. Notice that there is a small arrow to the right of the text area. Click it to reveal a dropdown list:

Context Menu options.

This is telling you that, besides adding menu items to your right-click menus, you can add a combo box, a separator and a Textbox. We'll just stick with the menu items, though.

Now that you have created a Context Menu, you need to add it as a property of another control. We'll attach ours to the form itself.

Click on your form to select it. Make sure that it is the form that you have selected and not a control on the form. In the Properties area on the right, locate the ContextMenuStrip item:

Properties of a form in VB Net

Click the dropdown arrow to see all the Context Menus available. We only have one, but you can add as many Context Menus as you like to a project:

Adding a Context Strip to a form in VB Net

Select the ContextMenuStrip1 item to bind it to the form.

Now run your form. Right click anywhere on the form itself. You should see your menu appear:

Context menu activated on a form in VB Net.

Nothing will happen when you click an item because we haven't written any code for each item. But the menu disappears when you do select an item.

Close your form down and return to Visual Studio.

The Context menu will likely have disappeared from your form. To bring it back, select ContextMenuStrip1 at the bottom of the Visual Studio screen.

You can access the properties of an induvial menu item by clicking in the margins next to the menu item:

Creating a VB Net code stub for a context menu item.

Double-clicking the margins will create a code stub for a right-click menu. This is the same as you did for an ordinary menu. In the code below, we've renamed the first Context Menu item. The code is just a message box:

Private Sub ItemOne_Click(sender As Object, e As EventArgs) Handles ItemOne.Click

MessageBox.Show("Do something here")

End Sub


Create your own code stubs for your menu items. Display a message when you select each item.


Now that you know how to add Menus, Sub Menus and Shortcuts it's time for you to complete your own menu bar. Click the project below to see what you need to do. It's not very difficult! In the section after the project, you'll see how to add code for your new menu bar.

Back to the VB NET Contents Page


Buy the Book of this Course

Email us: enquiry at homeandlearn.co.uk