BUY THIS BOOK
Add to Cart

PDF $19.99

Safari Books Online

What is this?

Looking to Reprint or License this content?


Visual Studio Hacks
Visual Studio Hacks Tips & Tools for Turbocharging the IDE By James Avery
March 2005
Pages: 500

Cover | Table of Contents | Colophon


Table of Contents

Chapter 1: Master Projects and Solutions
The majority of the time you spend working with Visual Studio will be spent inside of a project or a solution. You might edit the odd file that is not attached to a project or solution, but most files you work with will be in the context of a project or solution.
Projects can be used for many purposes, but primarily they are used to organize source code that will be compiled into a library or application.
Solutions collect a number of projects together under a single structure. The metaphor of solution works because it collects all the projects that make up your business solution . You may have five projects that make up various parts of your overall business solution. For example, you might have a data access project, business layer project, service interface layer project, presentation layer project, and web controls project. The solution groups all of these projects together and allows you to quickly move between projects and manage references between them.
The hacks in this chapter show you how to create and manage projects and solutions, manage assembly references, and dissect the formats of project and solution files. If you're not already familiar with how to work with projects and solutions, this chapter will better prepare you to get the most out of the other hacks in this book.
Visual Studio is all about projects and solutions. It's essential that you understand the differences between them, and get comfortable with using them.
A large part of working with Visual Studio is the management of solutions and projects. Nothing can hamper a project more than a poorly designed solution or project structure. In this hack, you will learn how to work with and configure solutions and projects.
This hack applies to all versions of Visual Studio, but the dialogs and screens will vary slightly between versions. The screenshots in this hack are all taken from Visual Studio .NET 2003 unless noted otherwise.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Hacks 1-5
The majority of the time you spend working with Visual Studio will be spent inside of a project or a solution. You might edit the odd file that is not attached to a project or solution, but most files you work with will be in the context of a project or solution.
Projects can be used for many purposes, but primarily they are used to organize source code that will be compiled into a library or application.
Solutions collect a number of projects together under a single structure. The metaphor of solution works because it collects all the projects that make up your business solution . You may have five projects that make up various parts of your overall business solution. For example, you might have a data access project, business layer project, service interface layer project, presentation layer project, and web controls project. The solution groups all of these projects together and allows you to quickly move between projects and manage references between them.
The hacks in this chapter show you how to create and manage projects and solutions, manage assembly references, and dissect the formats of project and solution files. If you're not already familiar with how to work with projects and solutions, this chapter will better prepare you to get the most out of the other hacks in this book.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Manage Projects and Solutions
Visual Studio is all about projects and solutions. It's essential that you understand the differences between them, and get comfortable with using them.
A large part of working with Visual Studio is the management of solutions and projects. Nothing can hamper a project more than a poorly designed solution or project structure. In this hack, you will learn how to work with and configure solutions and projects.
This hack applies to all versions of Visual Studio, but the dialogs and screens will vary slightly between versions. The screenshots in this hack are all taken from Visual Studio .NET 2003 unless noted otherwise.
Solutions are Visual Studio's highest level of organization. They collect any number of projects together under one manageable structure. Solutions store information about the contained projects, including project dependencies and build order, and can also contain miscellaneous solution items. Solution files have a few limitations. Only a single solution can be opened in an instance of Visual Studio at a time, and solution files cannot contain other solution files. Projects, however, can be members of multiple solutions. This allows you to create a number of solutions for different purposes that make use of the same projects. (An example of this is business entities or interfaces that you need to share across client and server solutions.)
Solutions are the best way to keep projects under source control. If you add the solution to source control, every developer on the project can use the same solution and is guaranteed to use the same project and file structure.

Section 1.2.1.1: Creating a solution

