Skip to main content

How to: Customize or Hide the Popup Menu

  • 3 minutes to read

This example demonstrates how to customize the SpreadsheetControl‘s context menu. In particular, this sample demonstrates how to remove or disable the existing items of the context menu and add new menu items.

Handle the SpreadsheetControl.PopupMenuShowing event to change specific items of the SpreadsheetControl’s popup menu every time it is invoked. The current menu can be accessed via the Menu property of the event parameter.

All context menu types are listed in the SpreadsheetMenuType enumeration. The following code modifies the SpreadsheetMenuType.Cell menu, which can be invoked by right-clicking any cell on a worksheet.

Note

The CommandPopupMenu<T>.EnableMenuItem method does not enable the context menu item if you set the corresponding command’s ICommandUIState.Enabled property to false.

using DevExpress.XtraSpreadsheet;
using DevExpress.XtraSpreadsheet.Commands;
using DevExpress.XtraSpreadsheet.Services;
using DevExpress.XtraSpreadsheet.Menu;
        private void spreadsheetControl1_PopupMenuShowing(object sender, PopupMenuShowingEventArgs e)
        {
            if (e.MenuType == SpreadsheetMenuType.Cell)
            {
                // Remove the "Clear Contents" menu item.
                e.Menu.RemoveMenuItem(SpreadsheetCommandId.FormatClearContentsContextMenuItem);

                // Disable the "Hyperlink" menu item.
                e.Menu.DisableMenuItem(SpreadsheetCommandId.InsertHyperlinkContextMenuItem);

                // Create a menu item for the Spreadsheet command, which inserts a picture into a worksheet.
                ISpreadsheetCommandFactoryService service = (ISpreadsheetCommandFactoryService)spreadsheetControl1.GetService(typeof(ISpreadsheetCommandFactoryService));
                SpreadsheetCommand cmd = service.CreateCommand(SpreadsheetCommandId.InsertPicture);
                SpreadsheetMenuItemCommandWinAdapter menuItemCommandAdapter = new SpreadsheetMenuItemCommandWinAdapter(cmd);
                SpreadsheetMenuItem menuItem = (SpreadsheetMenuItem)menuItemCommandAdapter.CreateMenuItem(DevExpress.Utils.Menu.DXMenuItemPriority.Normal);
                menuItem.BeginGroup = true;
                e.Menu.Items.Add(menuItem);

                // Insert a new item into the Spreadsheet popup menu and handle its click event.
                SpreadsheetMenuItem myItem = new SpreadsheetMenuItem("My Menu Item", new EventHandler(MyClickHandler));
                e.Menu.Items.Add(myItem);
            }
        }

        public void MyClickHandler(object sender, EventArgs e)
        {
            MessageBox.Show("My Menu Item Clicked!");
        }

The image below illustrates the result. The image on the left shows the context menu before customization, and the image on the right shows the custom popup menu.

CustomPopupMenu

Disable or Hide the Popup Menu

If you wish to prevent end-users from invoking the context menu, you can disable or hide it using the SpreadsheetBehaviorOptions.ShowPopupMenu property of the SpreadsheetBehaviorOptions object, which can be accessed from the SpreadsheetControlOptions.Behavior property. The SpreadsheetBehaviorOptions.ShowPopupMenu property uses the DocumentCapability enumeration values to specify the availability of certain functionality in the SpreadsheetControl. Select the DocumentCapability.Disabled or DocumentCapability.Hidden value to make the context menu unavailable to end-users.

// Hide the popup menu.
spreadsheetControl1.Options.Behavior.ShowPopupMenu = DocumentCapability.Hidden;