Back to Devexpress

Add Custom Menu Items to Standard Menus

windowsforms-5701-controls-and-libraries-tree-list-feature-center-context-menus-add-custom-menu-items-to-the-standard-menus.md

latest5.5 KB
Original Source

Add Custom Menu Items to Standard Menus

  • Mar 01, 2024
  • 3 minutes to read

Handle the TreeList.PopupMenuShowing event to add custom menu items to standard context menus. The e.Menu.Items collection contains displayed menu items and allows you to add custom items.

View Example: Customize Node Context Menu

You can use the following objects as custom items:

Use the DXMenuItem.Tag property to pass custom information (for example, a clicked tree list element) to the item’s event handlers.

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.

csharp
using DevExpress.Utils.Menu;
using DevExpress.XtraTreeList;
using DevExpress.XtraTreeList.Columns;
//...
void treeList1_PopupMenuShowing(object sender, DevExpress.XtraTreeList.PopupMenuShowingEventArgs e) {
    if (e.MenuType == TreeListMenuType.Summary && e.HitInfo.HitInfoType == HitInfoType.SummaryFooter) {
        DXMenuItem menuItem = new DXMenuItem("Clear All", clearAllMenuItemClick);
        menuItem.Tag = e.HitInfo.Column;
        e.Menu.Items.Add(menuItem);
    }
}

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;
}
vb
Imports DevExpress.Utils.Menu
Imports DevExpress.XtraTreeList
Imports DevExpress.XtraTreeList.Columns
'...
Private Sub treeList1_PopupMenuShowing(ByVal sender As Object, ByVal e As DevExpress.XtraTreeList.PopupMenuShowingEventArgs)
    If e.MenuType = TreeListMenuType.Summary AndAlso e.HitInfo.HitInfoType = HitInfoType.SummaryFooter Then
        Dim menuItem As DXMenuItem = New DXMenuItem("Clear All", AddressOf clearAllMenuItemClick)
        menuItem.Tag = e.HitInfo.Column
        e.Menu.Items.Add(menuItem)
    End If
End Sub

Private Sub clearAllMenuItemClick(ByVal sender As Object, ByVal e As EventArgs)
    Dim clickedColumn As TreeListColumn = TryCast((TryCast(sender, DXMenuItem)).Tag, TreeListColumn)
    If clickedColumn Is Nothing Then Return
    Dim tl As TreeList = clickedColumn.TreeList

    For Each column As TreeListColumn In tl.Columns
        column.SummaryFooter = SummaryItemType.None
    Next
End Sub

Add an Item that Deletes a Node

The example below invokes a custom context menu when a user right-clicks the node indicator. The menu contains the Delete Node command.

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

void treeList1_PopupMenuShowing(object sender, DevExpress.XtraTreeList.PopupMenuShowingEventArgs e) {
    // Check if a node indicator cell is clicked.
    if (e.MenuType == TreeListMenuType.Node && e.HitInfo.InRowIndicator) {
        // Create a Delete Node item.
        DXMenuItem menuItem = new DXMenuItem("Delete Node", this.deleteNodeMenuItemClick);
        menuItem.Tag = e.HitInfo.Node;
        e.Menu.Items.Add(menuItem);
    }
}

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);
}
vb
Imports DevExpress.Utils.Menu
Imports DevExpress.XtraTreeList
Imports DevExpress.XtraTreeList.Nodes

Private Sub treeList1_PopupMenuShowing(ByVal sender As Object, ByVal e As DevExpress.XtraTreeList.PopupMenuShowingEventArgs)
    ' Check if a node indicator cell is clicked.
    If e.MenuType = TreeListMenuType.Node AndAlso e.HitInfo.InRowIndicator Then
        ' Create a Delete Node item.
        Dim menuItem As DXMenuItem = New DXMenuItem("Delete Node", AddressOf Me.deleteNodeMenuItemClick)
        menuItem.Tag = e.HitInfo.Node
        e.Menu.Items.Add(menuItem)
    End If
End Sub

Private Sub deleteNodeMenuItemClick(ByVal sender As Object, ByVal e As EventArgs)
    Dim item As DXMenuItem = TryCast(sender, DXMenuItem)
    If item Is Nothing Then Return
    Dim node As TreeListNode = TryCast(item.Tag, TreeListNode)
    If node Is Nothing Then Return
    node.TreeList.DeleteNode(node)
End Sub