Share via


How to: Change the Appearance of Commands

You may want to provide feedback to your user by changing the appearance of a command. For example, you may want a command to look different when it is unavailable because of the current state of VSPackage. You can disable or enable commands, hide or show them, or check or uncheck them on the menu.

To change the appearance of a command, do one of the following:

  • Specify the appropriate flags in the command definition in the command table file.

  • Use the OleMenuCommandService service.

  • Implement the IOleCommandTarget interface and alter the raw command objects.

The following procedure shows how to find and update the appearance of a command by using the Managed Package Framework (MPF).

To change the appearance of a menu command

  • Obtain the command that you want to update from the OleMenuCommandService object and then set the appropriate properties on the command object. For example, the following method enables or disables the specified command from a VSPackage command set.

    [PrincipalPermission(SecurityAction.Demand)]
    public bool EnableMyCommand(int cmdID,bool fEnableCmd)
    {
        bool fCmdUpdated = false;
        var mcs = this.GetService(typeof(IMenuCommandService))
                as OleMenuCommandService;
        var newCmdID = new CommandID(GuidList.guidMenuTextCmdSet, cmdID);
        MenuCommand mc = mcs.FindCommand(newCmdID);
        if (mc != null)
        {
            mc.Enabled = fEnableCmd;
            fCmdUpdated = true;
        }
        return fCmdUpdated;
    }
    

See Also

Concepts

How VSPackages Add User Interface Elements to the IDE

Other Resources

Menus and Toolbars

Common Menu Tasks

Visual Studio Command Table (.Vsct) Files