UPDATE 3: The project is available on Google Code
UPDATE 2: Now solution folders and some other stuff are done, thanks to Huseyin Tufekcilerli
UPDATE: I updated the dlls and the source in the link, now it doesn't crash VS. Hopefully the only problem left is that solutions with multiple solution folders won't be reflected in Projects tab.
I have spent a couple of days on implementing a fast add reference dialog box for Visual Studio(with some help from an existing addin). The time of adding a reference has been a great time to have some coffee, to have lunch, or even a way to save economy (remember, developer time costs money!). If you don’t believe me, take a look at what tweeps say.
It is actually easy once you find some of the extension points of Visual Studio.
In my case, I had to implement IDTExtensibility2 which contains method signatures to be called when a plugin is loaded, unloaded etc.
As first step, I had added “Add Quick Reference” item to the context menu when a project is right clicked. This is the most tricky part as there is almost no documentation on that.
The method that we should implement for this is:
public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
The type of the application parameter implements DTE2 interface and the addInInst parameter implements AddIn interface, which has all we need.
The DTE2 interface has CommandBars property which gives us access to various VS stuff. The matter is to find the right one. After for-looping all CommandBar item, i found that the place I should add my custom item is “Context Menus”->”Project and Solution Context Menus”->”Reference Root”. Man this is hard to locate! Looking forward to MEF in 2010!
applicationObject = (DTE2)application;
addInInstance = (AddIn)addInInst;
CommandBars cb = applicationObject.CommandBars as CommandBars;
CommandBar bar = cb["Context Menus"];
CommandBarPopup cbarControl = bar.Controls["Project and Solution Context Menus"] as CommandBarPopup;
var commandBarControls = cbarControl.Controls;
this.referenceRoots = commandBarControls["Reference Root"] as CommandBarPopup;button = this.referenceRoots.Controls.Add(MsoControlType.msoControlButton,
System.Reflection.Missing.Value,
System.Reflection.Missing.Value, 1, true) as CommandBarButton;
button.Caption = "Add Quick Reference";
button.Click += oControl_Click;
There is one thing to be careful about: If you want to handle events of a button, for example, you should hold a reference in your class. Local method variables wouldn’t work. This situation is better told here
Now, I am done. I should now design the dialog itself that looks very similar to the original one. There is another problem: There is no "Windows Explorer Like” control for Windows Forms. There is OpenFileDialog but it is a dialog, not a control. I found the most similar one at GongShell Project which is licensed under GPL.
My current screen looks like the one below:
Very similar, even if I say so myself.
One thing to notice here is that the first tab is browse(.NET in original dialog), second is .net (Browse in original dialog) and third one is Projects. I haven’t written “Recent” part as I don’t want to deal with I/O really.
I believe that Browse window is more frequently used than .NET tab. There can be more improvements on that screen, such as having “common” tab which does the copying of commonly used references to a new project but hey this is a demonstration only ! :)
The items in the .NET tab are loaded in the background, and once it is loaded, it will be cached during the lifetime of the application.
I won’t comment more on the code, go grab it and try it. I wont continue developing this little addin, so you are free to do it on your own. Just drop me an email when you do it, though.
I just warn you: Com stuff is like walking on a mine field, and I am not taking any responsibility in case you loose data.
Download the code and the binaries here and put the binaries into Documents\Visual Studio 2008\Addins folder. Have fun!