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:
- GridView.PopupMenuShowing
- LayoutControl.PopupMenuShowing
- SchedulerControl.PopupMenuShowing
- VGridControl.PopupMenuShowing / PropertyGridControl.PopupMenuShowing
- PivotGridControl.PopupMenuShowing
- PictureEdit.PopupMenuShowing
- RibbonControl.ShowCustomizationMenu
- BarManager.QueryShowPopupMenu, etc.
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:
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:
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.
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?
- Create and customize a new ContextMenu.
- Handle the
ShownEditor
event (GridView.ShownEditor, TreeList.ShownEditor, BarEditItem.ShownEditor, etc.). - In the event handler, read the
ActiveEditor
property (GridView.ActiveEditor, TreeList.ActiveEditor, BarManager.ActiveEditor, etc.) to retrieve the in-place editor instance. - Assign your CustomMenu object to the editor's RepositoryItem.ContextMenu property:
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.
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