Using the Editor
空慈
2023-12-01
Using the Editor
The Visual Studio .NET environment includes a powerful text editor. This
editor includes a number of features aimed at making your code-writing
experience more productive and as flexible as possible. The following
subsections describe some of the Visual Studio .NET code editor features.
Using IntelliSense to Enter Code
Programming in Visual Studio .NET, even at the very beginning, involves
working with members (properties and methods) of objects. To make it easier
for you, when you enter the name of any object, Visual Studio .NET provides
a drop-down list of the members of that class of object. In addition, when
you type the name of a method, Visual Studio .NET provides a description of
the method signature, including a list of all the parameters you need to
supply for the method and a list of all the various ways you can call the
method. This technology is named IntelliSense. In addition, IntelliSense
allows you to auto-complete any syntax by pressing the space bar or the Tab
key once the command you wish to use is highlighted.
Splitting the Editing Window
At times, you may want to view locations within the same code window, as
shown in Figure 3.18. You can select the Window, Split menu item to insert
a horizontal splitter bar into the code window. You can scroll each pane of
the window individually, allowing you to view two locations within the same
file. You can also split the editing window by grabbing the divider bar in
the upper-right corner of the editing window and dragging it downward.
Double-clicking the same area acts as if you'd selected the Window, Split
menu item.
Figure 3.18. You can split the window to see two different areas of your
code at the same time.
TIP
If you need a full-height split of a document window, use the Window, New
Window menu item to create a second window. Then position it however you
like.
Finding Text
You can search for text anywhere in the current procedure, the current
module, or within a highlighted block of code. From any code window, you
can select the Edit, Find and Replace, Find menu item or press Ctrl+F to
bring up the Find dialog box, as shown in Figure 3.19. If you highlight a
piece of text in your code window and then select the Find window, the text
will be automatically added to the Find What combo box.
Figure 3.19. The Find dialog box will help you search for text in your
project.
Enter a piece of text in the Find What combo box and click the Find Next
command button to find the next location in the code that matches the
string. You can keep this dialog box open and keep clicking Find Next to
find the next occurrence of the string. You can also close the Find dialog
box and press F3 to repeat the search.
There are other options you can use to find a particular string in your
source code. You can narrow your search to the current procedure or the
current module, or you can search across all open documents. You can find a
whole word that matches the string you input, you can perform a
case-sensitive search, or you can enter wildcards, such as * and ?, to
perform pattern matching.
Replacing Text
Replace text using the Replace dialog box. To bring up the Replace dialog
box, select Edit, Find and Replace, Replace, press Ctrl+H from within a
code window, or click the Replace command button in the Find window. Figure
3.20 shows an example of the Replace dialog box.
Figure 3.20. Find and Replace can come in handy for changing one variable
name to another.
This dialog box is essentially the same as the Find dialog box, but it
allows you to replace one piece of text at a time. You can also find the
next occurrence of the Find What text by clicking Find Next. If you wish to
replace all the values within the scope specified, you can click Replace
All.
Finding Text in All Project Files
The Find dialog box is useful if you are searching open documents, but if
you need to search for items in any file in your current project or in your
whole solution, you will need to use the Find in Files dialog box shown in
Figure 3.21. This dialog box allows you to search all files, regardless of
whether they are open, across the current project, or even across the whole
solution.
Figure 3.21. Find in Files allows you to look for text in all files,
regardless of whether they are open.
Replacing Text in All Project Files
The Replace in Files dialog box, shown in Figure 3.22, allows you to
perform a search-and-replace operation throughout the files in your project
or within a folder. A nice feature of the Replace in Files dialog box is
the Keep Modified Files Opened on Replace All option. If you choose to make
a bulk change and click Replace All, this option (if checked) will open
each modified file so that you can review the change and undo it if it's
not what you intended.
Figure 3.22. Search-and-replace operations can be performed within files
using this dialog box.
Searching for Symbols
A symbol is a definition of a variable, such as you might find in your own
classes or structures. Searching for symbols is different from searching
for normal text in that searching for symbols only involves looking for
definitions and references to those definitions based on the symbols you
specify. The definitions and references are displayed in the Find Symbol
Result window shown in Figure 3.23, which resembles the Search Results
window you see in Windows Explorer.
Figure 3.23. The Find Symbol dialog box allows you to find the definition
of classes.
Incremental Searching
Under the Edit, Advanced menu is an item named Increment Search (Ctrl+Alt+I
when using the VB keyboard scheme). You enter an incremental search mode in
the code editor so you can press any of the alphanumeric keys and drill
down to the selection in your file that matches what you have typed so far.
The Incremental Search feature interacts with the main Find tool by sharing
the last Find pattern. You can typically press the Shift key instead of the
Alt key to initiate a reverse incremental search or to toggle between a
forward search and a reverse search.
NOTE
A regular Find operation searches for text in hidden regions when you've
selected the Search Hidden Text option, but an incremental search does not.
Using Bookmarks to Locate Code
A bookmark allows you to mark a certain line of code and then find it
quickly later. When you close a document, bookmarks in that document are
discarded. If you want pointers to code that are persistent, use the Edit,
Bookmarks, Add Task List Shortcut menu item. The Task List shortcuts will
be saved with your solution, regardless of whether the documents are open.
Task List shortcuts also display as items in the Task List, making
navigation to those locations even easier.
Commenting/Uncommenting Blocks of Code
This handy tool allows you to highlight a block of text and then comment or
uncomment it using the Edit, Advanced, Comment Selection or Uncomment
Selection menu item. This tool becomes even more handy when you realize
that lines of code that contain line continuation (that is, logical lines
of code broken into multiple physical lines using the " _" characters)
don't "comment out" as well as they did in Visual Basic 6 (where commenting
out the first line of the block commented out the whole block). Now, you
must add an apostrophe to comment out each line, in turn. Using these menu
items (or their corresponding toolbar buttons) makes it easy to comment and
uncomment blocks of code.
Dragging and Dropping Text
Highlight the text you want to move, click and hold your mouse, and then
drag the text to the new location. To copy the text, press and hold down
the Ctrl key while you are dragging the text.
Word Wrapping
Word Wrap is a feature that removes the horizontal scroll bar and ensures
that all text is visible in the document window by wrapping long lines at
word boundaries. You can enter this mode by selecting the Edit, Advanced,
Word Wrap menu item. No carriage returns or line feeds are inserted into
your document梂ord Wrap is a visual display effect only.
TIP
To be honest, we find Word Wrap to be a feature of limited value. Turning
this feature on makes it difficult to decipher Visual Basic .NET code,
because Visual Basic relies on line breaks to delimit code. Word Wrap would
be more useful in a language such as C#, where semicolons delimit logical
lines of code, not line breaks.
Toggling Full Screen Mode
Your Visual Studio .NET design environment can sometimes get cluttered with
the multitude of open windows and toolbars. This clutter can cause a
problem when you wish to see more than a few lines of your source code at
the same time. You can use the Full Screen mode provided by Visual Studio
.NET to see more of your source code. To enter this mode, open a code
editor window and select the View, Full Screen menu item. To return to
normal mode, you can select the View, Full Screen menu item again.
Tabbing Between Windows
To move quickly among the various open windows in your Visual Studio .NET
session, press Ctrl+Tab to iterate forward and Shift+Ctrl+Tab to iterate
backward. This keeps your hands on the keyboard instead of having to use
the mouse to select a specific window.
Managing the Clipboard Using the Clipboard Ring
Although the Windows Clipboard can hold only a single piece of text at a
time, Visual Studio provides the Clipboard Ring, in which you can store up
to 20 items at a time. If you add another item, the Clipboard Ring will
remove the first item you added so that it maintains at most 20 items. To
view the items in the Clipboard Ring, place your cursor somewhere in a code
window and use the Edit, Cycle Clipboard Ring menu item. You can also look
at the hotkey combination next to this menu item to determine the
keystrokes you should use. Whichever keystrokes you use, do not release
these keys but keep pressing the final key in your sequence. This allows
you to cycle through all the items in the Clipboard Ring. When you find the
one you want, release the keys, and the selected item will stay in your
code window.
You may also view the Clipboard Ring by selecting the Clipboard Ring tab in
the Toolbox window. You can then view each of the items and even drag and
drop from the Toolbox into the code editor.
Going Back
Most Visual Basic 6.0 users are familiar with using the Shift+F2 key to
take them to the definition of the procedure whose name is under the mouse
cursor. In Visual Basic 6.0, you can also use the Ctrl+Shift+F2 key to
return to the location where the procedure was called. (These keystrokes
also work in Visual Studio .NET, if you've selected the Visual Basic 6.0
keyboard layout. If you've selected another keyboard layout, you can
right-click a procedure name and select Go To Definition on the context
menu that appears.)
Visual Studio .NET enhances this capability by creating a stack of "Go
Back" locations. Each time you perform a command that takes you away from
where you currently are located in a document, that previous location is
placed into this Go Back stack. Some of the commands that mark these
locations are Incremental Find, Opening a New Document, GoTo Definition,
and others. This means that at any time you may use the Ctrl+Shift+F2 key
to return to any of these Go Back locations. You may continue to hit these
keystrokes to cycle through the complete stack of Go Back locations.
NOTE
We could not find any documentation on the Go Back feature for any of the
keyboard schemes. It works fine for the Visual Basic scheme, but if you are
using any of the others, you may just have to experiment.
Creating Macros
If you find yourself performing the same functions over and over again
within the Visual Studio .NET IDE, you can take advantage of the
environment's capability of recording these steps into a macro. Using this
feature is much like recording a macro in the Microsoft Office products.
Use the Tools, Macros, Record Temporary Macro menu item to turn on the
Macro recorder, perform the steps, and then save the macro either
temporarily for this session or permanently within a macro project that you
can then add to any solution you work with. Although this is a handy
feature, digging deeper here is beyond the scope of this book. Consult the
online help facility in Visual Studio .NET for more information on using
the Macro Recorder.