FMTFMT

Creating a Visual Studio Extension Part 2 of 2

One of the reasons why Visual Studio is such a great development environment is its fluidity when it comes to customization. In my previous post, Creating a Visual Studio Extension Part 1, we discussed the preparation process for creating a Visual Studio Extension.

In this post, I will explain the basic steps needed to create a very simple Visual Studio Extension to access text from the currently opened Editor on Visual Studio.

If you have not installed the Visual Studio SDK, see my previous Blog post.

Creating Your First Project

Once the setup is complete, we are ready to get started. First, start your Visual Studio Version and create a new project.

Next select VSIX Project under Extensibility, choose a name and location for your new extension and press ok.

When done correctly, this is what you should see in Visual Studio 2015:

Visual Studio New Project

You should also get this nice Getting Started page with some useful information, as shown below.

Visual Studio Getting Started Page

If you are using Visual Studio 2013, you will start right in the VSIX Manifest Designer.
As soon as you are done reading the Getting Started page, go ahead and close it. You can read it again later by right clicking on the index.html file in the Solution Explorer and selecting open in browser.

Visual Studio Solution Explorer

Now that we’re ready to create our extension, let’s start with some minor cosmetics.

Double click on the source.extension.vsixmanifest file to open it. You should see the VSIX Manifest Designer for your vsixmanifest file. Note this is already open in Visual Studio 2013.

In this Designer you can edit metadata, install targets, assets and dependencies for your Extension.

VSIX Manifest Designer

The Designer allows you to enter a description for your Extension, pick an icon and add some release notes in order to let users know what they are using.

For now, we can just leave these fields blank, but remember to come back and add some useful information before deploying your extension.

Adding functionality to the Extension

Now it is time to add some functionality to your extension.
Right click on your project and select Add and then New Item. Next, navigate to C# and Extensibility.

Visual Studio Extensibility

Select the CustomCommand item, give it a name, and click the Add Button.

A new class will be created for you and some code will be generated, but don’t change anything yet.

Debugging your Visual Studio Extension

Save everything and start your new CustomCommand Extension for the first time by pressing F5.
Your solution should build and the new Extension will start to debug in an Experimental Visual Studio instance.

An Experimental Visual Studio instance is a new instance of Visual Studio without any of your settings. All changes in this instance are not saved, so you don’t have to worry about destroying your settings by accident. See more Information about Experimental Instances
The first time you start your Experimental Instance you will see the Welcome Window. You only have to do this once. Just press “Not now, maybe later” pick a color theme and start the new Instance of Visual Studio.

Visual Studio Welcome Window

Go to the Tools menu in Visual Studio and you should see your Custom Command as Invoke CustomCommand.

Visual Studio CustomCommand

Clicking on it will show you a Message Box with some text.

Closing the Experimental Instance should stop your Visual Studio Debug run.

Adding some custom code

Go back to your “real” Visual Studio instance and scroll down in your CustomCommand class (mine is named CustomCommand.cs).
You should find a Method Called MenuItemCallback.

Visual Studio MenuItemCallback

This is the method that is executed when you click on the menu item in the Experimental Instance.
This code was generated by Visual Studio when you first created your class. So let us change it up a bit.

For this short How-To we will create some simple logic.
When our CustomCommand is executed in Visual Studio, the text from the currently active document in Visual Studio will be displayed in a message box.

Visual Studio CustomCommand 1

First, we get a top level object from the Visual Studio instance. It is important to make sure it is not null.

Visual Studio CustomCommand2

Next, we get the currently active document and make sure that is not null.

Visual Studio CustomCommand 3

We make sure it is a TextDocument and cast it.

Visual Studio Custom Command TextDocument

Finally, we will see the text from the document from the start to the end.
And all that’s left is to just display that text.

If everything is correct, this  is what our new Method will look like:

Visual Studio CustomCommand5

Replace the old method MenuItemCallback in your Extension with this new one and debug your Extension again.

Running your Visual Studio Extension

In your new Experimental Instance of Visual Studio, create a new text file and open it in the editor.

Enter some text in your new text file.

Visual Studio Extension Text File

Click on Tools and select Invoke CustomCommand to start your CustomCommand.
You should see a Messagebox with the text from your currently active editor as text.

Visual Studio Custom Command Message Box

Congratulations! You’ve created your first Visual Studio CustomCommand Extension.

This is just a very simple example of what your can do by creating your own Visual Studio Extensions.
Now get creative and customize it.
Add a spelling check to your files, minify JavaScript, create the Extension that helps you or your users be more productive.

If you have any questions about Visual Studio, or would like to learn more about how Visual Studio can help you to develop your own Extensions, don’t hesitate to contact us using the form below. We’d be happy to help!

©
2023
 FMT Consultants, LLC.
|
Privacy Policy
X
FMT

Contact Us

X
FMT

Newsletter Sign-up

menu linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram