I'm using this code to add an item to the code window right click menu:
public void OnConnection(
object application,
ext_ConnectMode connectMode,
object addInInst,
ref Array custom)
{
_applicationObject = (DTE2)application;
_addInInstance = (AddIn)addInInst;
object[] contextGUIDS = new object[] { };
Command codeWindowCommand = null;
CommandBarControl codeWindowButton;
CommandBar codeCommandBar;
CommandBars commandBars;
try
{
codeWindowCommand = _applicationObject.Commands.Item(
_addInInstance.ProgID + "." + CODEWINDOW_COMMAND_NAME, 0);
}
catch
{
}
if (codeWindowCommand == null)
{
codeWindowCommand = _applicationObject.Commands.AddNamedCommand(
_addInInstance,
CODEWINDOW_COMMAND_NAME,
CODEWINDOW_COMMAND_NAME,
"Pastebin selected code",
true,
18,
ref contextGUIDS,
(int)vsCommandStatus.vsCommandStatusSupported +
(int)vsCommandStatus.vsCommandStatusEnabled);
}
commandBars = (CommandBars)_applicationObject.CommandBars;
codeCommandBar = commandBars["Code Window"];
codeWindowButton = (CommandBarControl)codeWindowCommand.AddControl(
codeCommandBar, codeCommandBar.Controls.Count + 1);
codeWindowButton.Caption = "Text for button";
codeWindowButton.TooltipText = "Tooltip for button";
}
and the addin is set to autostart. However each time the run VS2008 it adds another button to the menu until I totally delete the addin. Anyone know how I fix this?
I would for example wrap the Command.AddControl() and later stuff in an if that only executes if the button doesn't already exist, but I can't seem to find a way to check this in the API?