This documentation is archived and is not being maintained.

How to: Change the Appearance of Commands

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

Sometimes it is desirable to provide feedback to your user via a change in the appearance of a command. One example where you would change the appearance of a command is when the command is not available due to the current state of the data or what the VSPackage is currently doing.

Inform your user about the availability of the commands offered by your package by disabling or enabling, hiding or showing, or checking or unchecking a menu item in the following ways:

  • By specifying the appropriate flags in the command's definition in the ctc file.

  • By using the OleMenuCommandService service.

  • By implementing the IOleCommandTarget interface and operating on the raw command objects.

The following procedure shows how to find and update the enabled state of a command from managed code using the managed package framework (MPF).

To change the appearance of a menu command

  • At some point in the execution of your program, you may need to change the appearance of a menu command, for example, to enable or disable the command. Obtain the menu command that is to be changed from the OleMenuCommandService object and set the appropriate properties on the menu command object. For example, the following method in managed code enables or disables the specified command from the VSPackage's own command set.

    using System.ComponentModel.Design; // for IMenuCommandService, MenuCommand, CommandID
    using Microsoft.VisualStudio.Shell; // For OleMenuCommandService
    
    namespace MyPackageNamespace
    {
        class MyPackage : Package
        {
            public bool EnableMyCommand(int cmdID,bool fEnableCmd)
            {
                bool fCmdUpdated = false;
                OleMenuCommandService mcs = this.GetService(typeof(IMenuCommandService))
                        as OleMenuCommandService;
                CommandID newCmdID = new CommandID(GuidList.guidPackageCmdSet, cmdID);
                MenuCommand mc = mcs.FindCommand(newCmdID);
                if (mc != null)
                {
                    mc.Enabled = fEnableCmd;
                    fCmdUpdated = true;
                }
                return fCmdUpdated;
            }
        }
    }
    

See Also

Show: