For those of you who aren't aware, code snippets are great. I don't use them nearly as much as I should but I've recently started making a concerted effort to find out what snippets VS supplies by default and also to create some of my own.
My motivation for supplying these here is partly to help eradicate the hated MsgBox function from VB.NET. One of the arguments I've seen for using MsgBox is that it takes fewer keystrokes than using MessageBox.Show. My counter to that has always been that if you truly care about a few keystrokes then you should be using code snippets anyway. Well, I'm putting my money where my mouth is.
The attached ZIP file contains installers for 11 code snippets designed for entering the MessageBox.Show function in various ways. Just extract the VSI files from the ZIP and then run each one separately. Each one will prompt you to install a code snippet. Unless you want them in a specific location I'd suggest you just accept the default path of My Code Snippets. Physically, that folder is located under the Code Snippets folder that is at the same level as your VS Projects folder.
Once the snippets are installed you can immediately start using them in your code. You simply type the shortcut for snippet you want to use (the shortcuts are the same as the file names for these snippets) and hit the Tab key to enter the code the snippet contains. Certain parts of the code will then be highlighted and you can use the Tab key to jump from one to the next, entering the desired values as you go if they differ from the defaults.
The shortcuts for the snippets all follow a convention. They all include the number of arguments passed to the MessageBox.Show method following a prefix. The prefixes are "mb", "imb", "iemb" and "smb", which indicate the MessageBox.Show method alone, in an If statement, in an If...Else statement and in a Select Case statement respectively. For instance, the shortcut mb1 will result in the MessageBox.Show method being entered with one argument:
vb.net Code:
MessageBox.Show(text)
The single argument will be selected so you can just start typing to replace it with a literal string or a String variable. mb2 will enter MessageBox.Show with two arguments:
vb.net Code:
MessageBox.Show(text, caption)
You can overwrite the first argument if desired and then hit Tab to select the second. imb3 will enter the MessageBox.Show method with three arguments in an If statement:
vb.net Code:
If MessageBox.Show(text, _
caption, _
MessageBoxButtons.OKCancel) = Windows.Forms.DialogResult.OK Then
End If
You can Tab to each argument and overwrite if desired and then Tab again to select the result and change that too. iemb4 will enter MessageBox.Show with four arguments in an If...Else statement:
vb.net Code:
If MessageBox.Show(text, _
caption, _
MessageBoxButtons.OKCancel, _
MessageBoxIcon.Error) = Windows.Forms.DialogResult.OK Then
Else
End If
smb5 will enter the MessageBox.Show method with five arguments in a Select Case statement:
vb.net Code:
Select Case MessageBox.Show(text, _
caption, _
MessageBoxButtons.YesNoCancel, _
MessageBoxIcon.Error, _
MessageBoxDefaultButton.Button2)
Case Windows.Forms.DialogResult.Yes
Case Windows.Forms.DialogResult.No
Case Windows.Forms.DialogResult.Cancel
End Select
The full list of shortcuts for the eleven snippets is mb1, mb2, imb3, imb4, imb5, iemb3, iemb4, iemb5, smb3, smb4, smb5.
If you want to know more about code snippets then you can read about the XML schema used to define them here. You can write the raw XML yourself if you like, or you can use a tool such as this, which comes originally from a Microsoft employee but has been further developed by others. There's a tutorial here, although it's actually for an older version, which can be found here. The information all still pretty relevant though, and you should be able to work out what's new for yourself. There's plenty more information on code snippets and their use on MSDN too, plus other tools on the web. I'm actually working on an editor myself right now, as an intro to WPF. I'll post it when I'm done. Enjoy!
P.S. Death to the MsgBox function!!
Last edited by jmcilhinney; Oct 18th, 2009 at 12:03 AM.
Nice post. I used to use the built in snippets when I first started learning VB, but I found it kind of a pain to create snippets using any of the existing tools. Maybe your editor will change that. I currently use a well organized text file to store my snippets & just copy & paste them into my code. One of the things I don't like about the VS snippets is that you can't see the code that will get inserted. My text file may be old school, but at least I can review the code before pasting it in, and it's simple to maintain & update. I will give your tool a try though, when you finish it.
Nice post. I used to use the built in snippets when I first started learning VB, but I found it kind of a pain to create snippets using any of the existing tools. Maybe your editor will change that. I currently use a well organized text file to store my snippets & just copy & paste them into my code. One of the things I don't like about the VS snippets is that you can't see the code that will get inserted. My text file may be old school, but at least I can review the code before pasting it in, and it's simple to maintain & update. I will give your tool a try though, when you finish it.
One of the good things about VS code snippets though is the navigation and substitution. One of my favourites is typing prop and hitting Tab twice and getting a property declaration complete with backing field. You can then set the field name and it gets automatically updated in the getter and setter too. The same goes for setting the field type and updating the property type and the setter parameter type. Your text file won't do that.
Code preview would have to be something built into VS, or a VS add-in, but if you know your snippets then you know what the code will look like and if it's not what you want for some reason you can just hit Ctrl+Z and the whole lot is removed because it is inserted atomically.
You are right in that the VS snippets provide functionality that my text file doesn't. But it was the frustration of creating the snippets that lead me to using a text file. I tried several tools, but it seems like the snippets wouldn't show up in VS or they would go to the wrong place, or get created twice, etc. Perhaps it was just my inexperience, but I just gave up on creating my own. It just seems like it wasn't a very intuitive or efficient process. My text file has served me well over the past few years & I would be lost without it now...
You are right in that the VS snippets provide functionality that my text file doesn't. But it was the frustration of creating the snippets that lead me to using a text file. I tried several tools, but it seems like the snippets wouldn't show up in VS or they would go to the wrong place, or get created twice, etc. Perhaps it was just my inexperience, but I just gave up on creating my own. It just seems like it wasn't a very intuitive or efficient process. My text file has served me well over the past few years & I would be lost without it now...
I can't say that I've ever had any issues with either version of the snippet editor that I posted links. To each their own though. I think I'd find copying and pasting boilerplate code from a text file rather cumbersome.
You must have encountered some short-comings with the existing tools if you are writing your own.
Not really. Only tiny niggles really. It's more the case that I was looking for a project to work on for my first foray into WPF. I was also interested in exploring the XML schema for code snippets too, so writing my own editor was a way to do that too.