Export (0) Print
Expand All

MenuCommand Class

Represents a Windows menu or toolbar command item.

For a list of all members of this type, see MenuCommand Members.

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

[Visual Basic]
<ComVisible(True)>
Public Class MenuCommand
[C#]
[ComVisible(true)]
public class MenuCommand
[C++]
[ComVisible(true)]
public __gc class MenuCommand
[JScript]
public
   ComVisible(true)
class MenuCommand

Thread Safety

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

Remarks

MenuCommand represents information about a Windows menu or toolbar command. The IMenuCommandService interface allows you to add MenuCommand objects to the Visual Studio .NET 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 ToString.

Example

[Visual Basic, C#, C++] This example creates a MenuCommand object, configures its properties, and adds it to a IMenuCommandService object.

[Visual Basic] 
' This example illustrates how to add a command of type StandardCommand to a
'   service of type IMenuCommandService.  It defines a class that is a designer
'   to a component, called CDesigner.  Then it creates a MenuCommand object using
'   one of the commands in the StandardCommands class.  Finally, it sets several
'   of the properties in the MenuCommand object and adds the MenuCommand object
'   to the MenuCommandService object.

Public Class CDesigner
   Inherits System.ComponentModel.Design.ComponentDesigner

   Public Overrides Sub Initialize(ByVal comp As IComponent)
      MyBase.Initialize(comp)

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

   Private Sub OnReplace(ByVal sender As Object, ByVal e As EventArgs)
      System.Windows.Forms.MessageBox.Show("Replace() has been invoked.")
   End Sub 'OnReplace
End Class 'CDesigner

[C#] 
/* This example illustrates how to add a command of type StandardCommand to a
    service of type IMenuCommandService.  It defines a class that is a designer
    to a component, called CDesigner.  Then it creates a MenuCommand object using
    one of the commands in the StandardCommands class.  Finally, it sets several
    of the properties in the MenuCommand object and adds the MenuCommand object
    to the MenuCommandService object.
*/

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(OnReplace),StandardCommands.Group);
        mc.Enabled = true;
        mc.Visible = true;
        mc.Supported = true;
        mcs.AddCommand(mc);
        System.Windows.Forms.MessageBox.Show("Initialize() has been invoked.");
    }

    private void OnReplace(object sender, EventArgs e) 
    {
        System.Windows.Forms.MessageBox.Show("Replace() has been invoked.");
    }
} // End Class Component1

[C++] 
/* This example illustrates how to add a command of type StandardCommand to a
    service of type IMenuCommandService.  It defines a class that is a designer
    to a component, called CDesigner.  Then it creates a MenuCommand object using
    one of the commands in the StandardCommands class.  Finally, it sets several
    of the properties in the MenuCommand object and adds the MenuCommand object
    to the MenuCommandService object.
*/

public __gc class CDesigner : public ComponentDesigner {
public:

    void Initialize(IComponent* comp)
    {
        ComponentDesigner::Initialize(comp);

        IMenuCommandService* mcs =
            static_cast<IMenuCommandService*>(comp->Site->GetService(__typeof(IMenuCommandService)));
        MenuCommand* mc = new MenuCommand(new EventHandler(this, &CDesigner::OnReplace), StandardCommands::Group);
        mc->Enabled = true;
        mc->Visible = true;
        mc->Supported = true;
        mcs->AddCommand(mc);
        System::Windows::Forms::MessageBox::Show(S"Initialize() has been invoked.");
    }

private:

    void OnReplace(Object* /*sender*/, EventArgs* /*e*/)
    {
        System::Windows::Forms::MessageBox::Show(S"Replace() has been invoked.");
    }
}; // End Class CDesigner

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.ComponentModel.Design

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

Assembly: System (in System.dll)

See Also

MenuCommand Members | System.ComponentModel.Design Namespace | StandardCommands | IMenuCommandService | CommandID

Show:
© 2014 Microsoft