Skip to main content

How to: Add custom menu items to the standard menus

  • 3 minutes to read

The following example demonstrates how to add a custom item to a standard context menu and handle the click event for this item. The TreeList.PopupMenuShowing event is handled to add the Clear All item to the summary footer menu. Clicking this item cancels the summary calculations for all Tree List columns.

ContextMenus - CustomItemClick

using DevExpress.Utils.Menu;
using DevExpress.XtraTreeList;
using DevExpress.XtraTreeList.Columns;
//...
private void treeList1_PopupMenuShowing(object sender, DevExpress.XtraTreeList.PopupMenuShowingEventArgs e) {
    TreeList tL = sender as TreeList;
    TreeListHitInfo hitInfo = tL.CalcHitInfo(e.Point);
    if (hitInfo.HitInfoType == HitInfoType.SummaryFooter) {
        DXMenuItem menuItem = new DXMenuItem("Clear All", this.clearAllMenuItemClick);
        menuItem.Tag = hitInfo.Column;
        e.Menu.Items.Add(menuItem);
    }
}

private void clearAllMenuItemClick(object sender, EventArgs e) {
    TreeListColumn clickedColumn = (sender as DXMenuItem).Tag as TreeListColumn;
    if (clickedColumn == null) return;
    TreeList tl = clickedColumn.TreeList;
    foreach (TreeListColumn column in tl.Columns)
        column.SummaryFooter = SummaryItemType.None;
}

How to: Display a Command that Deletes a Node

The example below shows how to display a custom context menu with a click on a node indicator. The menu contains the Delete Node command.

image

The TreeList.PopupMenuShowing event fires when the default menu is about to be shown. The TreeList.CalcHitInfo method in the event handler returns internal information about the clicked visual element. If a node indicator is clicked, the Delete Node command is added to the menu. The DXMenuItem.Click event handler removes the node.

using DevExpress.Utils.Menu;
using DevExpress.XtraTreeList;
using DevExpress.XtraTreeList.Nodes;

private void treeList1_PopupMenuShowing(object sender, DevExpress.XtraTreeList.PopupMenuShowingEventArgs e) {
    // Check if a node's indicator cell is clicked.
    TreeListHitInfo hitInfo = (sender as TreeList).CalcHitInfo(e.Point);
    TreeListNode node = null;
    if (hitInfo.HitInfoType == HitInfoType.RowIndicator) {
        node = hitInfo.Node;
    }
    if (node == null) return;
    // Create the Delete Node command.
    DXMenuItem menuItem = new DXMenuItem("Delete Node", this.deleteNodeMenuItemClick);
    menuItem.Tag = node;
    e.Menu.Items.Add(menuItem);
}

private void deleteNodeMenuItemClick(object sender, EventArgs e) {
    DXMenuItem item = sender as DXMenuItem;
    if (item == null) return;
    TreeListNode node = item.Tag as TreeListNode;
    if (node == null) return;
    node.TreeList.DeleteNode(node);
}