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.