Visual Basic Concepts

Displaying Pop-up Menus

See Also

A pop-up menu is a floating menu that is displayed over a form, independent of the menu bar. The items displayed on the pop-up menu depend on where the pointer was located when the right mouse button was pressed; therefore, pop-up menus are also called context menus. In Microsoft Windows 95 or later systems, you activate context menus by clicking the right mouse button.

Any menu that has at least one menu item can be displayed at run time as a pop-up menu. To display a pop-up menu, use the PopupMenu method. This method uses the following syntax:

[object.]PopupMenu menuname [, flags [,x [, y [, boldcommand ]]]]

For example, the following code displays a menu named mnuFile when the user clicks a form with the right mouse button. You can use the MouseUp or MouseDown event to detect when the user clicks the right mouse button, although the standard is to use the MouseUp event:

Private Sub Form_MouseUp (Button As Integer, Shift As _
   Integer, X As Single, Y As Single)
   If Button = 2 Then   ' Check if right mouse button 
                        ' was clicked.
      PopupMenu mnuFile   ' Display the File menu as a 
                        ' pop-up menu.
   End If
End Sub

Any code following a call to the PopupMenu method is not run until the user selects an item in the menu or cancels the menu.

Note   Only one pop-up menu can be displayed at a time. While a pop-up menu is displayed, calls to the PopupMenu method are ignored. Calls to the PopupMenu method are also ignored whenever a menu control is active.

Often you want a pop-up menu to access options that are not usually available on the menu bar. To create a menu that will not display on the menu bar, make the top-level menu item invisible at design time (make sure the Visible check box in the Menu Editor is not checked). When Visual Basic displays a pop-up menu, the Visible property of the specified top-level menu is ignored.

The Flags Argument

You use the flags argument in the PopupMenu method to further define the location and behavior of a pop-up menu. The following table lists the flags available to describe a pop-up menu's location.

Location constants Description
vbPopupMenuLeftAlign Default. The specified x location defines the left edge of the pop-up menu.
vbPopupMenuCenterAlign The pop-up menu is centered around the specified x location.
vbPopupMenuRightAlign The specified x location defines the right edge of the pop-up menu.

The following table lists the flags available to describe a pop-up menu's behavior.

Behavior constants Description
vbPopupMenuLeftButton Default. The pop-up menu is displayed when the user clicks a menu item with the left mouse button only.
vbPopupMenuRightButton The pop-up menu is displayed when the user clicks a menu item with either the right or left mouse button.

To specify a flag, you combine one constant from each group using the Or operator. The following code displays a pop-up menu with its top border centered on a form when the user clicks a command button. The pop-up menu triggers Click events for menu items that are clicked with either the right or left mouse button.

Private Sub Command1_Click ()
   ' Dimension X and Y variables.
   Dim xloc, yloc

   ' Set X and Y variables to center of form.
   xloc = ScaleWidth / 2
   yloc = ScaleHeight / 2

   ' Display the pop-up menu.
   PopupMenu mnuEdit, vbPopupMenuCenterAlign Or _
   vbPopupMenuRightButton, xloc, yloc
End Sub

The Boldcommand Argument

You use the boldcommand argument to specify the name of a menu control in the displayed pop-up menu that you want to appear in bold. Only one menu control in the pop-up menu can be bold.