Chapter 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 moment, even those options provide a powerful search engine that goes beyond what you'll find in most applications.

Note

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).

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).

Figure 5-1

Figure 5-1. 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 find process has reached the starting point again. As Visual Studio finds 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.

Note

Once you have performed the first Quick Find search, you no longer need the dialog to be visible. You can simply press F3 to repeat the same search.

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 find the next match in the currently open file. If you prefix what 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).

Figure 5-2

Figure 5-2. Figure 5-2

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.

Figure 5-3

Figure 5-3. Figure 5-3

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.

Note

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.

Figure 5-4

Figure 5-4. 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 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.

Figure 5-5

Figure 5-5. Figure 5-5

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

Figure 5-6. Figure 5-6

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
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 (surrounded by a space on each side), then the characters from the first group found (followed by a semicolon in the C# example). If you aren't familiar with regular expressions it may take some time to get your head around it, but it is a very quick and easy way to perform an otherwise rather mundane manual process.

Note

Note that the regular expressions used in the Quick Find tool don't have exactly the same syntax as the standard regular expressions you might find in the .NET Framework, with a few differences present between the two.

Find and Replace Options

You can further configure the find-and-replace functionality with its own set of options in the Tools

Find and Replace Options

FIND/REPLACE IN FILES

The Find in Files and Replace in Files commands enable you to broaden the search beyond the current solution to whole folders and folder structures, and even to perform mass replacements on any matches for the given criteria and filters. Additional options are available to you when using these commands, and search results can be placed in one of two tool windows so you can easily navigate them.

Note

This search tool is best suited when you need to do a simple text-based search/replace across files that are not necessarily a part of your current solution.

Find in Files

The really powerful part of the search engine built into Visual Studio is found in the Find in Files command. Rather than restrict yourself to files in the current solution, Find in Files gives you the ability to search entire folders (along with all their subfolders), looking for files that contain the search criteria.

The Find in Files dialog, shown in Figure 5-7, can be invoked via the menu command Edit

Find in Files

Most of the Quick Find options are still available to you, including wildcard and regular expressions searching, but instead of choosing a scope from the project or solution, you use the Look In field to specify where the search is to be performed. Either type the location you want to search or click the ellipsis to display the Choose Search Folders dialog, shown in Figure 5-8.

Figure 5-7

Figure 5-7. Figure 5-7

Figure 5-8

Figure 5-8. Figure 5-8

You can navigate through the entire filesystem, including networked drives, and add the folders you want to the search scope. This enables you to add disparate folder hierarchies to the one single search. Start by using the Available Folders list on the left to select the folder(s) that you would like to search. Add them to the Selected Folders list by clicking the right arrow. Within this list you can adjust the search order using the up and down arrows. Once you have added folders to the search, you can simply click OK to return a semicolon-delimited list of folders. If you want to save this set of folders for future use you can enter a name into the Folder Set drop-down and click Apply.

Note

The process of saving search folders is less than intuitive, but if you think of the Apply button as more of a Save button then you can make sense of this dialog.

Find Dialog Options

The options for the Find in Files dialog are similar to those for the Quick Find dialog. Because the search is being performed on files that are not necessarily open within the IDE or are even code files, the Search Up option is therefore not present. There is an additional filter that can be used to select only specific file types to search in.

The Look at these file types drop-down list contains several extension sets, each associated with a particular language, making it easy to search for code in Visual Basic, C#, J#, and other languages. You can type in your own extensions too, so if you're working in a non-Microsoft language, or just want to use the Find in Files feature for non-development purposes, you can still limit the search results to those that correspond to the file types you want.

In addition to the Find options are configuration settings for how the results will be displayed. For searching, you can choose one of two results windows, which enables you to perform a subsequent search without losing your initial action. The results can be quite lengthy if you show the full output of the search, but if you're interested only in finding out which files contain the information you're looking for, check the Display Filenames Only option and the results window will be populated with only one line per file.

Results Window

When you perform a Find in Files action, results are displayed in one of two Find Results windows. These appear as open tool windows docked to the bottom of the IDE workspace. For each line that contains the search criteria, the results window displays a full line of information, containing the filename and path, the line number that contained the match, and the actual line of text itself, so you can instantly see the context (see Figure 5-9).

Figure 5-9

Figure 5-9. Figure 5-9

Along the edge of each results window is a small toolbar, as shown in Figure 5-10 (left), for navigation within the results themselves. These commands are also accessible through a context menu, as shown in Figure 5-10 (right).

Figure 5-10

Figure 5-10. Figure 5-10

Simply double-click a specific match to navigate to that line of code.

Replace in Files

Although it's useful to search a large number of files and find a number of matches to your search criteria, even better is the Replace in Files action. Accessed via the keyboard shortcut Ctrl+Shift+H or the drop-down arrow next to Quick Replace, Replace in Files performs in much the same way as Find in Files, with all the same options.

Figure 5-11

Figure 5-11. Figure 5-11

The main difference is that you can enable an additional Results option when you're replacing files. When you're performing a mass replacement action like this, it can be handy to have a final confirmation before committing changes. To have this sanity check available to you, select the Keep Modified Files Open After Replace All checkbox (shown at the bottom of Figure 5-11).

Note that this feature works only when you're using Replace All; if you just click Replace, Visual Studio will open the file containing the next match and leave the file open in the IDE anyway.

Note

Important: If you leave this option unchecked and perform a mass replacement on a large number of files, they will be changed permanently without your having any recourse to an undo action. Be very sure that you know what you're doing.

Whether or not you have this option checked, after performing a Replace All action, Visual Studio reports back to you how many changes were made. If you don't want to see this dialog box, you have an option to hide the dialog with future searches.

FIND SYMBOL

The Find Symbol search tool enables you to search for a class, method, property, or other types of symbols. Whereas the standard Quick Find function is essentially a plaintext search across your selected scope (current document, current project, and so on), Find Symbol is limited to searching only for symbols.

You can invoke the Find Symbol dialog by the keyboard shortcut Alt+F12 or the menu command Edit

FIND SYMBOL
Figure 5-12

Figure 5-12. Figure 5-12

The Find Symbol dialog (see Figure 5-12) has slightly different options from the dialogs for the other Find actions. Rather than having its scope based on a current document or solution like Quick Find, or on the filesystem like Find in Files, Find Symbol can search through your whole solution, a full component list, or even the entire .NET Framework. In addition, you can include any references added to the solution as part of the scope. To create your own set of components in which to search, click the ellipsis next to the Look in field and browse through and select the .NET and COM components registered in the system, or browse to files or projects.

Note

The Find options are also simplified. You can search only for whole words, substrings (the default option), or prefixes.

After you click Find All, the search results are compiled and presented in a special tool window entitled Find Symbol Results. By default, this window shares space with the Find Results windows at the bottom of the IDE, and displays each result with any references to the particular object or component. This is extremely handy when you're trying to determine where and how a particular object is used or referenced from within your project.

Note

This search tool is best suited for when you need to search for all instances of a symbol and retrieve a list of all matches within the selected scope so you can easily navigate to a number of the results. By limiting the search scope to only symbols, you aren't searching extraneous text such as comments, code within methods, and so on. The search is also not limited to just your code but can also search the .NET Framework and referenced assemblies.

NAVIGATE TO

Navigate To is a powerful new search tool in Visual Studio 2010, providing an alternative to the standard find functions when searching for symbols. Like Find Symbol, you are limited to only searching for symbols; a number of differences between this and how Find Symbol operates can make this more useful.

As opposed to Find Symbol, Navigate To displays live results as you type the search text. The more of the search text you type, the more the results are narrowed down. Double-clicking one of the results closes the dialog and navigates to that result.

One of the most unique features of the Navigate To dialog, however, is in how it searches. Say you are looking for a class named ProductSummary. In this search tool, spaces are essentially AND operators, so typing prod sum as the search text (that is, searching for prod and sum in the same symbol name) returns the ProductSummary class as a result, as would typing in sum prod.

Figure 5-13

Figure 5-13. Figure 5-13

The other unique search capability that it has is its camel case searching. To find the ProductSummary class you can simply search for PS (the capitals in its name) to return it as a result (as shown in Figure 5-13) — a very powerful feature found only in this search tool.

If you enter the text to search for in lowercase, the matching will be non-case-specific. However, if you enter an uppercase character as a part of the search text, the search will become case-specific.

The shortcut to open the Navigate To dialog is Ctrl+, (comma).

Note

This search tool is best suited for when you need to search for and navigate to a single instance of a symbol, with the benefits of "live" results as you type and its partial/camel case search capabilities.

INCREMENTAL SEARCH

If you're looking for something in the current code window and don't want to bring up a dialog, the Incremental Search function might be what you need. Invoked by either the Edit

INCREMENTAL SEARCH

Immediately after invoking Incremental Search, simply begin typing the text you need to find. The mouse pointer will change to a set of binoculars and a down arrow. As you type each character, the editor will move to the next match for the text you entered. For example, typing f would find the first word containing an f — such as offer. Typing an o would then move the cursor to the first word containing fo — such as form; and so on.

If you enter the text to search for in lowercase, the matching will be non-case-specific. However, if you enter an uppercase character as a part of the search text, the search will become case-specific.

Note

This search tool is really mostly for use in the same situations as you might use the Quick Find tool, but it searches only the current file and doesn't have the additional options that Quick Find does. It does keep the Quick Find dialog from getting in the way if you don't require these advanced search features. However, you are better off using the Quick Find dialog if you want to find a result, make a change, then find the next result, as when finding the next result you would have to start typing the incremental search text all over again.

ACCESSING HELP

You are exposed to a wide range of technologies as a developer. Not only do they evolve at a rapid pace, but you are constantly being bombarded with additional new technologies which you must get up to speed on quickly. It's impossible to know everything about these technologies, and being a developer involves constantly learning. Often, knowing how to find information on using these technologies is as important a skill as being able to actually implement them. Luckily, there are a multitude of information sources on these technologies from which you can draw on. The inclusion of Intellisense into IDEs over a decade ago was one of the most useful tools for helping developers write code, but it's rarely a substitute for a full blown help system that provides all the ins and outs of a technology. Visual Studio's help system provides this support for developers.

The easiest way to get help for Visual Studio 2010 is to use the same method you would use for almost every Windows application ever created — press the F1 key, the universal shortcut key for help. Visual Studio 2010 has a brand new help system which uses Microsoft Help 3. Rather than using a special "shell" to host the help and enable you to navigate around and search it, the help system now runs in a browser window. To support some of the more complex features of the help system such as the search functionality (when using the offline help), there is now a help listener application that runs in your system tray and serves these requests. You'll also note that the address in the browser's address bar points to a local web server on your machine. The online and offline help modes look and behave very similarly to one another, but this chapter specifically focuses on the offline help.

Note

You may find that you receive a Service Unavailable message when using the help system. The likely cause of this error is that the help listener is no longer running in your system tray. Simply open the help system from within Visual Studio and the help listener will be automatically started again.

The help system in Visual Studio is contextual. This means that if the cursor is currently positioned on or inside a class definition in a project and you press F1, the help window will open immediately with a mini-tutorial about what the class statement is and how to use it, as shown in Figure 5-14.

Figure 5-14

Figure 5-14. Figure 5-14

This is incredibly useful because more often than not if you simply press F1, the help system will navigate directly to the help topic that deals with the problem you're currently researching.

Figure 5-15

Figure 5-15. Figure 5-15

However, in some situations you will want to go directly to the table of contents within the help system. Visual Studio 2010 enables you to do this through the Visual Studio Documentation menu item in its main Help menu (see Figure 5-15).

In addition to the several help links you also have shortcuts to MSDN forums and for reporting a bug.

Navigating and Searching the Help System

Navigating through the help system should be very familiar, as it is essentially the same as navigating the Web. On the left-hand side of the browser window you will find links to pages in the same part of the help system as the page being currently viewed, and you will also find links that might be related to the current page.

In the top left of the browser window, you will find a search text box. Enter your search query here (in much the same way you would in a search engine like Google or Bing). This search is a full text search of the pages in the help system, and your query does not necessarily need to appear in the title of the pages. This will take you to the results, which are again provided in a manner similar to the results from a search engine. A one-line extract from the page of each result is displayed to help you determine if it is the article you are after, and you can click through to view the corresponding page.

Configuring the Help System

When you first start using the help system, it's a good idea to configure it to your needs. To do so, select the Help

Configuring the Help System
Figure 5-16

Figure 5-16. Figure 5-16

The first option, Choose online or local help, opens another screen in the dialog that enables you to select whether you will be using the online or offline help. If you select the online option, pressing F1 or opening the help from the Help menu will automatically navigate to the appropriate page in the documentation on MSDN online (for the current context in Visual Studio). Selecting the offline option will navigate to the appropriate page in the documentation installed locally (assuming that the documentation has actually been installed on your machine).

The advantage of the online help over the offline help is that it will always be up to date and won't consume space on your hard drive (assuming you don't install the help content). The disadvantage is that you must always have an active Internet connection, and at times (depending on your bandwidth) it may be slower than the offline version to access. Essentially it is a trade-off, and you must choose the most appropriate option for your work environment.

