solution. Visual Studio 2010 will happily open the file, but
you will have to reopen it every time you open the solution.
Alternatively, if you enable Environment Documents Show fiGure 3-20
Miscellaneous Files in Solution Explorer via the Options
dialog, the file will be temporarily added to the solution. The miscellaneous files folder to which
this file is added is shown in Figure 3-20.
Visual Studio 2010 will automatically manage the list of miscellaneous files,
keeping only the most recent ones, based on the number of files defined in the
Options dialog. You can get Visual Studio to track up to 256 files in this list, and
files will be evicted based on when they were last accessed.
build and run
The Projects and Solutions . Build and Run node, shown in Figure 3-21, can be used to tailor
the build behavior of Visual Studio 2010. The first option to notice is Before Building. With the
default option of Save All Changes, Visual Studio will apply any changes made to the solution
prior to compilation. In the event of a crash during the build process or while you’re debugging
the compiled code, you can be assured that your code is safe. You may want to change this option
to Prompt to Save All Changes if you don’t want changes to be saved prematurely, though this is
not recommended. This setting will inform you of unsaved modifications made in your solution,
enabling you to double-check those changes prior to compilation.
50 .
chaPter 3 opTionS And cuSTomizATionS
fiGure 3-21
To reduce the amount of time it takes to build your solution, you may want to increase the
maximum number of parallel builds that are performed. Visual Studio 2010 can build in parallel
only those projects that are not dependent, but if you have a large number of independent projects
this might yield a noticeable benefit. Be aware that on a single-core or single-processor machine this
may actually increase the time taken to build your solution.
Figure 3-21 shows that projects will Always Build when they are out of date, and that if there are
build errors the solution will not launch. Both these options can increase your productivity, but be
warned that they eliminate dialogs letting you know what’s going on.
The last option worth noting in Figure 3-21 is MSBuild project build output
verbosity. In most cases the Visual Studio 2010 build output is sufficient for
debugging build errors. However, in some cases, particularly when building
ASP.NET projects, you will need to increase verbosity to diagnose the build
error. New to this version of the Visual Studio 2010 is the ability to control the
log file verbosity independently of the output.
Vb options
VB programmers have four compiler options that can be configured at a project or a file level. You
can also set the defaults on the Projects and Solutions . VB Defaults node of the Options dialog.
Previous versions of VB had an Option Explicit, which forced variables to be defined prior to their
use in code. When it was introduced, many experts recommended that it be turned on permanently
because it did away with many runtime problems in VB applications that were caused by improper
use of variables.
Option Strict takes enforcing good programming practices one step further by forcing developers to
explicitly convert variables to their correct types, rather than let the compiler try to guess the proper
conversion method. Again, this results in fewer runtime issues and better performance.
iMPortinG and exPortinG settinGs
Once you have the IDE in exactly the confi guration you want, you may want to back up the settings
for future use. You can do this by exporting the IDE settings to a fi le that can then be used to
restore the settings or even transfer them to a series of Visual Studio 2010 installations, so that they
all share the same IDE setup.
We advise strongly that you use Option Strict to ensure that your code is not
implicitly converting variables inadvertently. If you are not using Option Strict,
with all the new language features, you may not be making the most effective use
of the language.
The Environment . Import and Export Settings node in the Options dialog
enables you to specify a team settings fi le. This can be located on a network
share, and Visual Studio 2010 will automatically apply new settings if the fi le
changes.
To export the current confi guration, select
Tools . Import and Export Settings to start
the Import and Export Settings Wizard,
shown in Figure 3 - 22. The fi rst step in the
wizard is to select the Export option and
which settings are to be backed up during
the export procedure.
As shown in Figure 3 - 22, a variety of
grouped options can be exported. The
screenshot shows the Options section
expanded, revealing that the Debugging and
Projects settings will be backed up along with
the Text Editor and Windows Forms Designer
confi gurations. As the small exclamation
icons indicate, some settings are not included
in the export by default, because they
contain information that may infringe on
your privacy. You will need to select these sections manually if you want them to be included in the
backup. Once you have selected the settings you want to export, you can progress through the rest
of the wizard, which might take a few minutes depending on the number of settings being exported.
Importing a settings fi le is just as easy. The same wizard is used, but you select the Import option on
the fi rst screen. Rather than simply overwriting the current confi guration, the wizard enables you to
back up the current setup fi rst.
fiGure 3 - 22
importing and exporting settings . 51
52 .
chaPter 3 opTionS And cuSTomizATionS
You can then select from a list of preset configuration files — the same set of files from which you
can choose when you first start Visual Studio 2010 — or browse to a settings file that you created
previously. Once the settings file has been chosen, you can then choose to import only certain
sections of the configuration, or import the whole lot.
The wizard excludes some sections by default, such as External Tools or Command Aliases, so that
you don’t inadvertently overwrite customized settings. Make sure you select these sections if you
want to do a full restore.
If you just want to restore the configuration of Visual Studio 2010 to one of
the default presets, you can choose the Reset All Settings option in the opening
screen of the wizard, rather than go through the import process.
suMMary
This chapter covered only a core selection of the useful options available to you as you start to shape
the Visual Studio interface to suit your own programming style; many other options are available.
These numerous options enable you to adjust the way you edit your code, add controls to your
forms, and even select the methods to use when debugging code.
The settings within the Visual Studio 2010 Options page also enable you to control how and where
applications are created, and even to customize the keyboard shortcuts you use. Throughout the
remainder of this book, you’ll see the Options dialog revisited according to specific functionality
such as compiling, debugging, and writing macros.
4
The Visual studio Workspace
what’s in this chaPter?
.
Using the code editor
.
Exploring the core Visual Studio tool windows
.
Reorganizing your workspace
So far you have seen how to get started with Visual Studio 2010 and how to customize the
IDE to suit the way that you work. In this chapter, you learn to take advantage of some of the
built-in commands, shortcuts, and supporting tool windows that will help you to write code
and design forms.
the code editor
As a developer you’re likely to spend a considerable portion of your time writing code, which
means that knowing how to tweak the layout of your code and being able to navigate it
effectively are particularly important. Visual Studio 2010 introduces an all new WPF-based
code editor that provides numerous new features from what was available in previous versions
of Visual Studio.
the code editor window layout
When you open a code file for editing you are working in the code editor window, as shown
in Figure 4-1. The core of the code editor window is the code pane in which the code is
displayed.
54 .
chaPter 4 The ViSuAl STudio WorkSpAce
fiGure 4-1
Above the code pane are two drop-down lists that can help you navigate the code file. The first
drop-down lists the classes in the code file, and the second one lists the members of the selected
class in the first drop-down. These are listed in alphabetical order, making it easier to find a method
or member definition within the file.
As you modify the code in the code editor window, lines of code that you’ve modified since the file
has been opened are marked in the left-hand margin (which can be seen in Figure 4-2) — yellow for
unsaved changes and green for those that have been saved.
regions
Effective class design usually results in classes that serve a single purpose and are not overly complex
or lengthy. However, there will be times when you have to implement so many interfaces that your
code file will become unwieldy. In this case, you have a number of options, such as partitioning the
code into multiple files or using regions to condense the code, thereby making it easier to navigate.
The introduction of partial classes (where the definition of a class can be split over two or more
files) means that at design time you can place code into different physical files representing a
single logical class. The advantage of using separate files is that you can effectively group all methods
that are related, for example, methods that implement
an interface. The problem with this strategy is that
navigating the code then requires continual switching
between code files.
An alternative is to use named code regions to condense
sections of code that are not currently in use. In Figure 4-2
you can see that two regions are defined, Constructor
and IComparable. Clicking the minus sign next to
#Region
condenses the region into a single line and
clicking the plus sign expands it again.
fiGure 4-2
outlining
In addition to regions that you have defi ned, Visual Studio 2010 has the ability to auto - outline
your code, making it easy to collapse methods, comments, and class defi nitions. Auto - outlining
is enabled by default, but if it ’ s not enabled you can enable it using the Edit . Outlining . Start
Automatic Outlining menu item.
Figure 4 - 3 shows four condensable regions. One is a defi ned
region called Constructor , however there are also three other
automatic regions, outlining the class, the XML comments, and
the constructor method (which has been collapsed). Automatic
outlines can be condensed and expanded in the same way as
regions you defi ne manually.
The Edit . Outlining menu provides a number of commands
to help in toggling outlining, such as collapsing the entire fi le to
just method/property defi nitions (Edit . Outlining . Collapse to Defi nitions) and expanding it to
display all collapsed code again (Edit . Outlining . Stop Outlining). The other way to expand and
condense regions is via the keyboard shortcut Ctrl+M, Ctrl+M. This shortcut toggles between the
two layouts.
fiGure 4 - 3
You don ’ t need to expand a region to see the code within it. Simply hover the
mouse cursor over the region and a tooltip displays the code within it.
One trick for C# developers is that Ctrl+] enables you to easily navigate from the
beginning of a region, outline, or code block to the end and back again.
code formatting
By default, Visual Studio 2010 assists you in writing readable code by automatically indenting
and aligning. However, it is also confi gurable so that you can control how your code is arranged.
Common to all languages is the ability to control what happens when you create a new line. In
Figure 4 - 4 you can see that there is a Tabs node under the Text Editor . All Languages node of the
Options dialog. Setting values here defi nes the default value for all languages, which you can then
overwrite for an individual language using the Basic . Tabs node (for VB.NET), C# . Tabs, or
other language nodes.
By default, the indenting behavior for both C# and VB.NET is smart indenting, which will, among
other things, automatically add indentation as you open and close enclosures. Smart indenting is not
available for all languages, in which case block indenting is used.
The Code editor . 55
56 . chaPter 4 The ViSuAl STudio WorkSpAce
Visual Studio ’ s Smart Indenting does a pretty good job of automatically indenting code as it is
written or pasted into the code editor, but occasionally you can come across code that has not been
properly formatted, making it diffi cult to read. To have Visual Studio reformat the entire document
and set the brace locations and line indentations, select Edit . Advanced . Format Document or
press Ctrl+K, Ctrl+D. To reformat just the selected code block, select Edit . Advanced . Format
Selection or press Ctrl+K, Ctrl+F.
When writing code, to indent an entire block of code one level without changing each line
individually, simply select the block and press Tab. Each line will have a tab inserted at its start.
To unindent a block one level, select it and press Shift+Tab.
fiGure 4 - 4
If you are working on a small screen, you might want to reduce the tab and
indent sizes to optimize screen usage. Keeping the tab and indent sizes the same
ensures that you can easily indent your code with a single tab keypress.
What is interesting about this dialog is the degree of control C# users have over
the layout of their code. Under the VB Specifi c node is a single checkbox entitled
“ Pretty listing (reformatting) of code, ” which if enabled keeps your code looking
uniform without your having to worry about aligning methods, closures, class
defi nitions, or namespaces. C# users, on the other hand, can control nearly