MenuCommand MenuCommand MenuCommand MenuCommand Class


Represents a Windows menu or toolbar command item.

public ref class MenuCommand
public class MenuCommand
type MenuCommand = class
Public Class MenuCommand


The following code example creates a MenuCommand object, configures its properties, and adds it to the IMenuCommandService.

Create an instance of the Component1 class on your form and open the form in a design environment like Visual Studio. Press the F1 key to invoke the MenuCommand.

#using <system.dll>
#using <>
#using <>

using namespace System;
using namespace System::ComponentModel;
using namespace System::ComponentModel::Design;
using namespace System::Security::Permissions;

namespace CppMenuCommand
   public ref class CDesigner: public ComponentDesigner
    [PermissionSetAttribute(SecurityAction::Demand, Name="FullTrust")]
      virtual void Initialize( IComponent^ comp ) override
         ComponentDesigner::Initialize( comp );
         IMenuCommandService^ mcs = static_cast<IMenuCommandService^>(comp->Site->GetService( IMenuCommandService::typeid ));
         MenuCommand^ mc = gcnew MenuCommand( gcnew EventHandler( this, &CDesigner::OnF1Help ),StandardCommands::F1Help );
         mc->Enabled = true;
         mc->Visible = true;
         mc->Supported = true;
         mcs->AddCommand( mc );
         System::Windows::Forms::MessageBox::Show( "Initialize() has been invoked." );

      void OnF1Help( Object^ /*sender*/, EventArgs^ /*e*/ )
         System::Windows::Forms::MessageBox::Show( "F1Help has been invoked." );
using System;
using System.ComponentModel;
using System.ComponentModel.Design;

namespace CSMenuCommand
    public class Component1 : System.ComponentModel.Component
        private System.ComponentModel.Container components = null;

        public Component1(System.ComponentModel.IContainer container)

        public Component1()

        private void InitializeComponent()
            components = new System.ComponentModel.Container();

    [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")] 
    public class CDesigner : System.ComponentModel.Design.ComponentDesigner 
        public override void Initialize(IComponent comp) 

            IMenuCommandService mcs = (IMenuCommandService)comp.Site.
            MenuCommand mc = new MenuCommand(new EventHandler(OnF1Help), StandardCommands.F1Help);
            mc.Enabled = true;
            mc.Visible = true;
            mc.Supported = true;
            System.Windows.Forms.MessageBox.Show("Initialize() has been invoked.");

        private void OnF1Help(object sender, EventArgs e) 
            System.Windows.Forms.MessageBox.Show("F1Help has been invoked.");
Imports System.ComponentModel
Imports System.ComponentModel.Design

Namespace VbMenuCommand
    <Designer(GetType(CDesigner))> _
    Public Class Component1
        Inherits System.ComponentModel.Component
        Private components As System.ComponentModel.Container = Nothing

        Public Sub New(ByVal container As System.ComponentModel.IContainer)
        End Sub

        Public Sub New()
        End Sub

        Private Sub InitializeComponent()
        End Sub
    End Class

    <System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _
    Public Class CDesigner
        Inherits System.ComponentModel.Design.ComponentDesigner

        Public Overrides Sub Initialize(ByVal comp As IComponent)

            Dim mcs As IMenuCommandService = CType(comp.Site.GetService(GetType(IMenuCommandService)), IMenuCommandService)
            Dim mc As New MenuCommand(New EventHandler(AddressOf OnF1Help), StandardCommands.F1Help)
            mc.Enabled = True
            mc.Visible = True
            mc.Supported = True
            System.Windows.Forms.MessageBox.Show("Initialize() has been invoked.")
        End Sub

        Private Sub OnF1Help(ByVal sender As Object, ByVal e As EventArgs)
            System.Windows.Forms.MessageBox.Show("F1Help has been invoked.")
        End Sub
    End Class
End Namespace


The MenuCommand class represents information about a Windows menu or toolbar command. The IMenuCommandService interface allows you to add MenuCommand objects to the Visual Studio menu.

This class provides the following members:

  • An event-handler property to which you can attach an event handler for the command.

  • A CommandID property that uniquely identifies the command.

  • An Invoke method that executes the command.

  • An OnCommandChanged method that can be overridden to handle the event that occurs when a new command is selected.

  • Boolean flag states that indicate whether the command is Checked, Enabled, Supported, or Visible.

  • An OleStatus property that indicates the OLE command status code for the command.

  • An override for the ToString method.


MenuCommand(EventHandler, CommandID) MenuCommand(EventHandler, CommandID) MenuCommand(EventHandler, CommandID) MenuCommand(EventHandler, CommandID)

Initializes a new instance of the MenuCommand class.


Checked Checked Checked Checked

Gets or sets a value indicating whether this menu item is checked.

CommandID CommandID CommandID CommandID

Gets the CommandID associated with this menu command.

Enabled Enabled Enabled Enabled

Gets a value indicating whether this menu item is available.

OleStatus OleStatus OleStatus OleStatus

Gets the OLE command status code for this menu item.

Properties Properties Properties Properties

Gets the public properties associated with the MenuCommand.

Supported Supported Supported Supported

Gets or sets a value indicating whether this menu item is supported.

Visible Visible Visible Visible

Gets or sets a value indicating whether this menu item is visible.


Equals(Object) Equals(Object) Equals(Object) Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

Gets the Type of the current instance.

(Inherited from Object)
Invoke() Invoke() Invoke() Invoke()

Invokes the command.

Invoke(Object) Invoke(Object) Invoke(Object) Invoke(Object)

Invokes the command with the given parameter.

MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
OnCommandChanged(EventArgs) OnCommandChanged(EventArgs) OnCommandChanged(EventArgs) OnCommandChanged(EventArgs)

Raises the CommandChanged event.

ToString() ToString() ToString() ToString()

Returns a string representation of this menu command.


CommandChanged CommandChanged CommandChanged CommandChanged

Occurs when the menu command changes.


for full access to system resources. Demand values: LinkDemand, InheritanceDemand. Associated state:

Applies to

See also