Back to Devexpress

Change the Behavior of Standard Menu Items

windowsforms-5700-controls-and-libraries-tree-list-feature-center-context-menus-change-the-behavior-of-the-standard-menu-items.md

latest2.0 KB
Original Source

Change the Behavior of Standard Menu Items

  • Mar 01, 2024

A click on a menu item in standard context menus raises the TreeList.TreeListMenuItemClick event. You can handle this event to:

  • Execute custom actions.
  • Cancel the default action (set the e.Handled property to true).

Example

The following code sample changes the behavior of Sort Ascending and Sort Descending items in the column header menu. The default items do not clear the applied sorting. The TreeList.TreeListMenuItemClick event handler clears the existing sorting before the sort operation.

csharp
void treeList1_TreeListMenuItemClick(object sender, TreeListMenuItemClickEventArgs e) {
   if (e.MenuItem.Caption == "Sort Ascending" || e.MenuItem.Caption == "Sort Descending") {
         e.Column.TreeList.ClearSorting();
         if (e.MenuItem.Caption == "Sort Ascending")
            e.Column.SortOrder = SortOrder.Ascending;
         else
            e.Column.SortOrder = SortOrder.Descending;
         e.Handled = true;
   }
}
vb
Private Sub treeList1_TreeListMenuItemClick(ByVal sender As Object, ByVal e As TreeListMenuItemClickEventArgs)
    If e.MenuItem.Caption = "Sort Ascending" OrElse e.MenuItem.Caption = "Sort Descending" Then
        e.Column.TreeList.ClearSorting()
        If e.MenuItem.Caption = "Sort Ascending" Then
            e.Column.SortOrder = SortOrder.Ascending
        Else
            e.Column.SortOrder = SortOrder.Descending
        End If
        e.Handled = True
    End If
End Sub