GridDesktopのコンテキストメニューの管理

紹介

ContextMenuManagerクラスは、コンテキストメニューアイテムを管理するために使用されます。GridDesktop.ContextMenuManager属性は、ContextMenuManagerオブジェクトのインスタンスを取得します。たとえば、ContextMenuManager.MenuItemAvailable_Copy属性は、コンテキストメニューアイテムの ‘Copy’ が利用可能かどうかを取得または設定します。同様に、異なるコンテキストメニューアイテムに対応する属性があります。

**重要:**デフォルトでは、すべてのコンテキストメニューアイテムがリストに表示されます。

コンテキストメニューの管理

コンテキストメニューアイテムの非表示

このタスクを実行するには、最初にGridDesktopのデフォルトのコンテキストメニューを確認します。

GridDeskopのデフォルトメニュー

todo:image_alt_text

以下のコードを使用して、いくつかのメニューアイテムを非表示にします:

// 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;

上記のコードを実行した後、一部のメニューアイテムがユーザーには表示されなくなります:

一部のメニューアイテムが非表示になります

todo:image_alt_text

新しいメニューアイテムの追加

以下のコードスニペットを使用してリストに新しいコンテキストメニューアイテムを追加します。

// 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);

新しいコマンド/オプションのイベントハンドラも指定します。

// 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);
}

上記のコードを実行した後、コンテキストメニューに新しいメニューアイテムが表示されます。セルをクリックするとメッセージも表示されます。

リストに新しいメニューアイテムが追加されます

todo:image_alt_text