Popup Menu Example

This example shows you how to display a popup menu...

First, you need to add a number of controls:

Type Name Caption
Label lblPopup Right click for popup menu
Label lblDefaultPopup Right click for popup menu with default item
Label lblShowPopupAtX Right click here to display popup at X
Label lblX X
Menu mnuTest Test Menu
Menu mnuTestItem1 Test Item 1
Menu mnuTestItem2 Test Item 2

Make sure that mnuTestItem 1 and 2 are sub menus of mnuTest. Now, add the code below, run your project, and try right clicking on each of the labels.

Private Sub lblDefaultPopup_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Button = vbRightButton Then '// is right button?
        PopupMenu mnuTest, , , , mnuTestItem1 '// display popup menu
    End If
End Sub
Private Sub lblPopUp_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Button = vbRightButton Then '// is right button?
        PopupMenu mnuTest '// show popup
    End If
End Sub
Private Sub cmdHidePopup_Click()
    mnuTest.Visible = False '// hide the popup menu from the main menu bar
End Sub
Private Sub lblShowPopupAtX_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Button = vbRightButton Then
        '// get the coordinates of the X label (x and y)
        PopupMenu mnuTest, , lblX.Left, lblX.Top '// display menu
    End If
End Sub

You might also like...

Comments

James Crowley 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 audience ...

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.

“Any fool can write code that a computer can understand. Good programmers write code that humans can understand.” - Martin Fowler