Customize the VB IDE
The VB IDE is a terrifically productive development environment out of the box. But it can be a lot better still. Learn how to customize the IDE for better productivity and performance.
Technology Toolbox: VB.NET
- By Bill McCarthy
As a developer you probably spend large amounts of time every day working with Visual Studio; but have you taken the time to make it right for you, to learn about all the features it has? This thought was prompted by a funny thing that happened to me a couple of weeks ago. A storm knocked out the power in my house, forcing me to work from my laptop. After a couple of hours, the battery started running down, so I decided to charge the laptop from the car. I didn't want to sit in the car while it charged, but the keys had to be in the ignition for the car to charge my laptop. I thought there had to be a way to put the ignition on for accessories and take the keys with me, but I couldn't figure out how to do this. So, after owning the car for more than two years, I finally decided to read the manual that came with it.
Unfortunately, I didn't find a way to put the ignition on for accessories and take the keys, but I did find out some things about my car: There's a fuel safety switch behind the glove box I might have to reset after a collision, and, I discovered that I hadn't been locking the car properly for the two years I owned the car.
It turns out I have a thing called "super locking," which requires you to press the lock button once and then again within five seconds. I found these tidbits of information valuable, but it was a shame it took me two years and having to wade through a 450-page owner's manual to get them; it would have been much nicer if the dealer had mentioned them to me.
But it also made me wonder: How often do we just expect to start using things and not have to stop and read the manual? Does anyone really have the time to read all the manuals for all the tools and software he or she might have? How well do you really know Visual Studio, even though you use it day in and day out?
Setting up the IDE
You're asked to pick a set of developer settings the first time you start Visual Studio: Visual Basic Developer, General Developer, C# Developer, and so on. These settings change the toolbar layout, template order, shortcut keys, start page, and basic Visual Studio options.
For Visual Basic developers I recommend either using the General Developer or the Visual Basic settings, but with changes to the toolbars and options. If you prefer to press F8 to step to the next statement when debugging rather than F11, then pick Visual Basic developer settings.
The Visual Basic Settings provide a simplified IDE experience; however, in certain ways, it's an overly simplified experience. The instant find combo is missing from the toolbars, as is the build combo that lets you toggle easily between debug, release, or any custom build configurations. If you choose the Visual Basic settings, I recommend you import the Menu and Toolbar settings from the General Developer settings. To do this, select Import and Export Settings … from the Tools menu, then pick Import Selected Environmental Settings and select the General Development Settings collection. Finally, clear all the checkboxes except for the Menu and Command Bar Customizations and press Finish. The short cut keys will remain the same (F8 for step into, and so on), but now you have the search and build combos on the toolbar. The build combo appears disabled as you need to change some environmental options to enable it (see "Tweak the VB IDE,").
Environment Fonts and Colors
One set of options you should give a lot of consideration to is Environment Fonts and Colors. A recent trend I've observed among developers is to set the background to black, and have the text a light color. I still prefer the text dark on a light background, but I don't have my windows background set to pure white; instead, I set it to a light grey color such as RGB 243,245,244.
The exact color I use depends a lot on the monitor. If you're staring at a screen all day, it makes little sense to be staring at bright white. I set that background color for Windows, not just Visual Studio. In Visual Studio, I then modify the background color of Comments and Strings to a light green (RGB 230,255,230) and a pale yellow (RGB 254,255,234), respectively.
Once you have these options set to your liking, adjust the windows layouts for design time, code editing, and debugging, and then save your settings.
Adjusting Windows Layouts
To adjust the debugging layout, you'll need to start a project, such as a console project, press F5 to start it, and then lay out the windows. Typically, I put solution explorer on the left because that reminds me of Windows explorer. I also ensure the toolbox is on the right because it's less likely to cover my design surface when it "flies out" than if I have it on the left. When it is docked to the left, it can be awkward to add controls to the left side of the design surface.
Next, save your settings using the Import Export Settings entry from the Tools menu. You can then restore your exported settings should you need to, as well as take them to other machines.
Refine Your Toolbars
Visual Studio gives you other options for customizing your toolbars. For example, you can press CTRL as you drag items from another menu to make a copy of the item, rather than moving it. Also, Visual Studio 2005 and later let you customize the context menus. Simply select Context Menus from the Toolbars tab of the Customize dialog.
If you like to use Reflector from time to time, you should add it to the External Tools section of the Tools menu. When you do, set its arguments to be $(TargetPath), which ensures that reflector opens the output of the currently selected project automatically when launched (see Figure 1).
Files and Folder Sets
Another cool customization is the ability to launch Find in files and create folder sets.
For example, you might want to create a folder set called code samples and add paths to it where you store your code examples. I always create one called Platform SDK that points to the folder where the Platform SDK header files are located on my machine. This is handy when I'm searching for any Win API declarations or constants. When you double-click on a find result from searching in Find in Files using a folder set, the file opens up in the IDE and is added to the miscellaneous files in solution explorer.
Create Variables and Types
Another useful window to familiarize yourself with is the Immediate window, which also doubles as the Command window. You can toggle between Immediate and Command views by typing in >cmd and >immed. Immediate mode is for working with variables, while Command mode is for working with the IDE, such as when adding new project items. While in immediate mode, you can create variables and types implicitly in Visual basic.
For example, you can type f = New Form and a variable will be loaded into memory for you, instantiated as a form. You can do this for any type. If you type ? and the variable name, the window prints out the first 100 properties. If you call a method and have a break point in that method, the IDE switches to debug mode, and you can use the Watch window to view any variables you defined in the Immediate window.
One useful special variable name to remember is $exception, which is the last exception. You can use that in either the immediate window, by typing ? $exception, or add $exception to a watch window. Another useful command to remember is the >CLS command, which clears the window.
Customize Templates and Snippets
Visual Studio also lets you customize the templates you use each day. The easiest way to create a new template is to use the template wizard. Simply click on Export Template from the File menu and follow the prompts. I also like to customize the templates that ship with Visual Studio. For example, I like to add one line to set the Options at the top of code files:
Option Strict On: Option Explicit On: _
Option Compare Binary
The default templates are stored in .ZIP files in the Program Files folders, so you must have administrator rights to make modifications.
An example path for the Console Application template looks like this: C:\Program Files\Microsoft Visual Studio8\Common7\IDE\ProjectTemplates\ VisualBasic\Windows\1033\consoleapplication.zip.
If you extract the contents of the .ZIP file, you can then make any modifications you want to the files, repackage them as a .ZIP file, and replace the original .ZIP file. You need to run devenv.exe from a command line with administrator rights to signal to Visual Studio that the templates have been modified. Specify the /InstallVSTemplates switch to have Visual Studio rescan all the templates:
Snippets also provide a great way to enter commonly used code. Most often people use snippets for entering Property blocks, or any repeatable pattern they use in their code. However, you might want to create a snippet for Console.Writeline and give it the shortcut of cw. Or, you might want to create snippets for data-access blocks or similarly more complex blocks.
To see a hierarchal view of all snippets from inside the code editor, type a question mark and press the Tab key. You can also see a list of snippets based on their shortcut keys by typing any letter followed by a question mark then the tab key, like this:
Visual Studio doesn't come with a Snippet Editor, but you can download a free Snippet Editor.
The VB Snippet Editor is an open source project I worked on, previously hosted on the now-closed GotDotNet site. The original work was done by an intern at Microsoft. I did a major reworking of it, added a lot of features, and my fellow VB MVPs provided translations into Dutch, French, German, and Italian. This release works with Visual Studio 2008. The previous version for Visual Studio 2005 is also available in the download.
A word of caution: If you're using the snippet editor in Windows Vista, you need to run the application as Administrator. Otherwise, when you try to save files that are in the Program Files tree, Vista will save them in the Virtual Store, and Visual Studio won't see them properly.
These tricks and tips should be enough to get you started in thinking about VS customization, but I've barely scratched the surface of the different things you can do to get more out of Visual Studio. For example, you can also use macros and create add-ins. If you are looking for more tips on using Visual Studio, Sara Ford, a tester at Microsoft, has a great blog on the subject here.
Earlier I mentioned my experience with my car, and the "super locking" feature. Well, truth is, I had a similar experience with Visual Studio just this last week.
I was helping someone try to locate a strange compiler error. The developer insisted he was doing a Rebuild All, and for some reason I assumed that cleaned out any obj and debug artifacts. After many unsuccessful attempts at fixing the issue, I suggested he clean the project files. I was proposing that he delete any SUO files, the /obj and /bin paths, and so on, but he took my suggestion to mean that he should perform a Clean All. And guess what: The Clean All fixed the problem for the subsequent compile.
A Rebuild doesn't do a complete clean, and learning that after the fact doesn't give you back the hour lost. But knowing that can help you from wasting another hour in the future tracking down the same problem. In any case, it's well worth the time it takes up front to get to know your tools and get them working for you in a productive, enjoyable way.
About the Author
Bill McCarthy is an independent consultant based in Australia and is one of the foremost .NET language experts specializing in Visual Basic. He has been a Microsoft MVP for VB for the last nine years and sat in on internal development reviews with the Visual Basic team for the last five years where he helped to steer the language’s future direction. These days he writes his thoughts about language direction on his blog at http://msmvps.com/bill.