PopupMenu Method

Displays a pop-up menu at the current mouse location or at specified coordinates. Doesn't support named arguments.

Special Note: If you use the regular Visual Basic PopupMenu method, your system tray popup menus will probably not go away properly when the user clicks somewhere else on the screen. Whenever you want to popup a menu from the system tray, it is strongly advised you use the PopupMenu method of the SysTray control instead. 

Syntax

csSysTray[(index)].PopupMenu MenuName, [Flags], [X], [Y], [DefaultMenu]

The PopupMenu method syntax has these parts:

Part Description
csSysTray The SysTray control you are working with, or a control array of SysTray controls.
MenuName Required. The name of the pop-up menu to be displayed. The specified menu must have at least one submenu (item on it).
Flags Optional. A value or constant that specifies the location and behavior of a pop-up menu, as described in Settings.
X Optional. Specifies the x-coordinate where the pop-up menu is displayed. If omitted, the mouse coordinate is used.
Y Optional. Specifies the y-coordinate where the pop-up menu is displayed. If omitted, the mouse coordinate is used.
DefaultMenu Optional. Specifies the name of a menu control in the pop-up menu to display its caption in bold text. If omitted, no controls in the pop-up menu appear in bold. Making an item in a menu bold usually signifies to the user that it is the defualt item (they would get by left-clicking on the system tray icon).

Settings

The settings for Flags are:

Constant (location) Value Description
vbPopupMenuLeftAlign 0 (Default) The left side of the pop-up menu is located at x.
vbPopupMenuCenterAlign 4 The pop-up menu is centered at x.
vbPopupMenuRightAlign 8 The right side of the pop-up menu is located at x.
Constant (behavior) Value Description
vbPopupMenuLeftButton 0 (Default) An item on the pop-up menu reacts to a mouse click only when you use the left mouse button.
vbPopupMenuRightButton 2 (Recommended) An item on the pop-up menu reacts to a mouse click when you use either the right or the left mouse button.

To specify two flags, combine one constant from each group using the Or operator.

Examples

Example 1 (Popping up a menu from a system tray icon when it is right-clicked)

Private Sub csSysTray1_RightClick()

'we pass in vbPopupMenuRightButon as a flag so our popup menu works with both buttons.
csSysTray1.PopupMenu mnuSysTray, vbPopupMenuRightButton

End Sub
 

Example 2 (Popping up a menu again, but this time setting mnuHelpAbout to be bold, the default)

Private Sub csSysTray1_RightClick()

'we pass in vbPopupMenuRightButon as a flag so our popup menu works with both buttons.
csSysTray1.PopupMenu mnuSysTray, vbPopupMenuRightButton, , , mnuHelpAbout

End Sub


Remarks

These constants are listed in the Visual Basic (VB) object library in the Object Browser.

You specify the unit of measure for the x and y coordinates using the ScaleMode property. The x and y coordinates define where the pop-up is displayed relative to the specified form. If the x and y coordinates aren't included, the pop-up menu is displayed at the current location of the mouse pointer.

When you display a pop-up menu, the code following the call to the PopupMenu method isn't executed until the user either chooses a command from the menu (in which case the code for that command's Click event is executed before the code following the PopupMenu statement) or cancels the menu. In addition, only one pop-up menu can be displayed at a time; therefore, calls to this method are ignored if a pop-up menu is already displayed or if a pull-down menu is open.