Unable to Hide Buttons in Custom Toolbar with UI Context in Visual Studio Extension

Vasu Bansal 20 Reputation points
2025-07-21T09:22:04.0166667+00:00

I have created a custom toolbar in my Visual Studio extension that appears only with specific files, using a defined UI context. I want to hide certain buttons in this toolbar programmatically. However, setting the .Hidden = true property does not hide the buttons. In my .vsct file, I have also added the DynamicVisibility flag to these buttons. While enabling and disabling the buttons works as expected, hiding them does not.

Questions:

Is this issue related to the way the toolbar is shown using a specific UI context?

What is the recommended approach to actually hide (not just disable) specific toolbar buttons in this scenario?

Any insights or guidance would be appreciated.

Developer technologies | Visual Studio | Extensions
0 comments No comments
{count} votes

6 answers

Sort by: Most helpful
  1. Susmitha T (INFOSYS LIMITED) 160 Reputation points Microsoft External Staff
    2025-07-22T08:32:12.7233333+00:00

    Thank you for reaching out. Please find the answer below.

     

    1. Command Flags: Ensure you have both DefaultInvisible and DynamicVisibility flags set for the buttons in your .vsct file. Without DefaultInvisible, the button may still show up initially. Ensure DynamicVisibility Is Set in .vsct

    <Button guid="yourGuid" id="yourCommandId" type="Button"> <CommandFlag>DynamicVisibility</CommandFlag> <Strings> <ButtonText>Your Button</ButtonText> </Strings> </Button>

    1. Visibility Constraints: If you want specific buttons to appear only under certain conditions, make sure you have the right conditions set in your <VisibilityConstraints> section. If they are always appearing, it might mean those conditions aren't being applied correctly.
    2. Use OleMenuCommand and BeforeQueryStatus

            4. Avoid. Hidden = true After Initialization

                               Instead, rely on cmd.Visible = false inside BeforeQueryStatus.

             5. UI Context: Verify that the UI context you're relying on (like UICONTEXT_SolutionHasSingleProject or UICONTEXT_SolutionHasMultipleProjects) is indeed active when you're trying to hide the buttons. You can check this by logging or debugging to see the current active context.

                 6.Dynamic Updates: Sometimes, Visual Studio may cache the visibility state of buttons. You might need to ensure that the menu and its commands are refreshed when you expect them to change.

     

    What is the recommended approach to actually hide (not just disable) specific toolbar buttons in this scenario?

    Answer: Implement BeforeQueryStatus handler properly. To hide/show commands dynamically, ensure the command has: DynamicVisibility flag in .vsct

    If issue still persist after following all the steps, kindly share the above information with us, we’ll be happy to assist further if needed.


  2. Susmitha T (INFOSYS LIMITED) 160 Reputation points Microsoft External Staff
    2025-07-23T08:37:03.23+00:00

    Thank you for reaching out. Please find the answer below.

    1. To dynamically hide/show commands, implement your command logic inside the BeforeQueryStatus handler. Instead of setting Hidden = true, use cmd.Visible = false to hide the button.

           2. To hide or show buttons in your custom Visual Studio toolbar programmatically—based on file type or UI context— you’ll need to use the OleMenuCommand and its BeforeQueryStatus event. Kindly find the below code.

     

     In your package initialization: 

    var commandId = new CommandID(GuidList.guidYourPackageCmdSet,PkgCmdIDList.cmdidYourCommand); var menuCommand = new OleMenuCommand(YourCallback, commandId); menuCommand.BeforeQueryStatus += (s, e) =>
    {   
      var cmd = s as OleMenuCommand;   
      if (cmd != null)  
       {       
      cmd.Visible = ShouldBeVisible(); // Your logic here    
    }
    };
    mcs.AddCommand(menuCommand);

     

    Regarding your question about the recommended approach to hide toolbar buttons, definitely focus on the BeforeQueryStatus implementation along with your visibility settings in the .vsct file.

     

    If you still experience issues after trying these suggestions, it would be helpful to know:

    • The exact setup of your .vsct file.
    • What conditions you're checking in your BeforeQueryStatus handler.
    • Any specific errors or unexpected behavior you're observing.

     

    If issue still persist after following all the steps, kindly share the above information with us, we’ll be happy to assist further if needed.


  3. Susmitha T (INFOSYS LIMITED) 160 Reputation points Microsoft External Staff
    2025-07-24T11:08:00.8333333+00:00

    Thank you for reaching out. Please find the answer below.

    This is possible using BeforeQueryStatus as a callback that runs each time the command is evaluated. You don’t call it manually — instead, you trigger it by calling. Invalidate () whenever the condition changes.

    Here's how to do it: Setup in .vsct:

    <CommandFlag>DynamicVisibility</CommandFlag>

    <CommandFlag>DefaultInvisible</CommandFlag>

    CommandID cmdId = new CommandID(GuidList.guidYourPackageCmdSet, PkgCmdIDList.cmdidYourCommand);

    OleMenuCommand _cmd = new OleMenuCommand(Execute, cmdId);

    _cmd.BeforeQueryStatus += (s, e) =>

    {

    var cmd = (OleMenuCommand)s;
    
    cmd.Visible = MyVisibilityCondition(); // runs on every invalidate
    

    };

    OleMenuCommandService mcs = GetService(typeof(IMenuCommandService)) as OleMenuCommandService;

    if (mcs != null)

    {

    mcs.AddCommand(_cmd);
    

    }

    Trigger the callback when condition changes: Wherever your condition changes (e.g., in a document changed event, active window change), call:

    _cmd.Invalidate(); // This makes the callback run and toggles visibility

    The BeforeQueryStatus is your callback. You don’t change visibility directly — just update your condition and call _cmd.Invalidate(). This gives the effect of auto toggle (hide/unhide)

    If issue still persist after following all the steps, we’ll be happy to assist further if needed.


  4. Susmitha T (INFOSYS LIMITED) 160 Reputation points Microsoft External Staff
    2025-07-25T14:52:30.8633333+00:00

    Thank you for reaching out. Please find the answer below.

    Thanks for pointing that out! You're correct—OleMenuCommandService does not have an InvalidateCommand() method,  and we apologize for the confusion.

    1. Use the BeforeQueryStatus event to set .Visible = true/false.
    2. Ensure the command is marked with **DynamicVisibility **in the **.vsct **file.
    3. Let Visual Studio call **BeforeQueryStatus **automatically when the UI context changes.

    If issue still persist after following all the steps, we’ll be happy to assist further if needed." Kindly mark the answer as accepted if the issue resolved".


  5. Susmitha T (INFOSYS LIMITED) 160 Reputation points Microsoft External Staff
    2025-07-29T11:27:06.78+00:00

    Thank you for reaching out. Please find the answer below.

     

    Since OleMenuCommandService does not expose InvalidateCommand(), and OleMenuCommand does not have an Invalidate () method, the correct way to force Visual Studio to re-evaluate the command is to manually call: _yourCommand.OnBeforeQueryStatus(EventArgs.Empty);

     

    1.Trigger Visibility Updates: Whenever your condition for visibility changes (like when the active document changes), you will need a way to trigger a refresh on the button's visibility. You can do this with the service: menuCommandService?.InvalidateCommand(commandId);

    2.Verify UI Context: Make sure the UI context you are depending on is active when you’re testing your button’s visibility. Use logging to check the current context if needed.

     

    If you still have issues after trying these steps, it will help to clarify a few things:

    1.What specific conditions are you using in your BeforeQueryStatus handler to determine visibility?
    2. Are there any error messages or unexpected behaviors that you notice when trying these approaches?
    3. Can you share the relevant parts of your .vsct file for further insights?

    If issue still persist after following all the steps, we’ll be happy to assist further if needed." Kindly mark the answer as accepted if the issue resolved".

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.