MenuCommand Class

 

Represents a Windows menu or toolbar command item.

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

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

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

NameDescription
System_CAPS_pubmethodMenuCommand(EventHandler, CommandID)

Initializes a new instance of the MenuCommand class.

NameDescription
System_CAPS_pubpropertyChecked

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

System_CAPS_pubpropertyCommandID

Gets the CommandID associated with this menu command.

System_CAPS_pubpropertyEnabled

Gets a value indicating whether this menu item is available.

System_CAPS_pubpropertyOleStatus

Gets the OLE command status code for this menu item.

System_CAPS_pubpropertyProperties

Gets the public properties associated with the MenuCommand.

System_CAPS_pubpropertySupported

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

System_CAPS_pubpropertyVisible

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

NameDescription
System_CAPS_pubmethodEquals(Object)

Determines whether the specified object is equal to the current object.(Inherited from Object.)

System_CAPS_protmethodFinalize()

Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.)

System_CAPS_pubmethodGetHashCode()

Serves as the default hash function. (Inherited from Object.)

System_CAPS_pubmethodGetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_pubmethodInvoke()

Invokes the command.

System_CAPS_pubmethodInvoke(Object)

Invokes the command with the given parameter.

System_CAPS_protmethodMemberwiseClone()

Creates a shallow copy of the current Object.(Inherited from Object.)

System_CAPS_protmethodOnCommandChanged(EventArgs)

Raises the CommandChanged event.

System_CAPS_pubmethodToString()

Returns a string representation of this menu command.(Overrides Object.ToString().)

NameDescription
System_CAPS_pubeventCommandChanged

Occurs when the menu command changes.

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.

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.");
        }
    }
} 

NamedPermissionSet

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

.NET Framework
Available since 1.1

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

Return to top
Show: