Click here to Skip to main content
15,879,613 members
Articles / Programming Languages / C#

Quick Open Add-in for Visual Studio

Rate me:
Please Sign up or sign in to vote.
3.85/5 (7 votes)
6 Mar 2006CPOL2 min read 49.5K   174   8   4
Step-by-step creation of a quick open add-in for Visual Studio 2005

Introduction

While working on a project, I don't like to use the mouse very often. But some tasks like opening a project file in Visual Studio need to use the mouse. So I decided to implement an add-in that doesn't need any mouse strokes (but is optional) and assigns a shortcut "Ctrl+Shift+Alt+N". Here is my quick open add-in and step by step instruction on how to write such an add-in for Visual Studio .NET 2005.

Using the Code

First of all, you should create a new Visual Studio .NET 2005 add-in project. File » New » Project. Select Other Project Types » Extensibility »Visual Studio Add-in from New Project dialog. Give a name to the project like QuickOpen and click OK.

Image 1

On Add-in wizard, select Create an add-in using Visual C# option, Microsoft Visual Studio 2005 as application host, type a name and description and the wizard will generate your add-in project.

Image 2

After your Add-in project is created, Visual Studio opens the Connect class to edit. Now we should create a named command so we can create a menuItem to allow users to interact with our add-in.

C#
Command command =
        commands.AddNamedCommand2(addInInstance,
                                  "QuickOpen",
                                  "QuickOpen",
                                  "Quick way to open files ",
                                  true,
                                  59,
                                  ref contextGUIDS,
                                  (int)vsCommandStatus.vsCommandStatusSupported+
                                  (int)vsCommandStatus.vsCommandStatusEnabled,
                                  (int)vsCommandStyle.vsCommandStylePictAndText,
                                  vsCommandControlType.vsCommandControlTypeButton);

You have your command and now you can add your command to open menu placed under the Main menu:

C#
CommandBarPopup filePopup = (CommandBarPopup)menuBarCommandBar.Controls["File"];
CommandBarPopup openPopup = (CommandBarPopup)filePopup.Controls["Open"];
command.AddControl(openPopup.CommandBar, 4);

Now we have successfully added our menu item under File » Open with the index 4.

When the user clicks Quick Open menu item, Visual Studio will call the Exec function that comes with the IDTCommandTarget interface. Note that, since you may have more than one command in your add-in, Visual Studio will call that method with the CmdName parameter that's the name of the command being called. We can optionally add keybindings for addin as follows :

C#
command.Bindings = "Global::Ctrl+Shift+Alt+N";

Here, Global indicates that the shortcut can be used anywhere within Visual Studio.

We've almost finished our add-in. What we have to do is to analyze our solution and display the list of files. You can enumerate DTE2.Solution.Projects and then ProjectItems of each project. If you also want to display pretty icons that inform the user about the type of file, you can find each ProjectItem's type with its Kind property. Kind property gives you a GUID string, so you should know which GUID belongs to which type (or you may use Constants class for common types).

C#
FileCodeModel fileCodeModel = applicationObject.ActiveDocument.ProjectItem.FileCodeModel;
foreach (CodeElement codeElement in fileCodeModel.CodeElements) {
   //Add codeElement to your list
   AddChilds(index,codeElement.Children);
}

private void AddChilds(int parentId,CodeElements codeElements) {
   foreach (CodeElement codeElement in codeElements) {
    if (codeElement.Kind != vsCMElement.vsCMElementParameter &&
        codeElement.Kind != vsCMElement.vsCMElementOther) {
     //Add codeElement to your list
     AddChilds(index,codeElement.Children);
    }
   }
  }

Image 3

What we have done till now is, we have placed our menu item and analyzed our solution, finally we have listed the solution files. When the user selects one of the listed files, you should check whether it is a physical file (its Kind should be Constants.vsProjectItemKindPhysicalFile)

Finally, we can open our file in Visual Studio with the following code:

C#
projectItem.Open(Constants.vsViewKindPrimary).Activate();

I've used icons that ship with Visual Studio .NET 2005, placed under C:\Program Files\Microsoft Visual Studio 8\Common7\VS2005ImageLibrary\VS2005ImageLibrary.zip. I could not find any license terms for them. If any exist, please let me know.

History

  • 6th March, 2006: Initial post

License

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


Written By
Web Developer
Turkey Turkey
He is a Computer Engineer living in İzmir,Turkey; worked on desktop applications, web technologies, smart clients with various programming languages like asp.net, php, C#, Java and with various database managements systems like Mysql, MsSql and Oracle

You can find his other posts and articles on his blog at http://community.enterprisecoding.com/blogs/fatih/default.aspx , you can also find his other open source projects on his web site http://www.enterprisecoding.com/

Comments and Discussions

 
GeneralQuick open file for VS 2008 Pin
Jhimy Fernandes Villar21-Oct-09 14:03
Jhimy Fernandes Villar21-Oct-09 14:03 
GeneralKeep up the good work and an off-topic add-in question please Pin
Alexandru Matei4-Dec-06 9:48
Alexandru Matei4-Dec-06 9:48 
GeneralRe: Keep up the good work and an off-topic add-in question please Pin
stmarti10-Jan-07 2:21
stmarti10-Jan-07 2:21 
GeneralRe: Keep up the good work and an off-topic add-in question please Pin
Alexandru Matei10-Jan-07 5:56
Alexandru Matei10-Jan-07 5:56 

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.