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-29T11:32:23.9466667+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".


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.