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.
Comments