Back to Devexpress

TreeList.PopupMenuShowing Event

windowsforms-devexpress-dot-xtratreelist-dot-treelist-de49cdcd.md

latest12.3 KB
Original Source

TreeList.PopupMenuShowing Event

Allows you to customize default context menus or invoke custom menus.

Namespace : DevExpress.XtraTreeList

Assembly : DevExpress.XtraTreeList.v25.2.dll

NuGet Packages : DevExpress.Win.Navigation, DevExpress.Win.TreeList

Declaration

csharp
[DXCategory("Behavior")]
public event PopupMenuShowingEventHandler PopupMenuShowing
vb
<DXCategory("Behavior")>
Public Event PopupMenuShowing As PopupMenuShowingEventHandler

Event Data

The PopupMenuShowing event's data class is PopupMenuShowingEventArgs. The following properties provide information specific to this event:

PropertyDescription
AllowGets or sets whether to display the context menu. Inherited from BasePopupMenuShowingEventArgs.
HitInfoProvides access to information about the clicked visual element.
MenuGets or sets the control’s popup menu that will be shown.
MenuTypeGets the type of the context menu that is about to be shown.
PointGets or sets coordinates of the invoked context menu (top-left corner) relative to the parent control. Inherited from BasePopupMenuShowingEventArgs.
ScreenPointGets coordinates of the invoked context menu (top-left corner) relative to the screen. Inherited from BasePopupMenuShowingEventArgs.

The event data class exposes the following methods:

MethodDescription
ShowCustomMenu(IDXDropDownControlEx)Invokes a custom context menu instead of the control’s menu. Inherited from BasePopupMenuShowingEventArgs.
ShowCustomMenu(ContextMenuStrip)Invokes a custom context menu instead of the control’s menu. Inherited from BasePopupMenuShowingEventArgs.

Remarks

The Tree List shows a context menu when the user right-clicks within the following areas:

Refer to the following help topic for more information: Context Menus.

Examples

The following code sample invokes a custom context menu when a user right-clicks a column header:

  1. Add a BarManager to the form and create a custom PopupMenu as demonstrated in the following help topic: Popup Menus:

  2. Handle the TreeList.PopupMenuShowing event and call the e.ShowCustomMenu method to display your custom menu instead of the default menu.

csharp
void TreeList1_PopupMenuShowing(object sender, PopupMenuShowingEventArgs e) {
    if (e.MenuType == TreeListMenuType.Column) {
        popupMenu_Column.Tag = e.HitInfo;
        popupMenu_Column.MenuCaption = $"{e.HitInfo.Column}";

        e.ShowCustomMenu(popupMenu_Column);
    }
}
TreeListHitInfo GetHitInfo(BarItemLink link) {
    PopupMenu menu = link.LinkedObject as PopupMenu;
    return menu.Tag as TreeListHitInfo;
}
void barButtonItem_Filter_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) {
    TreeListHitInfo info = GetHitInfo(e.Link);
    info.Column.TreeList.ShowFilterEditor(info.Column);
}

void barButtonItem_ColumnChooser_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) {
    TreeListHitInfo info = GetHitInfo(e.Link);
    info.Column.TreeList.ShowCustomization();
}
vb
Private Sub TreeList1_PopupMenuShowing(ByVal sender As Object, ByVal e As PopupMenuShowingEventArgs)
    If e.MenuType = TreeListMenuType.Column Then
        popupMenu_Column.Tag = e.HitInfo
        popupMenu_Column.MenuCaption = $"{e.HitInfo.Column}"

        e.ShowCustomMenu(popupMenu_Column)
    End If
End Sub

Private Function GetHitInfo(ByVal link As BarItemLink) As TreeListHitInfo
    Dim menu As PopupMenu = TryCast(link.LinkedObject, PopupMenu)
    Return TryCast(menu.Tag, TreeListHitInfo)
End Function

Private Sub barButtonItem_Filter_ItemClick(ByVal sender As Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs)
    Dim info As TreeListHitInfo = GetHitInfo(e.Link)
    info.Column.TreeList.ShowFilterEditor(info.Column)
End Sub

Private Sub barButtonItem_ColumnChooser_ItemClick(ByVal sender As Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs)
    Dim info As TreeListHitInfo = GetHitInfo(e.Link)
    info.Column.TreeList.ShowCustomization()
End Sub

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

The following sample code handles the TreeList.PopupMenuShowing event to execute the following actions:

  • Disable Min and Max items in the summary footer context menu for the “Department” column.
  • Remove the Column Chooser item from the column header context menu.

csharp
void TreeList1_PopupMenuShowing(object sender, PopupMenuShowingEventArgs e) {
    // Disable Min and Max items in the summary footer menu for the "Department" column:
    if (e.MenuType == TreeListMenuType.Summary && e.HitInfo.Column.FieldName == nameof(SalesData.Department)) {
        e.Menu.Find(TreeListStringId.MenuFooterMax).Enabled = false;
        e.Menu.Find(TreeListStringId.MenuFooterMin).Enabled = false;
    }
    // Remove the "Column Chooser" item from the column header menu:
    if (e.MenuType == TreeListMenuType.Column) {
        e.Menu.Remove(TreeListStringId.MenuColumnColumnCustomization);
        e.Menu.Remove(TreeListStringId.MenuColumnBandCustomization);
    }
}
vb
Private Sub TreeList1_PopupMenuShowing(ByVal sender As Object, ByVal e As PopupMenuShowingEventArgs)
    ' Disable Min and Max items in the summary footer menu for the "Department" column:
    If e.MenuType = TreeListMenuType.Summary AndAlso e.HitInfo.Column.FieldName = NameOf(SalesData.Department) Then
        e.Menu.Find(TreeListStringId.MenuFooterMax).Enabled = False
        e.Menu.Find(TreeListStringId.MenuFooterMin).Enabled = False
    End If
    ' Remove the "Column Chooser" item from the column header menu:
    If e.MenuType = TreeListMenuType.Column Then
        e.Menu.Remove(TreeListStringId.MenuColumnColumnCustomization)
        e.Menu.Remove(TreeListStringId.MenuColumnBandCustomization)
    End If
End Sub

See Also

TreeListMenuItemClick

OptionsMenu

TreeList Class

TreeList Members

DevExpress.XtraTreeList Namespace