Back to Devexpress

Pop-Up Menus

windowsforms-119051-controls-and-libraries-spreadsheet-visual-elements-pop-up-menus.md

latest8.7 KB
Original Source

Pop-Up Menus

  • Jun 24, 2019
  • 4 minutes to read

The Spreadsheet has several types of context (pop-up) menus which are invoked when the user clicks different visual objects. The SpreadsheetControl provides the SpreadsheetControl.PopupMenuShowing event which allows you to customize context menus by adding or removing items.

Context Menus Overview

Menu TypeAppearanceDescription
SpreadsheetMenuType.AutoFilterSpecifies a context menu which can be invoked by clicking the AutoFilter drop-down arrow.
SpreadsheetMenuType.CellSpecifies a context menu which can be invoked by right-clicking any cell in a worksheet.
SpreadsheetMenuType.ChartSpecifies a context menu which can be invoked by right-clicking a chart in a worksheet.
SpreadsheetMenuType.ColumnHeadingSpecifies a context menu which can be invoked by right-clicking a column header.
SpreadsheetMenuType.DrawingObjectsSpecifies a context menu which can be invoked by right-clicking a drawing object when a worksheet contains several drawing objects, such as pictures or charts.
SpreadsheetMenuType.PictureSpecifies a context menu which can be invoked by right-clicking a picture embedded in a worksheet.
SpreadsheetMenuType.PivotTableSpecifies a context menu which can be invoked by right-clicking any cell in a pivot table.
SpreadsheetMenuType.PivotTableAutoFilterSpecifies a context menu which can be invoked by clicking the AutoFilter drop-down arrow on the row or column label of a pivot table.
SpreadsheetMenuType.RowHeadingSpecifies a context menu which can be invoked by right-clicking a row header.
SpreadsheetMenuType.SelectAllButtonSpecifies a context menu which can be invoked by right-clicking the Select All button in the upper-left corner of a worksheet.
SpreadsheetMenuType.SheetTabSpecifies a context menu which can be invoked by right-clicking a worksheet tab.

Customizing Context Menus in Code

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.

csharp
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!");
        }
vb
Imports DevExpress.XtraSpreadsheet
Imports DevExpress.XtraSpreadsheet.Commands
Imports DevExpress.XtraSpreadsheet.Services
Imports DevExpress.XtraSpreadsheet.Menu
        Private Sub spreadsheetControl1_PopupMenuShowing(ByVal sender As Object, ByVal e As PopupMenuShowingEventArgs) Handles spreadsheetControl1.PopupMenuShowing
            If e.MenuType = SpreadsheetMenuType.Cell Then
                ' 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.
                Dim service As ISpreadsheetCommandFactoryService = CType(spreadsheetControl1.GetService(GetType(ISpreadsheetCommandFactoryService)), ISpreadsheetCommandFactoryService)
                Dim cmd As SpreadsheetCommand = service.CreateCommand(SpreadsheetCommandId.InsertPicture)
                Dim menuItemCommandAdapter As New SpreadsheetMenuItemCommandWinAdapter(cmd)
                Dim menuItem As SpreadsheetMenuItem = CType(menuItemCommandAdapter.CreateMenuItem(DevExpress.Utils.Menu.DXMenuItemPriority.High), SpreadsheetMenuItem)
                menuItem.BeginGroup = True
                e.Menu.Items.Add(menuItem)

                ' Insert a new item into the Spreadsheet popup menu and handle its click event.
                Dim myItem As New SpreadsheetMenuItem("My Menu Item", New EventHandler(AddressOf MyClickHandler))
                e.Menu.Items.Add(myItem)
            End If
        End Sub

        Public Sub MyClickHandler(ByVal sender As Object, ByVal e As EventArgs)
            MessageBox.Show("My Menu Item Clicked!")
        End Sub

See Also

How to: Customize or Hide the Popup Menu