Build Your First Visual Studio VSIX Plugin

I few weeks ago, I had a task that required me to use reflection against an existing assembly, find a specific type in it, and generate a similar for current project. I had to do this task a  number of times each day.  I felt it was a great opportunity to learn about Visual Studio plugs ins and write one.  MSDN information was somewhat helpful, but I could not figure out how to do a simple thing – add a menu item and popup a custom form when item is clicked.  Once that is done, all other code could be in the form itself.  It took me a while to figure this task out, so I wanted to document a few simple steps required in getting this scenario working.  In this post I am using VS 2013.

Step 1

Create new project and pick Visual Studio Package project template.  I used C#

 

image

Next, walk through the wizard that will be kicked off, filling in necessary information.

image

 

image

 

image

 

image

 

image

 

image

After you click Finish, you will see the following project structure:

image

Now, what I want to do is popup a form with user input.  To do that, just add new item to VSPackage2 project and pick Windows Form (or WPF).

image

Form will open in designer.  You can now add controls and code to the form.  I just add a button with an event.

image

private void button1_Click(object sender, EventArgs e)
{
     MessageBox.Show("Hello world", "My first VSIX");
}

 

Now, i need to popup this form.  Locate the package C# file.  Mine is called VSPackage2Package.cs based on project name of VSPackage2.  Inside of it, find MenuItemCallback method and create and popup the form there.  Delete all generated code first.

private void MenuItemCallback(object sender, EventArgs e)
{
            var form = new Form1();
            form.Show();
 }

Now, build the solution.  Then got to bindebug folder for VSPackage2 project and find your VSIX file.  If you want to change description or other common things, just double click on .vsixmanifest file to see the manifest editor.  Once you double-click on vsix file, you will see familiar VSIX installer dialog.  Just install your new VSIX and restart the studio.  Under Tools menu you will see the text for your menu item – Click Me in my case.  Click on that.  You will see your form!

image

You can uninstall your test package from Extensions and Updates menu.

Enjoy.

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *