Results 1 to 1 of 1

Thread: [FAQ's: OD] How do I show a context menu popup on a UserForm?

Threaded View

  1. #1

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    [FAQ's: OD] How do I show a context menu popup on a UserForm?

    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.


    Screen shot of UserForm after a right click...




    VB Code:
    1. 'For a VBA UserForm ....
    2.  
    3. 'Behind UserForm1
    4. Option Explicit
    5. 'Example code written by RobDog888 (vbforums.com)
    6. 'Macros must be enabled for the code to run
    7. 'Add a reference to MS Office xx.0 Object Library (If necessary depending on app)
    8. 'Add an Image control and CommandButton to your userForm.
    9. Private moCBImage As Office.CommandBar
    10.  
    11. Private Sub CommandButton1_Click()
    12.     Unload Me
    13. End Sub
    14.  
    15. Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    16.     moCBImage.Delete
    17.     Unload Me
    18. End Sub
    19.  
    20. Private Sub Image1_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    21.     'Invoke the menu on a right click on the Image control
    22.     If Button = xlSecondaryButton Then
    23.         moCBImage.ShowPopup
    24.     End If
    25. End Sub
    26.  
    27. Private Sub UserForm_Initialize()
    28.     Dim oCBSavePic As Office.CommandBarButton
    29.     Dim oCBLoadPic As Office.CommandBarButton
    30.     Dim oCBClearPic As Office.CommandBarButton
    31.     'Add a titlebar caption
    32.     Me.Caption = "RobDog888 Context Menu Demo"
    33.     'Add the parent command bar popup
    34.     Set moCBImage = Application.CommandBars.Add("cbImage", msoBarPopup, , True)
    35.     With moCBImage
    36.         .Name = "cbImage"
    37.         .Enabled = True
    38.     End With
    39.     'Add the first child menu item to the popup
    40.     Set oCBSavePic = moCBImage.Controls.Add(msoControlButton, 1, "8889", , True)
    41.     With oCBSavePic
    42.         .Caption = "Save Picture"
    43.         .Enabled = True
    44.         .FaceId = 3 'Save bitmap resource image id
    45.         .OnAction = "SaveImage"
    46.         .Style = msoButtonIconAndCaption
    47.         .Visible = True
    48.     End With
    49.     'Add the second child menu item to the popup
    50.     Set oCBLoadPic = moCBImage.Controls.Add(msoControlButton, 1, "8890", , True)
    51.     With oCBLoadPic
    52.         .Caption = "Load Picture"
    53.         .Enabled = True
    54.         .FaceId = 23 'Open folder bitmap resource image
    55.         .OnAction = "LoadImage"
    56.         .Style = msoButtonIconAndCaption
    57.         .Visible = True
    58.     End With
    59.     'Add the third child menu item to the popup
    60.     Set oCBClearPic = moCBImage.Controls.Add(msoControlButton, 1, "8891", , True)
    61.     With oCBClearPic
    62.         .BeginGroup = True
    63.         .Caption = "Clear Picture"
    64.         .Enabled = True
    65.         .FaceId = 2087 'Delete bitmap resource image
    66.         .OnAction = "ClearImage"
    67.         .Style = msoButtonIconAndCaption
    68.         .Visible = True
    69.     End With
    70. End Sub
    71.  
    72.  
    73. 'Behind Module1
    74. Option Explicit
    75. 'This is where the event procedures for the menu item clicks need to be.
    76. 'Example code written by RobDog888 (vbforums.com)
    77. 'Macros must be enabled for the code to run
    78.  
    79. Public Sub SaveImage()
    80.     MsgBox "Save Picture", vbOKOnly + vbInformation, "RobDog888's Context Menu Demo"
    81. End Sub
    82.  
    83. Public Sub LoadImage()
    84.     MsgBox "Load Picture", vbOKOnly + vbInformation, "RobDog888's Context Menu Demo"
    85. End Sub
    86.  
    87. Public Sub ClearImage()
    88.     MsgBox "Clear Picture", vbOKOnly + vbInformation, "RobDog888's Context Menu Demo"
    89. End Sub
    90.  
    91.  
    92.  
    93. 'Behind ThisWorkbook (for Excel as an example)
    94. Option Explicit
    95. 'Show UserForm at workbook opening.
    96. 'Example code written by RobDog888 (vbforums.com)
    97. 'Macros must be enabled for the code to run
    98.  
    99. Private Sub Workbook_Open()
    100.     UserForm1.Show vbModeless
    101. End Sub
    Attached Files Attached Files
    Last edited by RobDog888; Jun 9th, 2007 at 02:38 AM. Reason: Updated attachment with code fix.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width