Export (0) Print
Expand All

MenuCommand Class

Represents a Windows menu or toolbar command item.

System.Object
  System.ComponentModel.Design.MenuCommand
    System.ComponentModel.Design.DesignerVerb

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

[ComVisibleAttribute(true)]
[PermissionSetAttribute(SecurityAction.InheritanceDemand, Name = "FullTrust")]
[PermissionSetAttribute(SecurityAction.LinkDemand, Name = "FullTrust")]
[HostProtectionAttribute(SecurityAction.LinkDemand, SharedState = true)]
public class MenuCommand

The MenuCommand type exposes the following members.

  NameDescription
Public methodMenuCommandInitializes a new instance of the MenuCommand class.
Top

  NameDescription
Public propertyCheckedGets or sets a value indicating whether this menu item is checked.
Public propertyCommandIDGets the CommandID associated with this menu command.
Public propertyEnabledGets a value indicating whether this menu item is available.
Public propertyOleStatusGets the OLE command status code for this menu item.
Public propertyPropertiesGets the public properties associated with the MenuCommand.
Public propertySupportedGets or sets a value indicating whether this menu item is supported.
Public propertyVisibleGets or sets a value indicating whether this menu item is visible.
Top

  NameDescription
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodInvoke()Invokes the command.
Public methodInvoke(Object)Invokes the command with the given parameter.
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Protected methodOnCommandChangedRaises the CommandChanged event.
Public methodToStringReturns a string representation of this menu command. (Overrides Object.ToString().)
Top

  NameDescription
Public eventCommandChangedOccurs when the menu command changes.
Top

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.

NoteNote

The HostProtectionAttribute attribute applied to this type or member has the following Resources property value: SharedState. The HostProtectionAttribute does not affect desktop applications (which are typically started by double-clicking an icon, typing a command, or entering a URL in a browser). For more information, see the HostProtectionAttribute class or SQL Server Programming and Host Protection Attributes.

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;
using System.ComponentModel;
using System.ComponentModel.Design;

namespace CSMenuCommand
{
    [Designer(typeof(CDesigner))]
    public class Component1 : System.ComponentModel.Component
    {
        private System.ComponentModel.Container components = null;

        public Component1(System.ComponentModel.IContainer container)
        {
            container.Add(this);
            InitializeComponent();
        }

        public Component1()
        {
            InitializeComponent();
        }

        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) 
        {
            base.Initialize(comp);

            IMenuCommandService mcs = (IMenuCommandService)comp.Site.
                        GetService(typeof(IMenuCommandService));
            MenuCommand mc = new MenuCommand(new EventHandler(OnF1Help), StandardCommands.F1Help);
            mc.Enabled = true;
            mc.Visible = true;
            mc.Supported = true;
            mcs.AddCommand(mc);
            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.");
        }
    }
} 

.NET Framework

Supported in: 4.5.2, 4.5.1, 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.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show:
© 2014 Microsoft