Managing GridDesktops Context Menu

Introduction

The ContextMenuManager class is used to manage the context menu items. The GridDesktop.ContextMenuManager attribute gets the instance of ContextMenuManager object. For example, the ContextMenuManager.MenuItemAvailable_Copy attribute gets or sets a value indicating whether the context menu item Copy is available or not. Similarly, we have all the corresponding attributes for different context menu items.

IMPORTANT: By default, all the context menu items are visible in the list.

Managing the Context Menu

Hiding Context Menu Items

To perform this task, we first take a look the default context menu the GridDesktop has.

GridDeskop’s default menu

todo:image_alt_text

Now, hide some menu items using the code below:

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Get the ContextMenuManager
ContextMenuManager cmm = this.grdDataEntry.ContextMenuManager;
// Hide the Copy option in the context menu
cmm.MenuItemAvailable_Copy = false;
// Hide the InsertRow option in the context menu
cmm.MenuItemAvailable_InsertRow = false;
// Hide the Format Cell dialog box
cmm.MenuItemAvailable_FormatCells = false;

After executing the above code, some menu items will not be visible for the users:

Some menu items are hidden

todo:image_alt_text

Adding New Menu Items

Add a new context menu item to the list using the following code snippet.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Get the active worksheet
Worksheet sheet = grdDataEntry.GetActiveWorksheet();
// Set the total columns diaplyed in the grid
sheet.ColumnsCount = 15;
// Set the total rows displayed in the grid
sheet.RowsCount = 15;
// Define a new menu item and specify its event handler
MenuItem mi = new MenuItem("newMenuItem", new System.EventHandler(miClicked));
// Set the label
mi.Text = "New Item";
// Add the menu item to the GridDesktop's context menu
grdDataEntry.ContextMenu.MenuItems.Add(mi);

We also specify an event handler for the new command/option.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Event Handler for the new menu item
private void miClicked(object sender, EventArgs e)
{
MenuItem mi = (MenuItem)sender;
MessageBox.Show("miCliked: " + mi.Text);
}

After executing the above code, a new menu item can be seen in the context menu. A message will also appear when cell is clicked.

A new menu item is added to the list

todo:image_alt_text