Amaranth Amaranth - 1 year ago 95
C# Question

hiding a button from a plugin toolbar

I am developping a plugin for Visual Studio in C#. The plugin has a settings page and a few buttons in a toolbar to call commands.

The problem is that in some circonstances, I want to hide a specific button. The best I was able to do is to disable the button.

Is it possible to dynamically change it's visibility?

EDIT: I wrote this question from my mobile, so maybe there is not enough details...

I create the toolbar in the .vsct file (I created the menu in the same file)

<Button guid="guidProductCmdSet" id="startCommand" priority="0x0100" type="Button">
<Parent guid="guidProductCmdSet" id="ToolbarGroup1" />
<Icon guid="Icons" id="startIcon" />

When the extension initializes, I create the commands:

var mcs = GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
if (null != mcs)
_startCommandId = new CommandID(GuidList.guidProducyVSICmdSet, (int)pkgCmdIDList.startCommand);
var startItem = new MenuCommand(StartProcess, _startCommandId);

After, I am able to disable some buttons from the toolbar like this:

var mcs = GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
var mc = mcs.FindCommand(commandId);
if (mc != null)
mc.Enabled = false;

I tried mc.Visible = false, but it does nothing.

Answer Source

Apparently, this works...

var commandBars = ((CommandBars)_dte2.CommandBars);
if (commandBars == null)
var commandBar = commandBars["MyPluginProductName"];
if (commandBar == null)
var startButton = commandBar.Controls["startCommand"];
if (startButton == null)
startButton.Visible = false;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download