Visual Basic also supports pop-up menus, those context-sensitive menus that
most commercial applications show when you right-click on an user interface object.
In Visual Basic, you can display a pop-up menu by calling the form's PopupMenu
method, typically from within the MouseDown event procedure of the object:
Private Sub List1_MouseDown(Button As Integer, Shift As Integer, _
X As Single, Y As Single)
If Button And vbRightButton Then
' User right-clicked the list box.
PopupMenu mnuListPopup
End If
End Sub
The argument you pass to the PopupMenu method is the name of a menu that you
have defined using the Menu Editor. This might be either a submenu that you can
reach using the regular menu structure or a submenu that's intended to work only
as a pop-up menu. In the latter case, you should create it as a top-level menu
in the Menu Editor and then set its Visible attribute to False. If your program
includes many pop-up menus, you might find it convenient to add one invisible
top-level entry and then add all the pop-up menus below it. (In this case, you
don't need to make each individual item invisible.) The complete syntax of the
PopupMenu method is quite complex:
PopupMenu Menu, [Flags], [X], [Y], [DefaultMenu]
By default, pop-up menus appear left aligned on the mouse cursor, and even
if you use a right-click to invoke the menu you can select a command only with
the left button. You can change these defaults using the Flags argument. The following
constants control the alignment: 0-vbPopupMenuLeftAlign (default), 4-vbPopupMenuCenterAlign,
and 8-vbPopupMenuRightAlign. The following constants determine which buttons are
active during menu operations: 0-vbPopupMenuLeftButton (default) and 2-vbPopupMenuRightButton.
For example, I always use the latter because I find it natural to select a command
with the right button since it's already pressed when the menu appears:
PopupMenu mnuListPopup, vbPopupMenuRightButton
The x and y arguments, if specified, make the menu appear in a particular position
on the form, rather than at mouse coordinates. The last optional argument is the
name of the menu that's the default item for the pop-up menu. This item will be
displayed in boldface. This argument has only a visual effect; If you want to
offer a default menu item, you must write code in the MouseDown event procedure
to trap double-clicks with the right button.
You can take advantage of the x and y arguments in a PopupMenu method to make
your program more Windows compliant, and show your pop-up menus over the control
that has the focus when the user presses the Application key (the key beside the
Windows key on the right side of a typical extended keyboard, such as the Microsoft
Natural Keyboard). But remember that Visual Basic doesn't define any key-code
constant for this key. Here's how you must proceed:
Private Sub List1_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 93 Then
' The system pop-up menu key has been pressed.
' Show a pop-up menu near the list box's center.
PopupMenu mnuListPopup, , List1.Left + _
List1.Width / 2, List1.Top + List1.Height / 2
End If
End Sub
Visual Basic's implementation of pop-up menus has a serious flaw. All Visual
Basic TextBox controls react to right-clicks by showing the standard Edit pop-up
menu (with the usual commands, such as Undo, Copy, Cut, and so on). The problem
is that if you invoke a PopupMenu method from within the TextBox control's MouseDown
event, your custom pop-up menu will be displayed only after the standard one,
which is obviously undesirable. You can solve it only by resorting to the unorthodox
and undocumented technique shown below.
Private Sub Text1_MouseDown(Button As Integer, _
Shift As Integer, X As Single, Y As Single)
If Button And vbRightButton Then
Text1.Enabled = False
PopupMenu mnuMyPopup
Text1.Enabled = True
End If
End Sub
More in Menus in Visual Basic 6