Share via


How to: Change the Text of a Menu Command

The following steps show how to change the text label of a menu command by using managed code and the IMenuCommandService service.

Procedure

To change a menu command label by using IMenuCommandService

  1. Create a VSPackage named, for example, MenuText. When the VSPackage wizard runs, select Menu Command and accept all defaults.

  2. In the .vstc file, add the TextChanges flag to your menu command, as shown in the following example.

    <Button guid="guidMenuTextCmdSet" id="cmdidMyCommand" priority="0x0100" type="Button">
      <Parent guid="guidMenuTextCmdSet" id="MyMenuGroup" />
      <Icon guid="guidImages" id="bmpPic1" />
      <CommandFlag>TextChanges</CommandFlag>
      <Strings>
        <CommandName>cmdidMyCommand</CommandName>
        <ButtonText>My Command name</ButtonText>
      </Strings>
    </Button>
    
  3. In the VSPackage, create an event handler to be called before the menu command is displayed.

    Private Sub OnBeforeQueryStatus(ByVal sender As Object, ByVal e As EventArgs)
        Dim myCommand As OleMenuCommand = TryCast(sender, OleMenuCommand)
        If myCommand IsNot Nothing Then
            myCommand.Text = "New Text" 
        End If 
    End Sub
    
    private void OnBeforeQueryStatus(object sender, EventArgs e)
    {
        var myCommand = sender as OleMenuCommand;
        if (null != myCommand)
        {
            myCommand.Text = "New Text";
        }
    }
    

    You can also update the status of the menu command in this method by changing the Visible, Checked, and Enabled properties on the OleMenuCommand object.

  4. Add code that performs the following actions when the VSPackage is initialized.

    1. Query for the menu command service.

    2. Create a OleMenuCommand object that represents the menu command.

    3. Add the BeforeQueryStatus event handler.

    4. Give the menu command to the menu command service. You must change MenuCommand to OleMenuCommand, as in the following example.

    Dim menuCommandID As CommandID = New CommandID(GuidList.guidMenuTextCmdSet, CInt(PkgCmdIDList.cmdidMyTextCommand))
    Dim menuItem As OleMenuCommand = New OleMenuCommand(New EventHandler(AddressOf MenuItemCallback), menuCommandID)
    AddHandler menuItem.BeforeQueryStatus, AddressOf OnBeforeQueryStatus
    mcs.AddCommand(menuItem)
    
    // Create the command for the menu item.
    CommandID menuCommandID = new CommandID(GuidList.guidMenuTextCmdSet, (int)PkgCmdIDList.cmdidMyCommand);
    OleMenuCommand menuItem = new OleMenuCommand(MenuItemCallback, menuCommandID );
    menuItem.BeforeQueryStatus +=
        new EventHandler(OnBeforeQueryStatus);
    mcs.AddCommand(menuItem);
    

    When you use the package template to create a menu command, it adds the required code automatically. However, it declares the menu command as a MenuCommand object and it does not add the BeforeQueryStatus handler. Therefore, you must change the menu command declaration to use OleMenuCommand and add the handler on the next line.

    The package file must include the System.ComponentModel.Design and Microsoft.VisualStudio.Shell namespaces to access the IMenuCommandService interface and the OleMenuCommand object.

See Also

Other Resources

Common Tasks with User Interface Elements