Click here to Skip to main content
Licence CPOL
First Posted 4 Aug 2011
Views 7,083
Downloads 266
Bookmarked 7 times

Collapse All Add-in for all Visual Studio Projects

By | 4 Aug 2011 | Article
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.

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.

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

/// <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.

/// <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.

Figure 2 - Collapse All Tool Menu in VS2010

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.

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)

About the Author

Sreekesh NK

Technical Lead

India India

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Questionscripting for installer Pinmemberalhambra-eidos23:07 8 Aug '11  
QuestionBetter screenshots needed Pinmembertlhintoq11:10 8 Aug '11  
QuestionThis isn't enough PinprotectorPete O'Hanlon0:43 5 Aug '11  
AnswerRe: This isn't enough PinmemberSreekesh NK4:15 5 Aug '11  
QuestionCollapse All Project and Folders? Pinmembercdkisa12:14 4 Aug '11  
AnswerRe: Collapse All Project and Folders? PinmemberSreekesh NK4:09 5 Aug '11  

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

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 4 Aug 2011
Article Copyright 2011 by Sreekesh NK
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid