automatically opens the menu and selects the menu item ready for editing. As you can imagine, this
is an incredibly useful way of navigating your form layouts, and it can often provide a shortcut for
locating wayward items.
fiGure 4-20
70 .
chaPter 4 The ViSuAl STudio WorkSpAce
The Document Outline window has more functionality when used in Control Outline mode than
just a simple navigation tool. Right-clicking an entry gives you a small context menu of actions that
can be performed against the selected item. The most obvious is to access the Properties window.
One tedious chore is renaming components after you’ve added them to the form. You can select each
one in turn and set its Name property in the Properties window, but using the Document Outline
window you can simply choose the Rename option in the context menu and Visual Studio will
automatically rename the component in the design code, thus updating the Name property for you
without your needing to scroll through the Properties list.
Complex form design can sometimes produce unexpected results. This often happens when a
component is placed in an incorrect or inappropriate container control. In such a case you’ll need
to move the component to the correct container. Of course, you have to locate the issue before you
even know that there is a problem.
The Document Outline window can help with both of these activities. First, using the hierarchical
view, you can easily locate each component and check its parent container elements. The example
shown in Figure 4 - 20 indicates that the TreeView control is in Panel1, which in turn is in
SplitContainer, which is itself contained in a ContentPanel object. In this way you can easily
determine when a control is incorrectly placed on the form ’s design layout.
When you need to move a component it can be quite tricky to get the layout right. In the Document
Outline window it’s easy. Simply drag and drop the control to the correct position in the hierarchy.
For example, dragging the TreeView control to Panel2 results in its sharing the Panel2 area with the
ListView control.
You also have the option to cut, copy, and paste individual elements or whole sets of containers and
their contents by using the right-click context menu. The copy-and-paste function is particularly
useful, because you can duplicate whole chunks of your form design in other locations on the form
without having to use trial and error to select the correct elements in the Design view, or resort to
duplicating them in the code-behind in the Designer.vb
file.
When you cut an item, remember to paste it immediately into the destination
location.
reorGanizinG tool windows
The Visual Studio IDE has been designed to be very customizable to enable you to position tool
windows such that you can be the most productive and can make full use of your available screen
real estate. You can dock tool windows, have them floating, or minimize them to the edge of the
IDE displaying only their tab using auto hide.
When dragging a tool window around, a series of guides are displayed to help you move the tool
window to a docked position. Drag the tool window onto a guide to dock the window. Dragging
over a part of a guide highlights the part of the IDE that the tool window would be docked to if you
were to drop it there (as shown in Figure 4-21).
summary .
71
fiGure 4-21
To float a docked tool window simply click and drag it to a new position (making sure not to drop
it on top of one of the guides that appears). Pressing the Ctrl key while moving the window will
prevent the guides from appearing and the window from snapping to them. When a tool window is
docked and part of a tab group (that is, windows that occupy the same space and can be switched
between by clicking their tabs), clicking and dragging the tab for the tool window moves just that
window, whereas clicking and dragging the title bar for the tool window moves the entire tab group.
To access a tool window that is set to auto hide, put your mouse over its tab to make it slide out. To
put a tool window into auto hide mode, click the pushpin button in the title bar for the window, and
click it again while in the auto hide mode to return it to its docked position.
After dragging a tool window out of its docked position and moving it elsewhere
(such as onto another monitor), simply double-click its title bar while holding the
Ctrl key to return it to its previously docked position.
suMMary
In this chapter you have seen a number of tool windows that can help you not only write code but
also prototype and try it out. Making effective use of these windows will dramatically reduce the
number of times you have to run your application in order to test the code you are writing. This,
in turn, will improve your overall productivity and eliminate idle time spent waiting for your
application to run.
5 5
find and replace and Help
what’s in this chaPter?
.
Using Visual Studio’s various Find and Replace tools
.
Navigating Visual Studio’s local help system
To be a productive developer, you need to be able to navigate your way around a code base and
find what you need quickly. Visual Studio 2010 provides not just one but a number of search
functions, each suited to particular searching tasks. The first part of this chapter discusses
each of these search functions and when and where to use them.
Visual Studio 2010 is an immensely complex development environment that encompasses
multiple languages based on an extensive framework of libraries and components. You will
find it almost impossible to know everything about the IDE, let alone each of the languages
or even the full extent of the .NET Framework. As both the .NET Framework and Visual
Studio evolve, it becomes increasingly difficult to stay abreast of all the changes; moreover, it
is likely that you need to know only a subset of this knowledge. Of course, you’ll periodically
need to obtain more information on a specific topic. To help you in these situations, Visual
Studio 2010 comes with comprehensive documentation in the form of the MSDN Library,
Visual Studio 2010 Edition. The second part of this chapter walks you through the methods of
researching documentation associated with developing projects in Visual Studio 2010.
quick find/rePlace
The simplest means of searching in Visual Studio 2010 is via the Quick Find dialog.
The find-and-replace functionality in Visual Studio 2010 is split into two broad tiers with a
shared dialog and similar features: Quick Find, and the associated Quick Replace, are for
searches that you need to perform quickly on the document or project currently open in the
IDE. These two tools have limited options to filter and extend the search, but as you’ll see in a
74 . chaPter 5 Find And replAce And help
moment, even those options provide a powerful search engine that goes beyond what you ’ ll fi nd
in most applications.
quick find
Quick Find is the term that Visual Studio 2010 uses to refer to the most basic search functionality.
By default, it enables you to search for a simple word or phrase within the current document, but
even Quick Find has additional options that can extend the search beyond the active module,
or even incorporate wildcards and regular expressions in the search criteria.
To start a Find action, press the standard keyboard shortcut Ctrl+F
or select Edit Find and Replace Quick Find. Visual Studio will display
the basic Find and Replace dialog, with the default Quick Find action
selected (see Figure 5 - 1).
Type the search criteria into the Find what textbox, or select from
previous searches by clicking the drop - down arrow and scrolling
through the list of criteria that have been used. By default, the scope
of the search is restricted to the current document or window you ’ re
editing, unless you have a number of lines selected, in which case
the default scope is the selection. The Look in drop - down list gives you additional options based
on the context of the search itself, including Selection, Current Block, Current Document, Current
Project, Entire Solution, and All Open Documents.
Find - and - replace actions will always wrap around the selected scope looking for the search terms,
stopping only when the fi nd process has reached the starting point again. As Visual Studio fi nds
each result, it highlights the match and scrolls the code window so you can view it. If the match is
already visible in the code window, Visual Studio does not scroll the code. Instead, it just highlights
the new match. However, if it does need to scroll the window, it attempts to position the listing
so the match is in the middle of the code editor window.
In the Standard toolbar there is a Quick Find box, as shown in Figure 5 - 2.
This box actually has multiple purposes. The keyboard shortcut Ctrl+/
will place focus on the box. You can then enter a search phrase and press
Enter to fi nd the next match in the currently open fi le. If you prefi x what
fiGure 5 - 1
fiGure 5 - 2
This search tool is best suited for when you need to do a simple text - based
search/replace (as opposed to searching for a symbol).
Once you have performed the fi rst Quick Find search, you no longer need the
dialog to be visible. You can simply press F3 to repeat the same search.
Quick find/replace .
75
you type with >, Visual Studio 2010 attempts to execute the command as if it had been entered into
the Command window (see Chapter 4 for more information).
quick replace
Performing a Quick Replace is similar to performing a Quick Find.
You can switch between Quick Find and Quick Replace by clicking
their respective buttons at the top of the dialog window. If you want
to go directly to Quick Replace, you can do so with the keyboard
shortcut Ctrl+H or the menu command Edit Find and Replace Quick
Replace. The Quick Replace options (see Figure 5-3) are the same
as those for Quick Find, but with an additional field where you can
specify what text should be used in the replacement.
The Replace With field works in the same way as Find What — you
can either type a new replacement string or, with the drop-down list
provided, choose any you’ve previously entered.
fiGure 5-3
A simple way to delete recurring values is to use the replace functionality with
nothing specified in the Replace With text area. This enables you to find all
occurrences of the search text and decide if it should be deleted.
find options
Sometimes you will want to filter the search results in different ways,
and that’s where the find options come into play. First, to display the
options section (available in all find-and-replace actions), click the
expand icon next to Find Options. The dialog will expand to show
a set of checkbox options and drop-down lists from which you can
choose, as shown in Figure 5-4.
These options enable you to refine the search to be case-sensitive
(Match Case) or an exact match (Match Whole Word). You can also
change the direction of the search (Search Up), and specify that you
are performing a more advanced search that is using wildcards or
regular expressions.
wildcards
Wildcards are simple text symbols that represent one or more characters, and are familiar to
many users of Windows applications. Figure 5-5 illustrates the Expression Builder when the
wildcard option is specified under the Use drop-down. Although additional characters can be
fiGure 5-4
76 .
chaPter 5 Find And replAce And help
used in a wildcard search, the most common
characters are ? for a single character and *
for multiple characters that are unknown or
variable in the search.
regular expressions
Regular expressions take searching to a whole
new level, with the capability to do complex text
matching based on the full RegEx engine built
into Visual Studio 2010. Although this book
doesn’t go into great detail on the advanced
matching capabilities of regular expressions, it’s
worth mentioning the additional help provided
by the Find and Replace dialog if you choose to
use them in your search terms.
Figure 5-6 again shows the Expression Builder,
this time for building a regular expression as
specified in the Use drop-down. From here you
can easily build your regular expressions with a
menu showing the most commonly used regular
expression phrases and symbols, along with
English descriptions of each.
An example of where using regular expressions
might come in handy is when reversing
assignments. For example, if you have
this code:
Vb
Description = product.Description
Quantity = product.Quantity
SellPrice = product.SellPrice
c#
Description = product.Description;
Quantity = product.Quantity;
SellPrice = product.SellPrice;
and want to reverse the assignments like so:
Vb
product.Description = Description
product.Quantity = Quantity
product.SellPrice = SellPrice
fiGure 5-5
fiGure 5-6
Quick find/replace .
77
c#
product.Description = Description;
product.Quantity = Quantity;
product.SellPrice = SellPrice;
this would be a perfect use for performing a Quick Replace with regular expressions rather than
modifying each line of code manually. Ensure you select regular expressions in the find options, and
enter the following as the “text” to find:
Vb
{ < .*} = {.*}
c#
{ < .*} = {.*};
and the following as the replace with “text”:
Vb
\2 = \1
c#
\2 = \1;
As a brief explanation, you are searching for two groups (defined by the curly brackets) separated
by an equals sign. The first group is searching for the first character of a word (<) and then any
characters (.*). The second group is searching for any characters until an end-of-line character is
found in the VB example or a semicolon is found in the C# example. Then when you do the replace,
you are simply inserting the characters from the second group found in its place, an equals sign