Visual Studio Toolbox Snippets

DZone 's Guide to

Visual Studio Toolbox Snippets

· ·
Free Resource

I don't use code snippets inside Visual Studio a great deal in my day to day coding, but there are times where it is handy to have them around.  I guess I should clarify that with I don't use custom code snippets in my normal day to day coding.  There are a bunch I use that are built in or part of custom add-ins.  For example, I can type "prop" and then tab and get a snippet to easily create a property in C#, or "foreach" then tab and get a nifty foreach loop setup for me.

As a presenter I have done my share of live coding and have been fairly successful.  I haven't really gotten bitten yet by doing it since most of the examples I use are very short.  While I haven't really had any trouble with the actual live coding part, it does take extra time.  A good portion of my presentations are on the ADO.NET Entity Framework and so doing some live coding helps show how things go together and can help show intellisense and such.  This is good and all, but some of the code really just is dead space while I type out a quick object initializer or something.

I have tried several code snippet tools and haven't really been impressed with any out there since all I was looking for was a way to just drop code that didn't need to be modified or templated.  I'm sure there are some out there I just haven't found yet, but one night before a code camp I was preparing for my presentation the next day and a friend turned me on to a cool feature in Visual Studio that apparently has been there since Visual Studio 2002.  

You can highlight a block of code in the editor and drag it to the toolbox.  Then when you need it, you can drag it out of the toolbox back to a code window or double click on the item in the toolbox and it inserts in where the cursor was.  I have a block of code I use fairly often in presentations where I need to spin up a new Person entity and fill it:

Person person = new Person();
person.FirstName = "Dane";
person.LastName = "Morgridge";
person.DateOfBirth = DateTime.Now;

When I highlight this code in visual studio and drag it to the toolbox, there is a new entry in my toolbox:

As you can see it isn't named very well, but a simple "F2" or a right-click-rename will take care of that.  Now I have a block of code that I can simply drag over when I need to use it.  This is very helpful when presenting and you need to quickly get to code that you don't want to live code.  I have seen people use Notepad to store their snippets (I have done this myself) and then copy and paste.  While that works, it requires you to jump out of Visual Studio to Notepad and then back.

I have found this method to be invaluable when presenting.  There is one small catch, that I happened to find the next day when I went to do my presentation.  I currently had a laptop with a hi-res screen and so I had a settings file I would switch to when presenting so reset my window layouts since I was working in a much smaller resolution while connected to a projector.  Well, the toolbox entries are part of that settings file so when I changed it, all of my snippets disappeared.  If I had saved my previous settings file, everything would have been fine, but lesson learned.  I also use this for other blocks of code that I find myself using frequently, like the StructureMap initialization code I use to setup my containers (More on that in the next few days...).

In short, I have found this to be a very handy place to drop code for later use.  It may not be the fanciest method, but the toolbox is an easy and efficient place to store snippets within Visual Studio without having to use any third-party plugins.


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}