every aspect of how the code editor reformats code, as you can see from the
additional nodes for C# in Figure 4 - 4 .
You may have noticed the Tabify/Untabify Selected Lines commands under
the Edit . Advanced menu and wondered how these differ from the Format
Selection command. These commands simply convert leading spaces in lines
to tabs and vice versa, rather than recalculating the indenting as the Format
Selection command does.
The Code editor .
57
navigating forward/backward
As you move within and between items, Visual Studio 2010 tracks where you have been, in much
the same way that a web browser tracks the sites you have visited. Using the Navigate Forward
and Navigate Backward items from the View menu, you can easily go back and forth between the
various locations in the project that you have made changes to. The keyboard shortcut to navigate
backward is Ctrl+–. To navigate forward again it is Ctrl+Shift+–.
additional code editor features
The Visual Studio code editor is very rich with far more features than we can cover in depth here.
However, here are a few additional features that you may find useful.
reference Highlighting
A new feature in Visual Studio 2010 is reference highlighting.
All uses of the symbol (such as a method or property) under the
cursor within its scope are highlighted (as shown in Figure 4-5).
This makes it easy to spot where else this symbol is used within
your code. You can easily navigate between the uses by
Ctrl+Shift+Up/Down.
fiGure 4-5
Code Zooming
You can use Ctrl+Mouse Wheel to zoom in and out of your code (effectively making the text
larger or smaller). This feature can be especially useful when presenting to a group to enable the
people at the back of the audience to see the code being demonstrated. The bottom left-hand
corner of the code editor also has a drop-down enabling you to select from some predefined
zoom levels.
Word Wrap
You can turn on word wrap in the code editor from the options. Go to Tools . Options, expand the
Text Editor node, select the All Languages subnode, and select the Word Wrap option. You can also
choose to display a return arrow glyph where text has been wrapped by selecting the Show Visual
Glyphs for Word Wrap option below the Word Wrap option.
You can turn this on for the current project by selecting Edit .
Advanced . Word Wrap.
line numbers
To keep track of where you are in a code file you may find it
useful to turn on line numbers in the code editor (as shown in
Figure 4-6). To turn line numbers on, go to Tools . Options,
expand the Text Editor node, select the All Languages subnode,
and select the Line Numbers option.
fiGure 4-6
58 .
chaPter 4 The ViSuAl STudio WorkSpAce
split View
Sometimes you want to view two different parts of
the same code file at the same time. Split view enables
you to do this by splitting the active code editor
window into two horizontal panes separated by a
splitter bar. These can then be scrolled separately to
display different parts of the same file simultaneously
(as shown in Figure 4-7).
To split a code editor window, select Split from the
Window menu. Alternatively, drag the handle directly
above the vertical scroll bar down to position the
splitter bar.
Drag the splitter bar up and down to adjust the size
of each pane. To remove the splitter simply double-
click the splitter bar, or select Remove Split from the Window menu.
tear away (floating) code windows
A welcome new feature in Visual Studio 2010 for those with multiple monitors is the ability to
“tear off” or float code editor windows (and tool windows) and move them outside the main Visual
Studio IDE window (as shown in Figure 4-8), including onto another monitor. This allows you to
now make use of the extra screen real-estate that having multiple monitors provides by enabling
multiple code editor windows to be visible at the same time over separate monitors. To tear off a
window, make sure it has the focus and then select Float from the Window menu. Alternatively,
right-click the title bar of the window and select Float from the drop-down menu, or simply click
and drag the tab for that window (effectively tearing it away from its docked position) and position
it where you want it to be located.
fiGure 4-7
fiGure 4-8
The Code editor .
59
You may find halving the code editor window in split view (discussed in the previous section) in order
to view different parts of a file at the same time too much of a limited view, so you might want to
use the floating code windows feature instead to open another code editor window for the same file,
and place it, say, on a different screen (if you have a multiple monitor setup). The trick to doing this
(because double-clicking the file again in the Solution Explorer simply activates the existing code editor
window instance for that file) is to select New Window from the Window menu. This will open the file
currently being viewed in another window which you can then tear away and position as you please.
creating tab Groups
If you don’t have the privilege of having more than one monitor, it is still possible to view more than
one code editor window at the same time. You do this by creating tab groups, and tiling these groups
to display at the same time. As their name would indicate, a tab group is a group of code editor
window tabs, with each tab group appearing in a separate tile. Multiple tab groups can be created,
limited only by the amount of screen real-estate they will occupy. You can choose to tile the tab
groups vertically or horizontally, although you cannot use a mix of the two.
To start this process you need to have more than one
tab open in the code editor window. Ensure a code
editor tab has the focus, then select Window . New
Horizontal Tab Group or Window . New Vertical
Tab Group from the menu displayed. This starts a
new tab group and creates a tile for it (as shown in
Figure 4-9).
Alternatively, you can simply drag a tab below or
beside an existing one and dock it to achieve the
same effect.
You can drag tabs between tab groups or move
them between tab groups using Window .
Move to
Next Tab Group and Window . Move to Previous
Tab Group. These options are also available from
the drop-down menu when right-clicking a tab.
To restore the user interface to having a single tab group again, move the tabs from the new tab
group(s) back into the original one again and the tiling will be removed.
advanced functionality
To be a truly productive developer it can help to know various advanced features available in the
code editor that are hidden away but can save you a lot of time. Here are some of the most useful
commands that aren’t immediately obvious within the code editor.
Commenting/Uncommenting a Code Block
Often you need to comment or uncomment a block of code, and you don’t want to have to add/
remove the comment characters to/from the start of each line, especially when there are many lines
in the block. Of course, in C# you could wrap the block of code between a /* and */ to comment it
fiGure 4-9
60 .
chaPter 4 The ViSuAl STudio WorkSpAce
out, but this type of comment isn’t available in Visual Basic, and it can be problematic in C# when
commenting out a block that already contains a comment using this style.
Visual Studio provides a means to comment/uncomment a block of code easily, by selecting the
block, then selecting Edit . Advanced . Comment Selection to comment it out, or selecting Edit .
Advanced . Uncomment Selection to uncomment it.
The easiest way to access these commands (you are likely to use these often) is via their shortcuts.
Press Ctrl+K, Ctrl+C to comment a block of code, and Ctrl+K, Ctrl+U to uncomment it. The Text
Editor toolbar is another simple means to access these commands.
Block selection
Also known as box selection, column selection, rectangle selection,
or vertical text selection, block selection is the ability to select text
in a block (as shown in Figure 4-10) instead of the normal behavior
of selecting lines of text (stream selection). To select a block of text,
fiGure 4-10
hold down the Alt key while selecting text with the mouse, or use
Shift+Alt+Arrow with the keyboard. This feature can come in handy when, for example, you have
code lined up and want to remove a vertical portion of that code (such as a prefix on variable
declarations).
Multiline editing
Multiline editing is a new feature available in Visual Studio 2010 that extends the abilities of block
selection. In previous versions, after selecting a vertical block of text you could only delete, cut, or
copy the block. With Visual Studio 2010 you can now type after selecting a vertical block of text,
which will replace the selected text with what’s being typed on each line. This can be handy for
changing a group of variables from private to protected, for example.
You can also insert text across multiple lines by creating a block with zero width
and simply starting to type.
The Clipboard ring
Visual Studio keeps track of the last 20 snippets of text that have been copied or cut to the
clipboard. To paste text that was previously copied to the clipboard but overwritten, instead of the
normal Ctrl+V when pasting, use Ctrl+Shift+V. Pressing V while holding down Ctrl+Shift cycles
through the entries.
full-screen View
You can maximize the view for editing the code by selecting View . Full Screen, or using the
Shift+Alt+Enter shortcut. This effectively maximizes the code editor window, hiding the other tool
windows and the toolbars. To return to the normal view, press Shift+Alt+Enter again or click the
Full-Screen toggle button that has been added to the end of the menubar.
The Command Window .
61
Go To Definition
To quickly navigate to the definition of the class, method, or member under the cursor, right-click .
Go To Definition, or simply press F12.
find all references
You can find where a method or property is called by right-clicking its definition and selecting
Find All References from the drop-down menu, or placing the cursor in the method definition and
pressing Shift+F12. This activates the Find Symbol Results tool window and displays the locations
throughout your solution where that method or property is referenced. You can then double-click a
reference in the results window to navigate to that result in the code editor window.
This feature has somewhat been made obsolete by the new Call Hierarchy
window, discussed later in this chapter. However, it can still be a quick way to
view where a method is used without navigating through the Call Hierarchy
window.
the coMMand window
As you become more familiar with Visual Studio 2010, you will spend less time looking for
functionality and more time using keyboard shortcuts to navigate and perform actions within the
IDE. One of the tool windows that ’s often overlooked is the Command window, accessible via
View .
Other Windows .
Command Window
(Ctrl+Alt+A). From this window you can
execute any existing Visual Studio command
or macro, as well as any additional macros
you may have recorded or written. Figure 4-11
illustrates the use of IntelliSense to show the
list of commands that can be executed from
the Command window. This list will include all
macros defined within the current solution.
A full list of the Visual Studio commands is available via the Environment . Keyboard node of the
Options dialog (Tools . Options). The commands all have a similar syntax based on the area of the
IDE that they are derived from. For example, you can open the debugging output window (Debug .
Windows . Output) by typing Debug.Output into the Command window.
The commands fall into three rough groups. Many commands are shortcuts to either tool windows
(which are made visible if they aren ’t already open) or dialogs. For example, File.NewFile opens
the new fi le dialog. Other commands query information about the current solution or the debugger.
Using Debug.ListThreads lists the current threads, in contrast to Debug.Threads, which opens
the Threads tool window. The third type includes those commands that perform an action without
fiGure 4-11
62 .
chaPter 4 The ViSuAl STudio WorkSpAce
displaying a dialog. This would include most macros and a number of commands that accept
arguments (a full list of these, including the arguments they accept, is available within the MSDN
documentation). There is some overlap between these groups: for example, the Edit.Find command
can be executed with or without arguments. If this command is executed without arguments, the
Find and Replace dialog is displayed. Alternatively, the following command will find all instances of
the string MyVariable in the current document (/d) and place a marker in the code window border
against the relevant lines (/m):
>Edit.Find MyVariable /m /d
Although there is IntelliSense within the Command window, you may find typing a frequently
used command somewhat painful. Visual Studio 2010 has the ability to assign an alias to a particular
command. For example, the alias command can be used to assign an alias, e?, to the find command
used previously:
>alias e? Edit.Find MyVariable /m /d
With this alias defined you can easily perform this command from anywhere within the IDE:
press Ctrl+Alt+A to give the Command Window focus, then type e? to perform the find-and-mark
command.
You will have imported a number of default aliases belonging to the environment settings when
you began working with Visual Studio 2010. You can list these using the alias command with
no arguments. Alternatively, if you want to find out what command a specific alias references, you
can execute the command with the name of the alias. For example, querying the previously defined
alias, e?, would look like the following:
>alias e?
alias e? Edit.Find SumVals /m /doc