Color scheme: Riviera
You are here: start » en » Documentation » Microsoft Access development tools » WKL Forms Object Library » WKL Forms Object Library how-to guides » How to: Add items to the shortcut menu

How to: Add items to the shortcut menu

Add your application's own commands, such as “Show details”, to the right-click menu of a tree or tree list.

Requires WKL Forms Object Library 1.23 or later.

Solution overview

AddMenuItem appends an item with a caption and a tag to the built-in shortcut menu, and MenuItemClick reports each click with the tag and the key of the node the menu was opened on. ContextMenu = False hides the built-in items so that only yours remain. NodeRightClick with Cancel = True suppresses the menu entirely, for example to show an Access shortcut menu instead.

Implementation

Private Sub Form_Load()
    With Me!Tree1.Object
        .AddMenuItem "Show details", "details", True       ' True: separator before the item
        .AddMenuItem "Mark as done", "done"
    End With
End Sub
 
Private Sub Tree1_MenuItemClick(ByVal Tag As String, ByVal Key As Variant)
    Select Case Tag
        Case "details": DoCmd.OpenForm "frmDetails", , , "ID = " & Key
        Case "done": CurrentDb.Execute "UPDATE tblTasks SET Done = True WHERE ID = " & Key
    End Select
End Sub

Remarks

  • Custom items aren't saved with the form, so add them in Form_Load.
  • RemoveMenuItem removes a single item by its tag, and ClearMenuItems removes all custom items.
  • The built-in items come first, and each appears only if its property allows the operation. In a WklTreeList, WklGantt, or WklGrid, MenuPlacement places your items before the built-in ones, shows yours alone, or hides yours. MenuItemEnabled, MenuItemChecked, MenuItemGlyph, MenuItemShortcut, and MenuItemParent (submenus) change an item after AddMenuItem, for example in BeforeContextMenu, which reports the record before the menu opens.
  • If the form has no module, use an Access shortcut menu bar instead (How to: Use an Access shortcut menu bar in a grid).
  • The captions are your own text; the built-in items follow the language of the components.
en/wiki/msaccess/comctl/how-to/add-context-menu-items.txt · Last modified: by Wolfgang van der Stille