A solution is automatically created when you create a new project, but you can also create a blank solution without creating a project. To create a new blank solution, click on File New Blank Solution. You can then add whatever projects you need to the blank solution.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Master Assembly and Project References
Visual Studio uses references to identify the external assemblies that your project relies on. How these references are used depends on the type of reference as well as a number of other factors.
Adding a reference to a project is a simple matter of right-clicking on the references folder of a project and selecting Add Reference. (Add Web Reference is a completely different feature that is used for creating a reference to a web service and won't be covered here.)
The Add Reference dialog contains a number of tabs, three in Visual Studio .NET 2002 and 2003 and five in Visual Studio 2005. Figure 1-11 shows the Visual Studio .NET 2003 Add Reference dialog.
Figure 1-11: Visual Studio .NET 2003 Add Reference dialog
The .NET tab contains a list of .NET assemblies. You can select any of these assemblies and create a reference to that assembly. The COM tab contains a list of all the registered COM objects that you can reference. The Projects tab contains a list of all the projects in your solution, and by selecting one, you can create a reference directly to that project.
Visual Studio 2005's Add Reference dialog is shown with its extra tabs (Browse and Recent) in Figure 1-12.
Figure 1-12: Visual Studio 2005 Add Reference dialog
The Browse tab is used to browse for an assembly from the filesystem—the same as the Browse button from the Add Reference dialog of older versions of Visual Studio. The other new tab is the Recent tab, which shows any assemblies you have recently used.
The majority of the time this process is as simple as selecting what you want to reference and then clicking the OK button. However, a better understanding of how references work will help avoid headaches when sharing projects between users and when working with projects under source control. One of the most frequent issues that developers encounter when working with shared solutions and projects is broken references.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Organize Projects and Solutions
Most development projects consist of more than one project and are grouped under a single solution file. How this solution file is created is the first thing to consider when organizing your projects and solutions.
Visual Studio creates a solution for you when you create a new project. The problem with this is that the solution will be named after your project and will be in the same directory as your new project. If you later add an additional project to your solution, your file structure will look something like this:
Folder A\
    Solution File
    Project File A
Folder B\
    Project File B
The problem with this structure is that it is not easy to work with since the solution file is nested inside one of the project directories and references a project file that is contained in a different directory. If you were to move Folder A to a different location, the solution would end up with a broken reference to Project File B. A better way to handle solution files is to always start with a blank solution and then add project files to this solution over time. To start with a blank solution, you simply need to click on File New Blank Solution. You can create a directory for this blank solution and add projects to this solution. Your file structure will now look more like this:
Solution Directory\
    Solution File
    Folder A\
        Project File A
    Folder B\
        Project File B
This file structure is much more manageable. You can check the entire solution into SourceSafe, you can safely move the solution directory, and you can even zip up the entire directory structure.
Whenever possible, you should stick to a single solution for your development project. But when you get above a certain number of projects in a solution, it starts to become unwieldy for you to manage. It is also inconvenient to have multiple executables or web projects in a single solution, as you will constantly be switching the startup project when you want to run or debug one of the other executables. There are a number of different strategies for working with multiple solutions:
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Hack the Project and Solution Files
Discover the format of these two Visual Studio files and learn about a tool to convert these files between versions of Visual Studio.
Solution and project files are an essential part of working with Visual Studio. You will almost always work with the solution and project files through the IDE, whether adding projects to your solution or configuring your project. The purpose of this hack is to describe the format of these files for two reasons. If your project or solution files become corrupted, knowing the structure of these files might help you fix the file without having to re-create the entire project or solution. Also, knowing the structure of these files will help you if you want to convert these files or write a tool that works directly with these files.
You must edit these files with extreme care. The format of these files is not published or documented and could change drastically in future versions. You should normally need to work with these files only through the IDE, and unless you find a compelling reason to, I would not directly edit these files. If you are interested only in converting the version of these files, then you may want to look toward the end of this hack under Section 1.5.3 to read about a tool that will do the conversion for you.
Visual Studio creates two separate files when you create a new solution in Visual Studio. The first file is the .suo (solution user options) file, which stores user settings such as the location of your breakpoints. This file is stored in binary format, which does not lend itself to easy editing. Since there are no compelling reasons to edit this file, I am not going to document the format of it here. If you think that this file is preventing your solution from opening, you can actually delete the file and Visual Studio will create a new one when you open the solution and save it when you close the IDE. By deleting the file, you will lose any of your user-specific settings like breakpoints, but this is a small price to pay for saving your solution. The
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Remove SourceSafe Bindings
Visual SourceSafe comes bundled with Visual Studio for free, so it is one of the most popular source code control programs for Visual Studio developers. However, it will leave cruft in your source tree that you might want to get rid of if you send your code elsewhere.
In order to do its job, SourceSafe adds some XML elements to the Visual Studio solution and project files and adds some source control files to each project directory. These changes are transparent when using SourceSafe, but cause problems when sharing the solution with someone who does not use SourceSafe or someone who does not have access to your SourceSafe database. It could also cause problems if you are attempting to change your source code control provider from SourceSafe to something else, like CVS or Subversion.
You must change two things to remove all SourceSafe bindings. The solution file and all project files must have any source control information removed, and any files ending in .scc must be deleted. To do this, the Visual Studio solution and project files must not be in use, so close down the Visual Studio IDE.
It is best to make a copy of the entire Visual Studio solution before hacking any files. If something goes wrong, you should have a backup.
Visual Studio solution files are simple text files ending in .sln. Right-click on a solution file, select the Open With... option, then choose Notepad to open the file for modification. Examining the solution file for a sample Visual Studio application called SourceSafeBindingRemover yields this code:
Microsoft Visual Studio Solution File, Format Version 8.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = 
"SourceSafeBindingRemover",
"SourceSafeBindingRemover\SourceSafeBindingRemover.csproj", 
"{C7687560-4B36-47E3-AF33-748E76411259}"
    ProjectSection(ProjectDependencies) = postProject
    EndProjectSection
EndProject
Global
    GlobalSection(SourceCodeControl) = preSolution
        SccNumberOfProjects = 2
        SccLocalPath0 = .
        CanCheckoutShared = false
        SolutionUniqueID = {634C866F-3CEB-43A1-9C7F-D34A03F0A044}
        SccProjectUniqueName1 = 
        SourceSafeBindingRemover\\SourceSafeBindingRemover.csproj
        SccLocalPath1 = .
        CanCheckoutShared = false
        SccProjectFilePathRelativizedFromConnection1 = 
            SourceSafeBindingRemover\\
    EndGlobalSection
    GlobalSection(SolutionConfiguration) = preSolution
        Debug = Debug
        Release = Release
    EndGlobalSection
    GlobalSection(ProjectConfiguration) = postSolution
     {C7687560-4B36-47E3-AF33-748E76411259}.Debug.ActiveCfg 
        = Debug|.NET
     {C7687560-4B36-47E3-AF33-748E76411259}.Debug.Build.0 
        = Debug|.NET
     {C7687560-4B36-47E3-AF33-748E76411259}.Release.ActiveCfg 
        = Release|.NET
     {C7687560-4B36-47E3-AF33-748E76411259}.Release.Build.0 
        = Release|.NET
    EndGlobalSection
    GlobalSection(ExtensibilityGlobals) = postSolution
    EndGlobalSection
    GlobalSection(ExtensibilityAddIns) = postSolution
    EndGlobalSection
EndGlobal
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Chapter 2: Master the Editor
Visual Studio is, above all else, a code editor. Before getting into some of the more advanced hacks like creating and using add-ins, customizing Visual Studio, or speeding up how quickly you write code, you must first learn how to best take advantage of the editor that is at the heart of Visual Studio.
The editor is where you probably spend most of your time in Visual Studio—after all, the editor is where you write and edit your code. The editor is also probably a key reason that you have decided to use Visual Studio as opposed to a simple text editor like Notepad. The editor offers enhancements like syntax coloring, IntelliSense, code formatting, and much more.
This chapter includes hacks on how to best take advantage of the clipboard, including an add-in that can be used to easily paste long strings into the editor. This chapter also include hacks that look at how to get the most out of IntelliSense and how to master the use of regions. Last, you will learn how to choose the right editor and learn how to use a couple new editor features in Visual Studio 2005.
Throughout this book, we will cover a lot of keyboard shortcuts, but since keyboard shortcuts differ between developer profiles, we will also include the name of the command for that keystroke. For example, when referring to the Ctrl-C shortcut, we will also include the name of the command in parentheses (Edit.Copy). If you know the command, you can always use the Keyboard Settings screen to either find the keystroke for that command in your profile, or you can create a new shortcut for that command. For more information on creating and managing your keyboard shortcuts, please refer to [Hack #24] .
In theory, a clipboard should be simple: copy, cut, or paste. But that's no fun! You can learn how to really supercharge these operations.
Visual Studio contains a number of different features pertaining to cutting and pasting and the clipboard. Some of these features are old text editor favorites that are finally making it into Visual Studio, and some are new innovations from the Visual Studio team. These might seem like small features in a program that is as feature-packed as Visual Studio, but the clipboard will affect your day-to-day programming more than most other features.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Hacks 6-15
Visual Studio is, above all else, a code editor. Before getting into some of the more advanced hacks like creating and using add-ins, customizing Visual Studio, or speeding up how quickly you write code, you must first learn how to best take advantage of the editor that is at the heart of Visual Studio.
The editor is where you probably spend most of your time in Visual Studio—after all, the editor is where you write and edit your code. The editor is also probably a key reason that you have decided to use Visual Studio as opposed to a simple text editor like Notepad. The editor offers enhancements like syntax coloring, IntelliSense, code formatting, and much more.
This chapter includes hacks on how to best take advantage of the clipboard, including an add-in that can be used to easily paste long strings into the editor. This chapter also include hacks that look at how to get the most out of IntelliSense and how to master the use of regions. Last, you will learn how to choose the right editor and learn how to use a couple new editor features in Visual Studio 2005.
Throughout this book, we will cover a lot of keyboard shortcuts, but since keyboard shortcuts differ between developer profiles, we will also include the name of the command for that keystroke. For example, when referring to the Ctrl-C shortcut, we will also include the name of the command in parentheses (Edit.Copy). If you know the command, you can always use the Keyboard Settings screen to either find the keystroke for that command in your profile, or you can create a new shortcut for that command. For more information on creating and managing your keyboard shortcuts, please refer to [Hack #24] .
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Master the Clipboard
In theory, a clipboard should be simple: copy, cut, or paste. But that's no fun! You can learn how to really supercharge these operations.
Visual Studio contains a number of different features pertaining to cutting and pasting and the clipboard. Some of these features are old text editor favorites that are finally making it into Visual Studio, and some are new innovations from the Visual Studio team. These might seem like small features in a program that is as feature-packed as Visual Studio, but the clipboard will affect your day-to-day programming more than most other features.
When you copy and paste text in any application, you are usually limited to copying and pasting one item at a time. If you want to copy two separate sentences, you have to copy the first one, paste it, then come back and repeat this for the next sentence. This can become tedious when you have 10 different things to copy and they reside in 10 different places in the document. You end up switching back and forth between the two documents 10 times, once for each sentence you want to copy.
The clipboard ring eliminates this limitation. The clipboard ring allows you to cut or copy up to 20 selections and access them using a keyboard shortcut. Here is the process for using the clipboard ring:
  1. Copy (Ctrl-C, Edit.Copy) or Cut (Ctrl-X, Edit.Cut) up to 20 text selections, one after the other. These selections are organized as a last-in-first-out (LIFO) stack. That is, the last item you cut or copy will be the top item on the clipboard ring.
  2. Press Ctrl-Shift-V to paste the first selection (the item on the top) from the clipboard ring into your document.
  3. If you don't want to paste the first selection, press Ctrl-Shift-V (Edit.CycleClipboardRing) again, and the first thing you pasted will be replaced with the next selection from the clipboard ring. For example, if you wanted to paste the fourth item in the clipboard ring, you would simply press Ctrl-Shift-V four times. If you want to paste the same item more than once, simply pressing Ctrl-V (Edit.Paste) after the first time will work, since it will now be the current item on the clipboard.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Make Pasting into Visual Studio Easier
Don't be limited to plain text. You can paste strings into Visual Studio as comments, string, StringBuilders, and more.
SmartPaster is a plug-in for Visual Studio .NET 2003 that allows text on the clipboard to be pasted in a format compatible with C# and Visual Basic code. SmartPaster can be downloaded from http://www.papadimoulis.com/alex/SmartPaster1.1.zip. After downloading and installing SmartPaster, you will see a new item on the right-click (context) menu, which is shown in Figure 2-4.
Figure 2-4: SmartPaster menu
I find myself most frequently pasting text as a string or a StringBuilder. You can copy any sort of text from another application, then when you paste that text into Visual Studio, you can choose to paste it as a string or as a StringBuilder.
Many developers like to build a SQL statement using a tool such as Query Analyzer, for easy testing and debugging, or Microsoft Access, for quick, visual development. As simple as it is to build queries externally, putting them into code can often be a challenge, especially when the queries span multiple lines. SmartPaster eases the task of bringing external queries to code: simply copy your query to the clipboard and paste as a string or StringBuilder. For example, if you copied the following SQL to your clipboard:
SET ROWCOUNT 10
SELECT ProductName
FROM Products
ORDER BY Products.UnitPrice DESC
then paste the code into Visual Studio using Paste As String, you would see the following code:
@"SET ROWCOUNT 10" + Environment.NewLine +
@"SELECT ProductName" + Environment.NewLine +
@"FROM Products" + Environment.NewLine +
@"ORDER BY Products.UnitPrice DESC" + Environment.NewLine +
@""
You could also paste this code using Paste As StringBuilder and specify a StringBuilder name of "sqlBuilder," and this would result in the following code:
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Master IntelliSense
Visual Studio can read your mind. Learn how to have Visual Studio's IntelliSense feature do lots of the typing for you.
Even if you have used Visual Studio only a couple times, I am sure you have noticed one of its trademark features: IntelliSense. The most popular form of IntelliSense assists you as you code by providing a list of the members for each class immediately after you finish typing the class name (and the following period). This is not the only form of IntelliSense though; this feature finds its way into a multitude of different areas of Visual Studio.
The first, and most useful, IntelliSense feature is actually just a shortcut: Ctrl-Space (Edit.CompleteWord). By using the Ctrl-Space shortcut, you can summon IntelliSense at any point during your coding session, not just when you finish typing a class name. This is one of the few shortcuts that can really change the way that you write code.
If you have already starting typing when you press Ctrl-Space, Visual Studio will take one of two actions. If there is only one object that matches what you have typed in so far, it will automatically complete the object name; for instance, if you typed in HttpR, it would automatically complete the rest of the object name (HttpRequest). If there are a number of objects that match what you have typed in, it will display the full list of members with the first match highlighted. Figure 2-7 shows what the listbox would look like if you only typed in the letter P.
Figure 2-7: IntelliSense listbox, brought to you by the letter P
You can also press Ctrl-Space on a blank line and be shown a complete list of members in the current context. This is a great way to find a variable name that has slipped your mind. Figure 2-8 shows an example of this.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Master Regions
Tag, expand, and collapse regions. Visual Studio's regions let you make even the largest source files become instantly readable.
Regions are a valuable part of organizing your code and making it more readable. Using regions, you can select a section of code that you want to be able to logically group and also collapse. In this hack, you will learn how to use regions in both C# and VB.NET and learn about an add-in that makes working with regions even easier.
To use regions in C#, you simply need to surround the code you want to include in the region with #region and #endregion. Optionally, you can also include a string literal after #region that will be shown when the region is closed. Figure 2-15 shows an example of C# code surrounded by a region.
Figure 2-15: C# region expanded
Figure 2-16 shows this same region, except collapsed.
Figure 2-16: C# region collapsed
In C#, you can include regions anywhere in your source code file, even inside of methods or properties. (Whether this is a good idea is debatable.) Regions can also be nested inside of other regions.
VB.NET regions are a little bit different than C# regions. In VB.NET, you surround the code that you want to include in the region with #Region and #End Region. You can also include a string after the #Region, but in VB.NET, the string will need to be enclosed in quotes. Figure 2-17 shows an example of VB.NET code surrounded by a region.
Figure 2-17: VB.NET region expanded
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Add Guidelines to the Text Editor
If you've got to have everything line up just right, turn on guidelines and get everything pixel perfect.
One of the features missing from the text editor in Visual Studio is the ability to add vertical guidelines. Vertical guidelines are thin vertical lines that can be used as visual guides when aligning text. This feature is not actually missing though—using a simple registry hack, you can add up to 13 different guidelines to Visual Studio's text editor.
When editing the registry, you should always back up your registry or use the alternative registry method [Hack #30] .
To add guidelines:
  1. Close Visual Studio.
  2. Open regedit (Start Run type regedit).
  3. Navigate to HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\<7.1>\Text Editor.
  4. Right-click on the Text Editor key and choose New String Value and name it "Guides".
  5. Set the value of the guides to RGB(128, 128, 128) 4, 16.
The first part of the value sets the color of the guidelines using common red, green, and blue values. 128, 128, and 128 sets the color of the guidelines to gray. The second numbers specify where the guidelines should appear. In this example, guidelines will be shown at the 4-space mark as well as the 16-space mark. You can add up to 13 different guidelines by simply adding more numeric values separated by commas.
After you have created your registry entry, you will see guidelines in the marks specified when you launch Visual Studio. Figure 2-21 shows an example of the results from the example settings.
Figure 2-21: Guidelines in Visual Studio
Removing these guidelines is simply a matter of deleting the Guides registry key and restarting Visual Studio.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Select the Best Editor
Different editors can be used for different file types, including some hidden possibilities.
A large part of learning to work with any toolset is understanding what tool should be used and when it should be used. Visual Studio contains a number of different editors, including a Text Editor, XML Editor, CSS Editor, and many more. This hack is about determining what editor should be used in what situation and also explains how Visual Studio can be configured to use a third-party editor as opposed to the native editor.
Visual Studio contains a host of different editors; you have probably encountered a number of them.

Section 2.7.1.1: Text Editor

The editor you are probably most familiar with is the Text Editor, which is used to edit and work with most forms of text, including source code. The Text Editor contains a number of configuration settings that can be specified on a per-language basis; these settings can be accessed through the Tools Options menu, which can be seen in Figure 2-22.
Figure 2-22: Text Editor Options dialog
Using the Options dialog, you can decide whether or not to enable word wrap, line numbers, and some other miscellaneous settings. Each language can be configured independently of the other languages; each is configured to different defaults initially.
As of Visual Studio .NET 2003, the VB.NET editor is set to Hide Advanced Members automatically. This is something you will most likely want to turn off, as it stops certain methods and properties from showing up in IntelliSense. Some of the hidden methods, like Page.RegisterClientScriptBlock, are very valuable and encourage better development and so should not remain hidden. The idea behind this setting is to filter members that won't normally be used by your average developer, but as it turns out, some of the members it hides are indeed very useful. Methods marked with the attribute
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Customize Syntax Coloring
Specify the keywords you want Visual Studio to colorize, even in PHP!
When working with Visual Studio, you will notice that language keywords are highlighted in blue. This coloring makes code more readable. If you ever want to see the impact that this coloring has, open a code file in Notepad and notice how much less readable it is.
Unfortunately, most languages, including VB.NET and C#, won't let you change how keywords are colored in Visual Studio. One language that does provide a method for specifying your own keywords is C++. For example, you can add the word "DateTime" as a keyword to C++ with a few simple steps. First, you need to create a file named usertype.dat . This is a simple text file that contains all of the words that you want to be colored as keywords.
Start by creating a new text file in your favorite text editor, add the word DateTime to the top line of that file, then save that file with the name of usertype and the extension of .dat. Here is what your file should look like:
DateTime
Next, copy this file to the following directory: <Visual Studio Directory>\Common7\IDE (for example, C:\Program Files\Microsoft Visual Studio .NET 2003\Common7\IDE).
Before Visual Studio will read this file, you need to restart the IDE; after the restart, you can open up a C++ source code file, and you should see DateTime colored as a keyword. You can then add additional keywords, each on its own line, and the IDE will pick these words up and treat them as keywords. Customizing the words that are colored is a great way to get confirmation that you typed the correct word and also increases the readability of your code.
If you wanted all of your classes to be colored, it would take a quite a bit of time to add all of them to the usertype.dat file. Steve King has written a nice add-in that will look through your entire solution, add all of your classes to a temporary usertype.dat file, then load that file with Visual Studio. This way, you can have all of your classes colored without the labor of doing it yourself. The only downside to this add-in is that this file is not saved when you close the IDE, so you will need to run this command every time you open the IDE. (It was written this way to avoid overwriting any changes to the original
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Edit the Most Recent File and Project Lists
The list of recently used files is a good place to go back to the projects you've been working with a lot. You can tweak this list to your liking and make it even better.
Visual Studio maintains a list of the most recently opened file and project files. Whenever you open a file or project, it is added to the top of the most recently opened list. Visual Studio then uses the most recent file and project lists for a number of different purposes. The first is to populate two menu items that list the entire recent file and project lists; this is shown in Figure 2-27.
Figure 2-27: Recent Files list
The second use is to display recent projects on the default Visual Studio .NET 2003 start page, which is shown in Figure 2-28.
Figure 2-28: Visual Studio .NET 2003 start page
By default, both of these lists display only four (or eight in Visual Studio 2005) of the most recent projects or files. By navigating to Tools Options General, you can set the number of items shown in these lists under the option called "Display n items in most recently used lists." This option can be seen in Figure 2-29.
Figure 2-29: Number of items to display in most recently used lists
You can set the number of items to any number between 1 and 24. When you click OK, you will immediately see the new files and projects in Visual Studio.
There is no place in Visual Studio to edit either of these lists, but thankfully Microsoft has released a power toy called VSTweak to do so.
A power toy is a small utility released after the full release of an application. It is usually written by the some of the same people who worked on the original application and often includes features or functionality that they could not get done in time to build into the original application. You can find power toys for most Microsoft applications including, but not limited to, Visual Studio, Windows, OneNote, and more. Power toys are not supported by Microsoft.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Refactor Your Code
Visual Studio 2005 puts this fundamental tenet of Extreme Programming into action. Use it to write better code.
Refactoring is a technique for improving a section of code by modifying its internal structure, but without affecting its external behavior. Refactoring is one of the key tenets of Extreme Programming; in a rapid development methodology, the idea is to create code quickly and then refactor it as you work with it. The goal of Extreme Programming is to create better quality code by concentrating on small deliverables and continuously testing and refactoring your code. Refactoring is not just limited to Extreme Programming—it is a valuable practice regardless of the methodology you happen to subscribe to. Visual Studio 2005 introduces a new Refactor menu. This menu is available whenever you right-click and provides a number of time-saving and code-improving functions.
Refactoring is almost exclusively a C# feature. The only part of the Refactor menu that is available in VB.NET is the Rename function.
Figure 2-31 shows the menu and the various functions included there.
Figure 2-31: Refactor menu
The first function available in the Refactor menu is Extract Method. This can be used to select a block of code and then extract that code into a separate method. This function has a number of applications, such as removing a section of code that deserves to be in its own method for code reuse purposes. Here is some existing code:
public class Car
{
    private bool _isStarted = false;
    private bool _hasFuel = true;
  
    public Car( )
    {
        //Start the Car
         if (_hasFuel)
               {
                 _isStarted = true;
               }
    }
}
That example has a little bit of code in the constructor that checks to see if the car has fuel and then "starts" the car by setting a Boolean value. If you decide that this class needs a second constructor, instead of duplicating this tiny piece of logic, you could extract this into a separate
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Use and Share Code Snippets
Tap into a whole universe of other people's code and share some of your own.
Visual Studio 2005 introduces a new feature called Code Snippets that allows you to insert a piece of example code directly into the class you are working with. At the time of this writing, this feature is available only for Visual Basic.
You can quickly insert example pieces of code by right-clicking in the area where you want to insert code, then clicking the Insert Snippet item on the context menu that appears. A list of code categories appears, as shown in Figure 2-39.
Figure 2-39: Code Snippet categories
After you select the category Accessing Data, you will see the list of examples that you can automatically insert, as shown in Figure 2-40.
Figure 2-40: Accessing Data example list
You can then select the "Create a DataTable with columns and a primary key" snippet, and the following code will be inserted into the class:
Private Sub CreateTable( )
    Dim IDColumn As New DataColumn("ID")
    IDColumn.DataType = GetType(Integer)
    IDColumn.AutoIncrement = True
  
    Dim FNameColumn As New DataColumn("FirstName")
    FNameColumn.DataType = GetType(String)
  
    Dim LNameColumn As New DataColumn("LastName")
    LNameColumn.DataType = GetType(String)
  
    Dim EmployeeTable As New DataTable("Employees")
    EmployeeTable.Columns.Add(IDColumn)
    EmployeeTable.Columns.Add(FNameColumn)
    EmployeeTable.Columns.Add(LNameColumn)
  
    EmployeeTable.Constraints.Add("Key1", IDColumn, True)
End Sub
Of course, this code probably does not do exactly what you want it to do, but it gives you a great start to work from. Code Snippets are a great way to incorporate examples into your everyday coding, but wouldn't it be great if you could build your own library of code snippets?
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Chapter 3: Navigating Visual Studio
Visual Studio is a rich development environment including windows, toolbars, commands, and editors. Knowing how to quickly and easily move around the development environment makes developing applications that much easier. Being able to quickly and easily find the information you are looking for is a key part of learning how to best work with Visual Studio, whether that information is a piece of source code or a property for a control. Any time spent fumbling around the IDE is time wasted.
The hacks in this chapter cover how to arrange and work with windows, find files quickly, search your files using incremental search and regular expressions, and navigate your source code using shortcuts and task list shortcuts. You'll also learn about a power toy that assigns commands to different mouse buttons and an add-in that enhances the tabs in the document window.
Dock, float, and size your windows, and save a layout for every mode and mood.
Visual Studio contains many ways to customize your workspace. Every window can be moved, docked, or hidden. Visual Studio includes different window layouts for different modes; modifying a window layout during debug mode will not affect your window layout while coding. There is even a full-screen mode. This hack delves into all the ways you can take control of your workspace.
Visual Studio provides a plethora of windows that you can move around the IDE. Windows have a number of different states; they can be floating, docked, hidden, or in auto hide mode. Understanding these different states and how changes you make are saved is important to getting the most out of the Visual Studio IDE.
Window state is controlled by right-clicking on the window titlebar and selecting the state (see Figure 3-1) or by selecting a window option while the focus is on the window you want to change. Each of these states affects how the window behaves.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Hacks 16-23
Visual Studio is a rich development environment including windows, toolbars, commands, and editors. Knowing how to quickly and easily move around the development environment makes developing applications that much easier. Being able to quickly and easily find the information you are looking for is a key part of learning how to best work with Visual Studio, whether that information is a piece of source code or a property for a control. Any time spent fumbling around the IDE is time wasted.
The hacks in this chapter cover how to arrange and work with windows, find files quickly, search your files using incremental search and regular expressions, and navigate your source code using shortcuts and task list shortcuts. You'll also learn about a power toy that assigns commands to different mouse buttons and an add-in that enhances the tabs in the document window.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Take Control of Your Workspace
Dock, float, and size your windows, and save a layout for every mode and mood.
Visual Studio contains many ways to customize your workspace. Every window can be moved, docked, or hidden. Visual Studio includes different window layouts for different modes; modifying a window layout during debug mode will not affect your window layout while coding. There is even a full-screen mode. This hack delves into all the ways you can take control of your workspace.
Visual Studio provides a plethora of windows that you can move around the IDE. Windows have a number of different states; they can be floating, docked, hidden, or in auto hide mode. Understanding these different states and how changes you make are saved is important to getting the most out of the Visual Studio IDE.
Window state is controlled by right-clicking on the window titlebar and selecting the state (see Figure 3-1) or by selecting a window option while the focus is on the window you want to change. Each of these states affects how the window behaves.
Figure 3-1: Choosing the window state

Section 3.2.1.1: Dockable

This is the default state for most windows. When a window is set to be dockable, it can either float above other windows or be docked to the edges of the IDE window. To dock a window, you simply need to drag that window and, following the bold outline, choose where you want the window to be docked. When a window is docked, it will resize with and react to the other windows around it (including the IDE main window). Visual Studio 2005 includes a new method for docking that makes it easier to determine where that window will actually be docked. Figure 3-2 shows an example of the new docking interface.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Find Files Quickly
Visual Studio's Solution Explorer gives you a quick view of your files, but you'll be able to search through thousands of files quickly with a free add-in.
The Solution Explorer shows you all of the files in your solution in an easy-to-use tree. Each project is a node, and under that node is any number of files and folders. The Solution Explorer usually works perfectly; however, it starts to fall short when you have a lot of projects and files. If your solution has six projects and 500 files, finding one of those files starts to become quite a task.
A free add-in that provides a new interface for finding files anywhere in your solution is available. It is called the VS File Finder and can be downloaded from the following link:
http://www.zero-one-zero.com/vs
The first step is to download the add-in and run the installation file. The installation does not include any choices and can be completed by simply launching the .msi file. Once the add-in is installed, it will show up as a new window the next time you start Visual Studio. If it does not automatically appear or if you'd like to show the window again after you close it, you need to click View VS File Finder, and the window will be displayed. Figure 3-6 shows the VS File Finder interface, which includes a list of all the files in all the projects in my solution.
Figure 3-6: VS File Finder window
The text box at the top of the window lets you enter any number of characters—the list below will be filtered based on what is entered in the window. Unlike the IntelliSense provided by the command window, this window will search for the entered string in any part of the filename. Figure 3-7 shows the string "Collection" entered in the text box and the filtering that the tool provides.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Search Files Quickly
Visual Studio's default Find function is pretty run-of-the-mill. Perk it up with incremental search.
Most users of Visual Studio are already familiar with the normal Find functionality, which lets you enter a search term, and then Visual Studio will look for that term in your current file. There is another type of search, an incremental search, that lets you enter your search term character by character while Visual Studio actively finds the first instance of that string in your current document. You can then quickly jump to each instance that is found in your document. All of this can be done with just a couple of quick keystrokes.
The first keystroke you need to remember is Ctrl-I (Edit.IncrementalSearch). First, you will need to open a document, then press this keystroke. This will turn incremental search on, which turns your cursor into what can only be described as a pair of binoculars with a down arrow. The next thing you need to do is simply start typing your search term character by character. Visual Studio will jump to the first match for that character combination and continue to search when you enter more characters. There is no text box area to type in; just start typing and you will see Visual Studio react. Figure 3-11 shows incremental search in action. After having typed in the string "Car", you can see that it is now selected.
Figure 3-11: Incremental search
Visual Studio has highlighted the first instance of this search term, but you may be looking for a different instance of this term. You can simply press Ctrl-I again and Visual Studio will jump to the next matching instance in this document. You can also step backward by pressing Ctrl-Shift-I (Edit.ReverseIncrementalSearch). This keystroke will jump back to the previous matching instance in the document.
You can cancel your search at any time by pressing Esc or clicking anywhere with the mouse.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Search for Patterns in Your Files
Unleash the power of regular expressions to make everyday searches and Find and Replace operations more powerful.
Regular expressions are an extremely versatile text-matching language that gives you incredible power when searching your documents and when used with replace operations, can greatly assist with repetitive changes to blocks of code.
The basic regular expression search is easily done. You simply open the Find dialog through the Edit Find and Replace Find menu or with Ctrl-F (Edit.Find). Enable regular expression searching by ensuring the Use checkbox is selected and the drop-down list has Regular Expressions selected, as shown in Figure 3-12.