Export (0) Print
Expand All

IMenuCommandService.AddCommand Method

Adds the specified standard menu command to the menu.

Namespace:  System.ComponentModel.Design
Assembly:  System (in System.dll)

void AddCommand(
	MenuCommand command


Type: System.ComponentModel.Design.MenuCommand

The MenuCommand to add.


The CommandID of the specified MenuCommand is already present on a menu.

The MenuCommands and StandardCommands enumerations contain CommandID identifiers for predefined standard commands and menu commands that are already associated with a location on a particular menu.


An attempt to add a menu command with an already existing CommandID will throw an InvalidOperationException. When adding a menu command, be sure to check that it is not already on a menu using the FindCommand method, or use exception handling wisely.

You can attach an event handler to a predefined menu command with the following procedure:

  1. If the menu command to link with an event handler has been added to a menu, or is located with the FindCommand method, consider whether you wish to restore this menu command later, and whether you want to invoke the current event handler from your new event handler. If your code might restore the menu command or chain the invocation of event handlers, be sure to store a reference to the menu command somewhere.

  2. Ensure that any previously existing menu command with the same CommandID has been removed from the menu using the RemoveCommand method.

  3. Create a new MenuCommand and specify your event handler in the constructor, along with a CommandID representing the command to add. Each standard menu command is associated with a predefined menu location where it is added to. The supported command IDs are defined in the StandardCommands and MenuCommands enumerations.

  4. If you want to invoke any preexisting event handler for the menu command, call the Invoke method of the menu command you have replaced on the menu from the event handler that handles the invoke event of your menu command.

  5. If you are interested in restoring the event handler of a preexisting menu command that you have replaced, add the stored, preexisting menu command after you remove the replacement menu command you created. You may want to add this behavior to the Dispose method for your type.

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

© 2014 Microsoft