Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Collapse All Add-in for all Visual Studio Projects

0.00/5 (No votes)
4 Aug 2011 1  
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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here