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