SubMenu by Right-Clicking the Mouse

Posted on 2000-05-10
Last Modified: 2013-12-26
I am trying to make a submenu appear
when the mouse was right-clicked.

Just like Right-Clicking some objects
on the desktop, a submenu will appear.

Additional points will be offered for
excellent answer
Question by:Seles
LVL 43

Expert Comment

ID: 2795863

Here is a simple form file in ascii format that you can paste into a new file called frmMenu using Notepad.

Begin VB.Form Form1
   Caption         =   "Form1"
   ClientHeight    =   3195
   ClientLeft      =   60
   ClientTop       =   345
   ClientWidth     =   4680
   LinkTopic       =   "Form1"
   ScaleHeight     =   3195
   ScaleWidth      =   4680
   StartUpPosition =   3  'Windows Default
   Begin VB.Menu mnuPopup
      Caption         =   "Right-Click Menu"
      Visible         =   0   'False
      Begin VB.Menu mnuPopup1
         Caption         =   "Option &1"
      Begin VB.Menu mnuPopup2
         Caption         =   "Option &2"
      Begin VB.Menu mnuPopup3
         Caption         =   "Option &3"
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Private Sub mnuPopup1_Click()
    MsgBox "Option 1 Clicked"
End Sub

Private Sub mnuPopup2_Click()
    MsgBox "Option 2 Clicked"
End Sub

Private Sub mnuPopup3_Click()
    MsgBox "Option 3 Clicked"
End Sub

Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
   If Button = 2 Then
      PopupMenu mnuPopup
   End If
End Sub

Basically you create a menu using the menu editor in the VB IDE. Then using the form_mousedown event (or a control's mousedown event) test for right-click (button = 2) and use the popupmenu method to display the menu that you have created.

Hope this helps, get back if you need anything more.
LVL 28

Expert Comment

ID: 2796380
basically what you do is create a menu as you normally would using the menu editor...such as


except you make the File menu item invisible by unchecking its visible property box in the menu editor.  Now for any object you want the popup menu to show up on, you use that object's mousedown for a form:

Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Button = 2 Then _
        PopupMenu mnuFile
End Sub

where mnuFile is the name you gave the File menu option in your menu editor

LVL 28

Expert Comment

ID: 2797192
you dont have to make the menu invisible but generally when creatin a pop up menu its a menu you dont see across the top of your form...of course it is entirely up to you however
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.


Expert Comment

ID: 2797205
One thing to note is that if you do make the main menu item invisible you have to be careful that there is an actual visible submenu item when you Popupmenu <Whatever>.  

I manage the visibility of all the submenus based on what is being right clicked and sometimes no submenu's will actually be visible and you'll get an error if that happens.  

My solution was to just do all my checks.. and figure out if anything would be visible and if nothing would be I just exit sub at that point.
LVL 18

Expert Comment

ID: 2801447
One thing these other answers did not mention is how to pop up a "sub menu"

Let's say your menu looks like this:

Edit                         (mnuEdit)
     Insert |>            (mnuEditInsert)
                  Picture  (mnuEditInsertPicture)
                  Text      (mnuEditInsertText)

Edit is a Main menu (it shows up on the menu bar) and Insert is a Sub Menu.

If you say:

PopupMenu mnuEdit

Then you'll get a square menu box with one item on it:  Insert |>
and if the user clicks the arrow then they get the cascading menu.

However if you wanted to display the Insert Sub Menu then you could say:

PopupMenu mnuEditInsert

Then you'll get a square menu box with the items "Picture" and "Text" in it.

One more thing to note.  By default the menu appears under the mouse, which is generally what you want to do, but you can also specify the X and Y positions of the mouse after the name of the menu if you want the menu to appear someplace else on the screen:

PopupMenu mnuEdit, 100, 100

Accepted Solution

liuqian earned 100 total points
ID: 2803257
Just is a easy thing you can make it.

Use menu edit to do like:

and make the mnuTestMenu invisible.
Add code in Form_MouseUp
Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Button = 2 Then PopupMenu mnuTestMenu
End Sub

That's all.A context menu with submenu will appear when you right-click form.

LVL 28

Expert Comment

ID: 2803269
liugian...welcome to is customary here to simply post comments rather than answers...especially when there have already been comments posted that your answer does nothing more than reiterate.  the questioner can accept a comment as an answer at any time.

Expert Comment

ID: 2803377
liuqian changed the proposed answer to a comment

Author Comment

ID: 2836346
I am sorry that I accept the answer so late. My Problem is I was not familiar with "MenuEditor" and the command "PopupMenu". Now I did.

The Key of problem is
PopupMenu menuname,flags,x,y,boldname

Thanks for all comments.

I will share pts as below.
liuqian  100 (accepted as ANS)
TimCottee 100 (pls ans dummy Question)
AzraSound 100 (pls ans dummy question)


Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VB error "Type mismatch" 2 60
passing parameter in sql procedure 9 65
How to make an ADE file by code? 11 94
Child Form in front 4 49
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question