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.
To change a menu command label by using IMenuCommandService
Create a VSPackage named, for example, MenuText. When the VSPackage wizard runs, select Menu Command and accept all defaults.
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>
In the VSPackage, create an event handler to be called before the menu command is displayed.
Add code that performs the following actions when the VSPackage is initialized.
Query for the menu command service.
Create a OleMenuCommand object that represents the menu command.
Add the BeforeQueryStatus event handler.
Give the menu command to the menu command service. You must change MenuCommand to OleMenuCommand, as in the following example.
// 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.