Click here to Skip to main content
15,940,173 members
Articles / Programming Languages / C#
Article

Collapse All Add-in for all Visual Studio Projects

Rate me:
Please Sign up or sign in to vote.
3.75/5 (4 votes)
4 Aug 2011CPOL3 min read 36.1K   743   9   6
To Collapse All the Open Projects in Visual Studio IDE 2005, 2008 or 2010 using C#

Introduction

When we open up a Visual Studio solution which contains ample projects, usually all the projects will be in expanded mode. There is no direct tool menu supported in the Visual Studio IDE to collapse all the expanded projects. So I thought of creating a Visual Studio plug-in which serves the purpose. On rummaging around, I could find so many macros that support the functionality that I mentioned but I was unable to find a pure C# code that can be used to implement the Collapse All functionality in .NET. So I thought of creating one of my own using C#.

The below mentioned are the various steps for developing the Add-in.

Image 1

Figure 1 - Creating a Visual Studio Add-in Project

Once you have created a Visual Studio Add-in Project, create a class which is extended from IDTExtensibility2 and IDTCommandTarget. The main objects that you have to use are the DTE2 and AddIn. You need to add reference binaries in order to include EnvDTE and EnvDTE80.

Image 2

I have used the below two functions to implement the core Collapse all functionality.

C#
/// <summary>
      /// Command to Collapse All Nodes.
      /// </summary>
      private void DoCollapse()
/// <summary>
      /// To Collapse all the nodes in the tree 
      /// </summary>
      /// <param name="item"></param>
      /// <param name="solutionExplorer"></param>
      private void Collapse(UIHierarchyItem item,ref UIHierarchy solutionExplorer)

The DoCollapse function is called from the Exec function of IDTCommandTarget interface.

C#
/// <summary>Implements the Exec method of the IDTCommandTarget interface. 
/// This is called when the command is invoked.</summary>
/// <param term='commandName'>The name of the command to execute.</param>
/// <param term='executeOption'>Describes how the command should be run.</param>
/// <param term='varIn'>Parameters passed from the caller to the command handler.</param>
/// <param term='varOut'>Parameters passed from the command handler to the caller.</param>
/// <param term='handled'>Informs the caller if the command was handled or not.</param>
/// <seealso class='Exec' />
public void Exec(string commandName, vsCommandExecOption executeOption, 
	ref object varIn, ref object varOut, ref bool handled)

Add Name to your Tool bar Menu

You can add your own name in the tool bar by changing the argument in the AddNamedCommand2. In the stated example, I have given the name of the tool bar as “CollapseAll_SNK“.

Add an Icon to the Tool bar Menu

In void OnConnection() function in commands.AddNamedCommand2() function, I have used 6743 as the 5th Argument for function AddNamedCommand2. Have you noticed the Red Star in the CollaspeAll_SNK menu, 6743 is the index value for the red star. The default index value is 59 the icon is a smiley-face. To change to a different standard icon, change this index number with range 59 to 6743.

If you cannot find an appropriate icon in the Microsoft.VisualStudio.CommandBars library, you can use a custom bitmap for the add-in's command icon. The bitmap is contained as a resource in a satellite DLL. For more information, see How to: Display a Custom Icon on the Add-in Button. After creating the satellite DLL resource, you then point AddNamedCommand2 to the custom icon.

Image 3

Figure 2 - Collapse All Tool Menu in VS2010

Image 4

Figure 3 - Collapse All Tool Menu in VS2008 and VS2005

About Collapse All Add in Installer

I have also created an Add Installer for the above created Add-in. Using this installer, you can install the Collapse All add-in to your machine by selecting which Visual Studio you need to have the add-in attached. After installation, you should close your Visual Studio if it was already open to reflect the changes.

Image 5

Figure 4 - Collapse All Add-in Installer UI

Included Binaries

  • CollapseAll.dll
  • CollapseAll_SNK.AddIn
  • CollaspeAll_AddinInstaller.exe

Conclusion

Hope this article will give you a general idea on how to go about creating a Visual Studio Add-in and more over the primary thing is to get the created Collapse All Add-in for your Visual Studio.

References

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionscripting for installer Pin
kiquenet.com8-Aug-11 23:07
professionalkiquenet.com8-Aug-11 23:07 
QuestionBetter screenshots needed Pin
tlhIn`toq8-Aug-11 11:10
tlhIn`toq8-Aug-11 11:10 
QuestionThis isn't enough Pin
Pete O'Hanlon5-Aug-11 0:43
mvePete O'Hanlon5-Aug-11 0:43 
AnswerRe: This isn't enough Pin
Sreekesh NK5-Aug-11 4:15
Sreekesh NK5-Aug-11 4:15 
QuestionCollapse All Project and Folders? Pin
cdkisa4-Aug-11 12:14
cdkisa4-Aug-11 12:14 
AnswerRe: Collapse All Project and Folders? Pin
Sreekesh NK5-Aug-11 4:09
Sreekesh NK5-Aug-11 4:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.