Core_F
Core_F

Reputation: 3442

VS2010 Add-In, adding a command to a context menu?

I know there are already some threads about this, but I just won't work for me.

What I want: I need a new entry in a context menu of the Visual Studio Source Control Explorer. For this I started a new Add-In Project.

What I used: I used this article as a guide. http://blogs.msdn.com/b/team_foundation/archive/2010/06/24/extending-work-item-tracking-context-menus.aspx

What is not working: I don't get any exceptions, the menu just won't show up, no matter where I add it.

Some code snippets:

public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
{
_applicationObject = (DTE2)application;
_addInInstance = (AddIn)addInInst;
if(connectMode == ext_ConnectMode.ext_cm_UISetup)
{
   AddCommandToContextMenu(
                         "Team Project", // context menu Name
                         "ClearQuery", // menu reference name
                         "Clear", // display name
                         47, // command icon
                         1);    // command placement, 1= first item on top
                }
}

I am using "Team Project" menu name for testing. VSIPLogging tells me, that this is the name of the menu if I make a right click on our TFS Team Project. I also tried other menus without success.

Here are the AddCommandToContextMenu functions:

private void AddCommandToContextMenu(string menuName, string commandName, string commandText, int iconId, int position)
    {

                    CommandBar contextMenu = ((CommandBars)_applicationObject.CommandBars)[menuName];

                    AddCommand(contextMenu, commandName, commandText, iconId, position);
    }



private void AddCommand(CommandBar parent, string commandName, string commandText, int iconId, int position)

    {
                     Commands2 commands = (Commands2)_applicationObject.Commands;
                    //create the command
                    Command newCommand = commands.AddNamedCommand2(_addInInstance, commandName, commandText, commandText, true, iconId);
                    // add it to parent menu
                    newCommand.AddControl(parent, position);
    }

The commandbar "parent" gives me quite some exceptions, if I take a closer look at it:

accChildCount = 'parent.accChildCount' threw an exception of type 'Microsoft.VisualStudio.PlatformUI.Automation.DeprecatedException'

The same for every other "acc" value.

Now I really don't know what I did wrong or what else I could try to make this work. All I want to do is to have a context menu entry in the source control explorer, which should call the power tools command line exe to call the "undo unchanged" function of it.

Upvotes: 3

Views: 2309

Answers (1)

Pavel Donchev
Pavel Donchev

Reputation: 1889

I am pretty sure the Popups in Visual Studio were of CommnadBarPopup type. The other thing I am pretty sure was that you need to make your commands / controls global so a reference is kept on them, otherwise GC will kill them.

You need to make sure that the command name in the AddCommand doesn't contain dots, and in the Query / Exec functions it does, e.g.:

newCommand = commands.AddNamedCommand2(_addInInstance, commandName, commandText, commandText, true, iconId, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled,(int)vsCommandStyle.vsCommandStylePictAndText,vsCommandControlType.vsCommandControlTypeButton);

Few things to note here:

  1. newCommand is not a local variable as in your code, it is promoted to a global variable to keep it alive (anyway, this is not your case probably, if this was the problem - you would see it the first time and then it will disappear).
  2. You ommited parameters, the ref ContextGUIDS here is a new object[] that was declared just before the method call to hold the guid for the command, it is not that important, just add it, what is important are the next parameters, the first one tells visual studio if the command is visible and enabled : (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled and the next give some hint on what your command should look like (button in our case).

This is just a start point, plaese refer to this article: HOWTO: Create a context menu using a Visual Studio commandbar popup from an add-in

Good luck!

Upvotes: 3

Related Questions