# Visual Basic > Visual Basic FAQs >  [FAQ's: OD] How do I show a context menu popup on a UserForm?

## RobDog888

There isn't a menu editor in VBA like there is in VB6. So what we use is the CommandBars collection. Its really rather easy if you already know how to use the CommandBars collection. 

If you create a new CommandBar as designate it as a msoBarPopup type then you will get a .ShowPopup method for that commandbar object. Add some menu items to it and you have yourself a context menu.  :Smilie: 


Screen shot of UserForm after a right click...




VB Code:
'For a VBA UserForm ....
 'Behind UserForm1
Option Explicit
'Example code written by RobDog888 (vbforums.com)
'Macros must be enabled for the code to run
'Add a reference to MS Office xx.0 Object Library (If necessary depending on app)
'Add an Image control and CommandButton to your userForm.
Private moCBImage As Office.CommandBar
 Private Sub CommandButton1_Click()
    Unload Me
End Sub
 Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    moCBImage.Delete
    Unload Me
End Sub
 Private Sub Image1_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    'Invoke the menu on a right click on the Image control
    If Button = xlSecondaryButton Then
        moCBImage.ShowPopup
    End If
End Sub
 Private Sub UserForm_Initialize()
    Dim oCBSavePic As Office.CommandBarButton
    Dim oCBLoadPic As Office.CommandBarButton
    Dim oCBClearPic As Office.CommandBarButton
    'Add a titlebar caption
    Me.Caption = "RobDog888 Context Menu Demo"
    'Add the parent command bar popup
    Set moCBImage = Application.CommandBars.Add("cbImage", msoBarPopup, , True)
    With moCBImage
        .Name = "cbImage"
        .Enabled = True
    End With
    'Add the first child menu item to the popup
    Set oCBSavePic = moCBImage.Controls.Add(msoControlButton, 1, "8889", , True)
    With oCBSavePic
        .Caption = "Save Picture"
        .Enabled = True
        .FaceId = 3 'Save bitmap resource image id
        .OnAction = "SaveImage"
        .Style = msoButtonIconAndCaption
        .Visible = True
    End With
    'Add the second child menu item to the popup
    Set oCBLoadPic = moCBImage.Controls.Add(msoControlButton, 1, "8890", , True)
    With oCBLoadPic
        .Caption = "Load Picture"
        .Enabled = True
        .FaceId = 23 'Open folder bitmap resource image
        .OnAction = "LoadImage"
        .Style = msoButtonIconAndCaption
        .Visible = True
    End With
    'Add the third child menu item to the popup
    Set oCBClearPic = moCBImage.Controls.Add(msoControlButton, 1, "8891", , True)
    With oCBClearPic
        .BeginGroup = True
        .Caption = "Clear Picture"
        .Enabled = True
        .FaceId = 2087 'Delete bitmap resource image
        .OnAction = "ClearImage"
        .Style = msoButtonIconAndCaption
        .Visible = True
    End With
End Sub
  'Behind Module1
Option Explicit
'This is where the event procedures for the menu item clicks need to be.
'Example code written by RobDog888 (vbforums.com)
'Macros must be enabled for the code to run
 Public Sub SaveImage()
    MsgBox "Save Picture", vbOKOnly + vbInformation, "RobDog888's Context Menu Demo"
End Sub
 Public Sub LoadImage()
    MsgBox "Load Picture", vbOKOnly + vbInformation, "RobDog888's Context Menu Demo"
End Sub
 Public Sub ClearImage()
    MsgBox "Clear Picture", vbOKOnly + vbInformation, "RobDog888's Context Menu Demo"
End Sub
   'Behind ThisWorkbook (for Excel as an example)
Option Explicit
'Show UserForm at workbook opening.
'Example code written by RobDog888 (vbforums.com)
'Macros must be enabled for the code to run
 Private Sub Workbook_Open()
    UserForm1.Show vbModeless
End Sub

----------

