Click here to Skip to main content
Click here to Skip to main content

Collapse All Add-in for all Visual Studio Projects

By , 4 Aug 2011
 

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.

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

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

/// <span class="code-SummaryComment"><summary>Implements the Exec method of the IDTCommandTarget interface. 
</span>/// This is called when the command is invoked.<span class="code-SummaryComment"></summary>
</span>/// <span class="code-SummaryComment"><param term='commandName'>The name of the command to execute.</param>
</span>/// <span class="code-SummaryComment"><param term='executeOption'>Describes how the command should be run.</param>
</span>/// <span class="code-SummaryComment"><param term='varIn'>Parameters passed from the caller to the command handler.</param>
</span>/// <span class="code-SummaryComment"><param term='varOut'>Parameters passed from the command handler to the caller.</param>
</span>/// <span class="code-SummaryComment"><param term='handled'>Informs the caller if the command was handled or not.</param>
</span>/// <span class="code-SummaryComment"><seealso class='Exec' />
</span>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
No Biography provided

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

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Questionscripting for installermemberalhambra-eidos8-Aug-11 23:07 
QuestionBetter screenshots neededmembertlhintoq8-Aug-11 11:10 
QuestionThis isn't enoughprotectorPete O'Hanlon5-Aug-11 0:43 
AnswerRe: This isn't enoughmemberSreekesh NK5-Aug-11 4:15 
QuestionCollapse All Project and Folders?membercdkisa4-Aug-11 12:14 
AnswerRe: Collapse All Project and Folders?memberSreekesh NK5-Aug-11 4:09 

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

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