Amaranth Amaranth - 1 month ago 8
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" />
<Strings>
<CommandName>startCommand</CommandName>
<ButtonText>Start</ButtonText>
</Strings>
</Button>


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);
mcs.AddCommand(startItem);
}


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

Apparently, this works...

var commandBars = ((CommandBars)_dte2.CommandBars);
if (commandBars == null)
{
    return;
}
var commandBar = commandBars["MyPluginProductName"];
if (commandBar == null)
{
    return;
}
var startButton = commandBar.Controls["startCommand"];
if (startButton == null)
{
    return;
}
startButton.Visible = false;
Comments