Windows フォームのショートカット メニュー項目を追加および削除する方法について説明します。
Windows フォーム ContextMenu コンポーネントには、選択したオブジェクトに関連する頻繁に使用されるコマンドのメニューが用意されています。 MenuItem コレクションにMenuItemsオブジェクトを追加することで、ショートカット メニューに項目を追加できます。
ショートカット メニューからアイテムを完全に削除できます。ただし、実行時には、代わりに項目を非表示または無効にする方が適切な場合があります。
重要
MenuStrip と ContextMenuStrip は、以前のバージョンの MainMenu コントロールと ContextMenu コントロールに機能を置き換えて追加しますが、下位互換性と将来の使用の両方を選択した場合、MainMenu と ContextMenu は保持されます。
ショートカット メニューから項目を削除するには
特定のメニュー項目を削除するには、Remove コンポーネントのRemoveAt コレクションのMenuItemsまたはContextMenuメソッドを使用します。
' Removes the first item in the shortcut menu. ContextMenu1.MenuItems.RemoveAt(0) ' Removes a particular object from the shortcut menu. ContextMenu1.MenuItems.Remove(mnuItemNew)
// Removes the first item in the shortcut menu. contextMenu1.MenuItems.RemoveAt(0); // Removes a particular object from the shortcut menu. contextMenu1.MenuItems.Remove(mnuItemNew);
// Removes the first item in the shortcut menu. contextMenu1->MenuItems->RemoveAt(0); // Removes a particular object from the shortcut menu. contextMenu1->MenuItems->Remove(mnuItemNew);
-又は-
メニューからすべての項目を削除するには、
Clear
コンポーネントのMenuItems
コレクションのContextMenu メソッドを使用します。ContextMenu1.MenuItems.Clear()
contextMenu1.MenuItems.Clear();
contextMenu1->MenuItems->Clear();
こちらも参照ください
GitHub で Microsoft と共同作業する
このコンテンツのソースは GitHub にあります。そこで、issue や pull request を作成および確認することもできます。 詳細については、共同作成者ガイドを参照してください。
.NET Desktop feedback