hiding a button from a plugin toolbar
Asked Answered
C

2

0

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.

Candlemaker answered 28/1, 2013 at 22:15 Comment(2)
have you tried the Visible property? Eg. button.Visible = false ?Subgenus
Yes, I did. The strange thing is that it does not work. Sorry for the lack of details on my first draft, I was on my mobile.Candlemaker
C
1

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;
Candlemaker answered 4/2, 2013 at 14:31 Comment(0)
P
1

Add

<CommandFlag>AllowVisibilityChangeOnToolBar</CommandFlag>

to your button.

Best Regards,

Konstantin S.

Proximity answered 21/10, 2016 at 8:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.