Export (0) Print
Expand All

IMenuCommandService Interface

Provides methods to manage the global designer verbs and menu commands available in design mode, and to show some types of context menus.

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

[Visual Basic]
<ComVisible(True)>
Public Interface IMenuCommandService
[C#]
[ComVisible(true)]
public interface IMenuCommandService
[C++]
[ComVisible(true)]
public __gc __interface IMenuCommandService
[JScript]
public
   ComVisible(true)
interface IMenuCommandService

Remarks

This interface provides methods to:

  • Find, invoke, add and remove global designer verb commands.
  • Find, invoke, add and remove standard menu commands.
  • Alter the event handlers associated with standard menu commands.
  • Display a context menu of standard commands that is associated with a menu CommandID.

Designer verbs represent custom-defined commands that are listed on the shortcut menu in design mode. A designer verb can provide a specified text label. Each designer verb is automatically assigned a unique CommandID. A designer can provide designer verbs through its Verbs property, but these are only available when the designer's component is currently selected. Global designer verbs are designer verb commands that can be accessed from a design-mode shortcut menu regardless of the selected component. This interface allows you to manage the set of global designer verbs that are available in design mode.

You can add a global designer verb using the AddVerb method, and you can remove a global designer verb using the RemoveVerb method. You can invoke a designer verb using the GlobalInvoke method if you know the CommandID of the verb. The Verbs property of this interface contains the current set of designer verb commands to display in a shortcut menu. This set of designer verb commands consists of all global designer verbs and any designer verbs offered by the designer of any currently selected component. This set of verbs is updated each time a component with a designer offering designer verb commands is selected or deselected.

Menu commands are limited to the set of predefined standard commands. Most of the predefined standard commands are defined in the StandardCommands and MenuCommands enumerations. You can add, remove, and invoke menu commands, and search for menu commands that have been added to a menu using methods of this interface.

You can add a standard menu command using the AddCommand method, and remove a standard menu command using the RemoveCommand method. You can attach an event handler to a predefined standard menu command by following the procedure detailed in the documentation for the AddCommand method. You can retrieve a menu command by CommandID if it has been added to a menu using the FindCommand method. You can invoke a menu command or designer verb command by CommandID using the GlobalInvoke method.

Note   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.
Note   A menu command can be added to a menu, and have its Visible or Enabled properties set to false. If you cannot visually locate a menu command that has been added on a menu, one of these properties may have been set to false.

You can show certain standard shortcut menus containing menu commands at a specified location using the ShowContextMenu method. The documentation for this method contains a table listing the command IDs that specify the valid menus to show.

Example

[Visual Basic, C#, C++] This example demonstrates using the IMenuCommandService to add a MenuCommand.

[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

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

Show:
© 2014 Microsoft