Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Visual-Studio

Got Sweet Code? Save It!

4.33/5 (2 votes)
12 May 2014CPOL2 min read 10.1K  
Got sweet code? Save it!

If you’re a Visual Studio user, you’re more than familiar with snippets (I would hope). ctor, propa, testm, all very useful. But have you created your own snippets?

You can do this with tools like the Snippet Designer extension, or roll your own by creating & editing a snippet XML file by hand based on the snippet schema.

Ain’t nobody got time for that.

Far and away, the quickest way I’ve found to save code I reuse often is even simpler than all that. And I’m about to clue you in on it.

If you’ve been to BUILD or other conferences/presentations by Microsoft folk, you’ve undoubtedly seen them take their “sample code” and insert it via a snippet. But what I saw at BUILD 2014 this past April surprised me. All the presenters had the toolbox open *in code view* and were simply dragging & dropping code snippets from the toolbox right in to their code window. Every time they did it, they did so fast that I wasn’t even able to see exactly where things were coming from or what they looked like.

This past week I was preparing for a presentation and decided to just give something a whirl:

Image 1

I got my code all ready in my sample app, and highlighted it. Then I got an idea… what if…

Image 2

Sure enough, just dragging the code over to the toolbox gives you a little ‘text’ entry in your toolbox. You can even rename it so you know what you’re going to use it for:

Image 3

Now, when you want to use it again, you just drag that toolbox item back in to your code:

Image 4

And boom, it appears.

Insanely useful for doing live-code presentations, or storing away snippets you use over and over without the hassle of actually writing a snippet.

The best part? These items persist across VS launches; they’ll be there the next time you launch Visual Studio.

License

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