Menus

Popup menus

VB allows you to easily create a popup menu like those you see in Explorer when you right click. All you need to do is create a menu, containing all the items you want displayed on the popup menu. Then, set the menus visible property to false, so it is not displayed on the forms menu bar. Now add the following code to the MouseUp event of any control on which you want the popup menu to be displayed when it is right clicked.

If Button = vbRightButton Then
    ' Set the flags
    vFlags = vbPopupMenuRightButton
    ' Display the menu
    PopupMenu mnuEdit, vFlags
End If

The popupmenu function has the following parameters:

PopupMenu PopupMenu, Flags, X, Y, DefaultMenu

There are a few flags which you can set before displaying the menu. These are described below:

vbPopupMenuRightButton

Popup menu disappears when whether the left or right mouse button is clicked off the menu

vbPopupMenuLeftButton

Popup menu only disappears when the left mouse button is clicked off the menu

vbPopupMenuCenterAlign

Aligns the menu in the middle of where the mouse is clicked

vbPopupMenuLeftAlign

Aligns the menu to the left of where the mouse is clicked.

vbPopupMenuRightAlign

Aligns the menu to the right of where the mouse is clicked.

The X and Y parameters are optional, but you could use them to display the menu at a fixed point (ie next to a button that is clicked to display it).

You can also display a default item in bold, by passing the menu item you want in its DefaultMenu parameter.

You might also like...

Comments

About the author

James Crowley

James Crowley United Kingdom

James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audien...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Weeks of coding can save you hours of planning.”