The Check for updates online option will check if there are any updates to each of the product documentation sets that are currently installed. A screen will show the documentation sets that are installed and do a check for updates for each. When the checks are complete, it will show an estimated download size at the bottom of the dialog. Be aware that these documentation sets can be rather large, and only continue with the update if you have the bandwidth to download files of that size.

The Find content online option enables you to download and add additional product documentation sets to your offline library. The dialog will obtain a list of the available documentation sets and their size from the Internet. You can then click the Add hyperlink button next to each documentation set that your want to download. Again, be aware that these files can be rather large.

The Find content on disk option enables you to install documentation sets from local media, such as a CD/DVD, SD card, or hard drive. You will need to navigate to a manifest file (which has a .msha extension) that accompanies the documentation, and the Help Library Manager will take it from there and handle the installation of the related documentation.

The final option is Remove content, which enables you to remove product documentation sets from your local disk and free some disk space. The screen will show the documentation sets that are currently installed, and you can uninstall a documentation set by pressing the Remove hyperlink button next to its name.

SUMMARY

As you've seen in this chapter, Visual Studio 2010 comes with a number of search-and-replace tools, each best suited to a particular type of search task to enable you to navigate and modify your code quickly and easily.

The new help system is a powerful interface to the documentation that comes with Visual Studio 2010. The ability to switch easily between online and local documentation ensures that you can balance the speed of offline searches with the relevance of information found on the Web. And the abstract paragraphs that are shown in all search results, regardless of their locations, help reduce the number of times you might click a false positive.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.173.199