Short post this one, but its something I needed to do during the course of developing a Visual Studio Add-In, so thought I’d note it down.

Along the way there were several test add-ins I developed as proofs-of-concept and to demonstrate that items could be added to different menus within the IDE. However, the Visual Studio IDE itself doesn’t appear to offer any easy way of removing add-ins once they have been integrated.

This is especially annoying when the IDE appears to lock the DLL for the particular plugin you are currently working on, preventing you from compiling any changes.

The ‘Add-in Manager’ (under Tools–>Add-in Manager) isn’t all that effective, and certainly doesn’t remove it. So what can we do?

Fortunately, there is an MSDN article to hand that explains exactly how to do this: How to: Deactivate and Remove an Add-In.

The steps (taken from this article) are as follows:

  1. Delete the .addin XML registration file for the add-in that you want to remove.

    The default location is ..\Users\username\ Documents\Visual Studio 2010\Addins\.

  2. At a Visual Studio command prompt, type devenv /resetaddin Namespace.ClassName, where Namespace is the name of your add-in project and Classname is its class name, for example, devenv /resetaddin MyAddin1.Connect.

Although this is very simple, it baffles me as to why there is not a simple option available in Visual Studio to do this.

Previous - Visual Studio Add Ins – Changing The Default Icon

, , ,

This is the second of a series of posts on Visual Studio Add-Ins that I’m writing to record my experiences on creating a plugin for Visual Studio.

Previously, we discussed Adding Items to Context Menus. On this occasion we are looking at how to customise the icon that is displayed.

VS Plugin Default Icon

As you can see from the above image, the default icon is an annoying little smiley face. When you search through the project, you find no reference to this icon, so you are left wondering how to change it.

Well, if you look at the OnConnection method in the Connect class you will see a call to AddNamedCommand2 located within the try/catch block.

The fifth and sixth parameters here are true and 59 by default. This stipulate that we are to use Windows default command bar button faces (true), and that we should use the 59th one (which happens to be a smiley face). Try changing this – you’ll get a different one.

Viewing all these button faces isn’t particularly easy. The MSDN Article Listing Button Faces in the Command Bar for the Microsoft Office System goes into a pretty lengthy explanation on how you might do this. However, we’re just interested in how we’re going to do this for ourselves.

Fortunately, there is another MSDN article How to: Display a Custom Icon on the Add-In Button that details just how to do this. There is little sense in reproducing this information here, so I suggest you follow the steps from the article. 

Tip: Read them all before you start the first one. Some of the steps don’t make any sense until later.

Previous -  Visual Studio Add-Ins – Adding Items to Context Menus.

Next -  Visual Studio Addins – Deactivate and Remove an Add-In.

, , ,

This is the first of a series of posts on Visual Studio Add-Ins, that I’m writing to record my experiences on creating a plugin for Visual Studio.

When developing a Visual Studio 2010 addin recently, I was wanting to add an item to the ‘Tools’ menu and to the context menu for the code-editor window.

This article is working from the assumption that you have already created the appropriate Visual Studio AddIns project. If not, you can do so by clicking File -> New Project -> Other Project Types -> Extensibility -> Visual Studio Add-in, and following the setup wizard.

The project you are given should add the menu items to the ‘Tools’ menu by default.

public void OnConnection(....
{
.....
    CommandBar menuBarCmds = ((CommandBars)_applicationObject.CommandBars)["MenuBar"];
    CommandBarControl toolsControl = menuBarCmds.Controls["Tools"];
    CommandBarPopup toolsPopup = (CommandBarPopup)toolsControl;
.....
    Command command = commands.Aon ddNamedCommand2(_addInInstance, ....
.....
    command.AddControl(toolsPopup.CommandBar, 1);

And to execute, we add our desired code to the Exec method later:

public void Exec(....
{
    ....
    if(commandName == "MyAddin1.Connect.MyAddin1") 
    {
        //your code here
    }

The code above adds a new option to the ‘Tools’ menu under ‘Menu Bar’. We are wanting to add specifically to the code editor context menu but are not sure which one it is.

I tracked this down rather unscientifically using the following process.

Firstly, add the following code to the OnConnection method:

CommandBars AllCommandBars = (CommandBars)_applicationObject.CommandBars;

We really want to inspect the contents of this object, and there is no better place than in debug mode. So, place a breakpoint upon this line and launch the app.

When the debugger hits the breakpoint you can step over the line (F10) and then inspect the contents:

As we can see there are 465 possible menus that we could add our item to. Which one? Well, it’s not “Code Window” as I originally anticipated. So how do we find this?

You may have a better method, but the way I found it was by trial and error.

I added the new menu item to all (465) menu controls in visual studio with the following loop

foreach (CommandBar cc in cmdBars)
{
    if (cc.Index >= 1 && cc.Index <= 465)
    {
        command.AddControl(cmdBars[cc.NameLocal]);
    }
}

I then narrowed this using a divide and conquer technique by adjusting the bounds of the loop:

if (cc.Index >= 1 && cc.Index <= 256)
    ...
    if (cc.Index >= 1 && cc.Index <= 128)
    ...
    if (cc.Index >= 64 && cc.Index <= 128)
    ...etc...

Until I eventually found what I was looking for.

The item in question lay at position [88] in the menu collection and was named “Script Context”. This means that the item will only appear when you are editing what Visual Studio deems as script files (this includes JavaScript).

Of worthy note (amongst others) are these other types of editor file:

  • HTML Context
  • ASPX Context
  • ASPX Code Context
  • ASPX VB Code Context

And now, when I right-click on the source of my JavaScript file, I can see the new menu that I have added.

Next - Visual Studio Add Ins – Changing The Default Icon

, , ,