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. RemoveMenuItemremoves a single item by its tag, andClearMenuItemsremoves all custom items.- The built-in items come first, and each appears only if its property allows the operation. In a
WklTreeList,WklGantt, orWklGrid,MenuPlacementplaces your items before the built-in ones, shows yours alone, or hides yours.MenuItemEnabled,MenuItemChecked,MenuItemGlyph,MenuItemShortcut, andMenuItemParent(submenus) change an item afterAddMenuItem, for example inBeforeContextMenu, 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.
Related topics
en/wiki/msaccess/comctl/how-to/add-context-menu-items.txt · Zadnja sprememba: uporabnika Wolfgang van der Stille