KB Article T929350
Visible to All Users

Context and Popup Menus - WinForms Cheat Sheet

Other DevExpress WinForms Cheat Sheets


Controls that have built-in context menus (for instance GridControl, TreeList, Editors, etc.) often provide the PopupMenuShowing or BeforeShowMenu events that you can handle to customize these menus:

All TextEdit-based editors (TextEdit, ButtonEdit, SpinEdit, ImageEdit, etc.) have the BeforeShowMenu event that you can access via a Repository item: RepositoryItemTextEdit.BeforeShowMenu or TextEdit.Properties.BeforeShowMenu.

If you are not familiar with Data Editors yet, we recommend that you review the following articles that can help you get started faster: Editor Architecture: Repositories and Repository Items, DevExpress WinForms Cheat Sheet - Data Editors

To access items in the menu, use the e.Menu.Items collection - you can add, remove, reorder, rename items or change their icons. Use Tags of items (DXMenuItem.Tag) in the Menu to identify required items.

Some controls like (TreeList, GridView, PivotGrid, VGridControl and others) offer the OptionsMenu settings group that provides access to menu-related options:

DevExpress controls don't display empty menus. If you handle PopupMenuShowing and remove all items from the e.Menu.Items collection, a context menu will not be displayed. You can also set the e.Allow parameter to False to disable the menu.

If you need a custom context menu for a DevExpress WinForms editor, you can utilize the RepositoryItem.ContextMenu property instead of handling aforementioned events. Create a new ContextMenu instance, fill it with items, and assign it to the RepositoryItems.ContextMenu property:

C#
ContextMenu emptyMenu = new ContextMenu(); textEdit1.Properties.ContextMenu = emptyMenu;

Menus for other controls that have the Control.ContextMenu can also be customized in this manner.


Examples

How to remove the Fit Image option from the PictureEdit Zoom Menu?

Handle the PictureEdit.PopupMenuShowing event to find and remove the required item:

Code Example
C#
private void PictureEdit1_PopupMenuShowing(object sender, DevExpress.XtraEditors.Events.PopupMenuShowingEventArgs e) { var zoomMenu = e.PopupMenu.Items.Where(item => (StringId)item.Tag == StringId.PictureEditMenuZoom). FirstOrDefault() as DXSubMenuItem; if (zoomMenu == null) return; var fitItem = zoomMenu.Items.Where(item => (StringId)item.Tag == StringId.PictureEditMenuFitImage).FirstOrDefault(); if (fitItem == null) return; zoomMenu.Items.Remove(fitItem); }

How to hide the Time Scale Captions item in the Scheduler context menu?

Handle SchedulerControl.PopupMenuShowing and use GetPopupMenuById and GetMenuItemById methods to find required items. Then, remove them from the e.Menu.Items collection:

Code Example
C#
private void SchedulerControl1_PopupMenuShowing(object sender, DevExpress.XtraScheduler.PopupMenuShowingEventArgs e) { if (e.Menu.Id == SchedulerMenuItemId.DefaultMenu) { SchedulerPopupMenu item = e.Menu.GetPopupMenuById(SchedulerMenuItemId.TimeScaleVisible); item.Visible = false; } }

How to display a custom menu when a Data Grid row is right clicked?

Handle GridView.PopupMenuShowing and use the PopupMenuShowingEventArgs.MenuType property to identify which element was right-clicked. If this property returns "GridMenuType.Row", customize the menu as your needs dictate.

Code Example
C#
private void gridView_PopupMenuShowing(object sender, DevExpress.XtraGrid.Views.Grid.PopupMenuShowingEventArgs e) { if (e.MenuType == DevExpress.XtraGrid.Views.Grid.GridMenuType.Row) { e.Menu.Items.Clear(); DXMenuCheckItem item = new DXMenuCheckItem("Processed", true, null, OnCheckedChanged); e.Menu.Items.Add(item); } }

How to replace the standard context menu with my custom menu when a default in-place editor is right-clicked?

Code Example
C#
private void gridView1_ShownEditor(object sender, System.EventArgs e) { GridView view = sender as GridView; view.ActiveEditor.ContextMenu = contextMenu1; }

To disable the standard context menu, handle the BeforeShowMenu event.

How to display a context menu when a date cell in CalendarControl is right clicked?

  • Handle the CalendarControl.MouseClick event.
  • Check what mouse button was pressed
  • Call the GetHitInfo(Point) method to obtain a CalendarHitInfo object, and use this object to identify whether the cursor is above a date cell.
  • Use the ShowPopup method to show a custom context menu.
Code Example
C#
private void calendarControl1_MouseClick(object sender, MouseEventArgs e) { if(e.Button == MouseButtons.Right) { CalendarControl calendarControl = sender as CalendarControl; CalendarHitInfo chi = calendarControl.GetHitInfo(e.Location); if (chi.IsInCell) { popupMenu1.ShowPopup(Cursor.Position); } } }

How to customize the TextEdit PopupMenu item captions?

You can handle the static (shared in VB) DevExpress.Utils.Localization.XtraLocalizer.QueryLocalizedString event and modify translations for the following strings:

  • StringId.TextEditMenuUndo
  • StringId.TextEditMenuCut
  • StringId.TextEditMenuCopy
  • StringId.TextEditMenuPaste
  • StringId.TextEditMenuDelete
  • StringId.TextEditMenuSelectAll

Help us improve this article

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.