Files
docs-desktop/dotnet-desktop-guide/framework/winforms/controls/add-and-remove-menu-items-with-wf-contextmenu-component.md
Andy De George c0ba284473 Initial winforms content migrated (#18)
* Merge winforms framework content to working branch (#14)

* Breadcrumb / TOC / Move net5 to net folder (#15)

* change path from net5 to net

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Add .net 5 winforms placeholder article (#16)

* added some metadata and adjusted net5 placeholder

* corrections

* corrections

* corrections

* Test1

* Swapping landing page vs concept

* fix links

* Fix links

* Fix desc
2020-09-01 16:26:21 -07:00

3.3 KiB

title, ms.date, dev_langs, helpviewer_keywords, ms.assetid
title ms.date dev_langs helpviewer_keywords ms.assetid
Add and Remove Menu Items with ContextMenu Component 03/30/2017
csharp
vb
cpp
context menus [Windows Forms], removing items
ContextMenu component [Windows Forms], adding items
shortcut menus [Windows Forms], removing items
shortcut menus [Windows Forms], examples
context menus [Windows Forms], adding items
shortcut menus [Windows Forms], adding items
ContextMenu component [Windows Forms], removing items
context menus [Windows Forms], examples
examples [Windows Forms], context menus
426d1eaf-7fb8-4b0b-8a33-5e8721786ea4

How to: Add and Remove Menu Items with the Windows Forms ContextMenu Component

Explains how to add and remove shortcut menu items in Windows Forms.

The Windows Forms xref:System.Windows.Forms.ContextMenu component provides a menu of frequently used commands that are relevant to the selected object. You can add items to the shortcut menu by adding xref:System.Windows.Forms.MenuItem objects to the xref:System.Windows.Forms.Menu.MenuItems%2A collection.

You can remove items from a shortcut menu permanently; however, at run time it may be more appropriate to hide or disable the items instead.

Important

Although xref:System.Windows.Forms.MenuStrip and xref:System.Windows.Forms.ContextMenuStrip replace and add functionality to the xref:System.Windows.Forms.MainMenu and xref:System.Windows.Forms.ContextMenu controls of previous versions, xref:System.Windows.Forms.MainMenu and xref:System.Windows.Forms.ContextMenu are retained for both backward compatibility and future use if you choose.

To remove items from a shortcut menu

  1. Use the xref:System.Windows.Forms.Menu.MenuItemCollection.Remove%2A or xref:System.Windows.Forms.Menu.MenuItemCollection.RemoveAt%2A method of the xref:System.Windows.Forms.Menu.MenuItems%2A collection of the xref:System.Windows.Forms.ContextMenu component to remove a particular menu item.

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

    -or-

  2. Use the Clear method of the MenuItems collection of the xref:System.Windows.Forms.ContextMenu component to remove all items from the menu.

    ContextMenu1.MenuItems.Clear()  
    
    contextMenu1.MenuItems.Clear();  
    
    contextMenu1->MenuItems->Clear();  
    

See also