This documentation is archived and is not being maintained.

How to: Change Text of Menus

[This topic is pre-release documentation and is subject to change in future releases. Blank topics are included as placeholders.]

The following procedure shows how to support changing the text of a menu command from managed code using the IMenuCommandService service.

Procedure

Change the Menu Label using IMenuCommandService

  1. In your .ctc file, add the TextChanges flag to your menu command. For example:

    BUTTONS_BEGIN
        guidMyCmdSet:cmdidMyCommand,      // command
            guidMyCmdSet:MyMenuGroup      // parent
            0x0100,                       // priority
            guidOfficeIcon:msotcidNoIcon, // icon (no icon in this case)
            BUTTON,                       // type
            TextOnly | TextChanges,       // Text only and it changes
            "My Text";
    BUTTONS_END
    

    Note that the command could also have been marked as IconAndText instead of TextOnly.

  2. In your VSPackage, create a new event handler that is called before the menu command is displayed. For example:

    class MyPackage : Package
    {
        public void OnBeforeQueryStatus(object sender, EventArgs e)
        {
            Microsoft.VisualStudio.Shell.OleMenuCommand myCommand;
            myCommand = sender as Microsoft.VisualStudio.Shell.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.

  3. When the VSPackage initializes, do the following:

    1. Query for the menu command service

    2. Create a new OleMenuCommand object representing your menu command

    3. Add the BeforeQueryStatus event handler, and

    4. Give the menu command to the menu command service.

      using System.ComponentModel.Design; // for IMenuCommandService, CommandID
      using Microsoft.VisualStudio.Shell; // For OleMenuCommandService, OleCommand
      
      class MyPackage : Package
      {
          protected override void Initialize()
          {
              OleMenuCommandService menuService;
              menuService = this.GetService(typeof(IMenuCommandService))
                            as OleMenuCommandService;
              if (null != menuService)
              {
                   CommandID commandId = new CommandID(guidMyCmdSet,cmdidMyCommand);
                   OleMenuCommand myCommand = new OleMenuCommand(new EventHandler(OnMyCommandHandler),
                                                                 commandId);
                   myCommand.BeforeQueryStatus += new EventHandler(OnBeforeQueryStatus);
                   menuService.AddCommand(myCommand);
              }
          }
      }
      

See Also

Other Resources

Common Menu Tasks

Show: