Chapter 3: The Visual Studio Editor


The editor is the heart of any development environment. In this chapter, we'll take a close look at the editing tools built into Microsoft® Visual Studio®. Whether you're a new or experienced programmer, the information in this chapter will help you become even more productive in Visual Studio 2005. We'll show you how to access editor features that make your job easier, and we'll describe some of the features of the integrated development environment (IDE) that make working in Visual Studio 2005 a real pleasure.

Documents in the IDE

In Visual Studio, everything you do revolves around the solution and the projects in the solution. In that way, Visual Studio becomes your project management tool. What you're managing, for the most part, are the source documents that comprise your solution. To create and edit these documents, you use the Code Editor and the designers in the IDE. The source files you're editing show up in windows that open to the center of the IDE and become part of the tabbed view. The windows that contain these files are known collectively as document windows, and they can be designers, editors, a Web browser, and Help windows.

All these features can be accessed by using named commands either from the Visual Studio Command Window (Ctrl+Alt+A) or through menu commands or keyboard shortcuts. Master these commands and you master Visual Studio 2005.

image from book
Dockable Tool Windows

Not all the tabbed windows in the IDE are document windows. You can add a tool window to the tabbed windows at the center of the IDE by selecting the window and toggling off the window's Dockable value on the Window menu. The Object Browser window (Ctrl+Alt+J) is undocked by default, making it a tabbed window in the IDE. The benefit of adding a tool window to the set of tabbed windows at the center of the IDE is that you can display a large amount of information at once. Alternatively, you can undock a tool window by dragging it away from the edge where it's docked and leaving its Dockable setting on, essentially making the window a floating window. This technique is especially handy if you're working with multiple monitors.

image from book

It's All About Text

The place where you write your code in Visual Studio goes by one of two names, depending on the context of the file being edited. When you're working on a file that's been saved as a programming language type recognized by Visual Studio, the editor you're working in is called the Code Editor. The functionality that you'll find attached to the Code Editor will depend on how your language was integrated into the IDE. When you're working on a text file or a file type that's not been recognized by the IDE, the editor is called the Text Editor. This editor has less functionality than the Code Editor, but it's still fairly powerful. It's important to note that you can run macros in either editor, although the Code Editor hosts a much larger feature set. For the most part, we'll refer to these editors collectively as the Code Editor, but we'll make a distinction where appropriate.

Figure 3-1 shows the various parts of the Code Editor in the IDE. Take a look at the names used in the figure. The parts are probably familiar to you from a usage standpoint, but you might not be aware of their names. Depending on which language you're using, you might find some slight naming differences in the Code Editor, but the functionality is fairly consistent between languages.

image from book
Figure 3-1: The parts of the Cord Editor window

The Code Editor is where you type in code and text. You can click and drag just above the scroll bar on the right side of the Code Editor to break the view into two separate Code Panes. By using multiple Code Panes with a single file, you can look at different parts of your code concurrently. You can also open a new window on your code by typing Window.NewWindow from the Command Window.

The Navigation Bar contains the two boxes at the top of the Code Editor. In Microsoft Visual C#®, these are the Types and the Members drop-down lists. In Microsoft Visual Basic®, these boxes are called the Class Name and Method Name combo boxes. In Microsoft Visual C++®, they are called the Scopes and Members boxes, and in a Web project, these are called the Object and Event boxes. You can use the Navigation Bar to jump quickly to various parts of your code. In Visual Basic, you can also use these boxes to add methods to the current source file.

The light vertical line to the left of the code is the outlining indicator. By clicking the + and – boxes along this line, you can show and hide blocks of code, respectively, within a source file.

Lines of code that changed in the current editing sessions are marked in yellow and green in the Indicator Margin. Changes that have been saved are marked in green, and changes that have yet to be saved are marked in yellow.

The area between the rightmost part of the outlining indicator and the Indicator Margin is the Selector Margin. Clicking in the Selector Margin selects the adjacent line of code. When your mouse pointer is in the area of the Selector Margin, it changes from an arrow pointing to the top left to one that's pointing to the top right. By clicking and dragging down or up in this area, you can select complete blocks of code, as shown in Figure 3-2. The benefit is that you end up selecting the same amount of white space in each line of code, giving you a nice, clean block.

image from book
Figure 3-2: Selecting text by using the Selector Margin

The Indicator Margin is a tool that serves many purposes. It's used to set and delete breakpoints in your code, to indicate bookmarks in code, and to hold Task List shortcuts. During debugging, you'll see an indicator in this margin. When a breakpoint is hit, the breakpoint indicator will contain a yellow arrow that points to the current line of code. This line of code is highlighted in yellow by default. As you step through the code, the yellow indicator shows you where you are in the code, and that statement is highlighted in the Code Editor. You'll also see a Current Line indicator in this margin as you search and navigate your source code.

Notice the tab at the top of the Code Editor window. These tabs let you navigate easily between multiple source files and forms in your project. If you prefer working with multiple document interface (MDI) windows, such as the ones in Visual Studio 6, you can turn off the tabs in the Options dialog box.

Now that we've reviewed the Code Editor window, let's take a look at the kinds of things we can do inside the Code Editor to make programming and editing tasks easier.

Typing and Shortcuts

Two of the most important characteristics of a good editor are efficient typing and text manipulation. If you're new to programming, you might not be aware of the ferocious battles being fought in chat rooms, newsgroups, and Web sites between factions of programmers who prefer one editor over another and who will argue incessantly about what makes their editor better than another.

So what is it about text editing that causes such a strong reaction among programmers? I think it has to do with the idea that programmers like to find the most efficient way to do anything, and if a specific editor allows them to accomplish their goals, they become very attached to that editor. A secondary reason is that it takes some time to master an editor, and, once a programmer does so, he's less likely to want to learn things all over again unless a better editor comes along.

The sections that follow are designed to show you how Visual Studio can work for a programmer who likes to keep her hands on the keyboard. I've noticed that many of the most productive programmers I work with rarely take their hands off the keyboard to perform routine tasks that less experienced programmers use the mouse to perform. The idea behind these shortcuts is to improve your speed in the IDE, and they take some time and practice to learn.

Common Editing Shortcuts

Applications written for Microsoft Windows® use a number of standard keyboard shortcuts that you're probably familiar with. These shortcuts are known as Common User Accessibility (CUA) shortcuts and are based on work done at IBM that has standardized shortcuts across a number of platforms. The biggest advantage of using this particular set of shortcuts is that once you learn them, you can apply them in almost any Windows application, including Microsoft Office. These shortcuts have also been labeled on a number of popular keyboards,

Tip 

Tip For more information about Windows keyboard shortcuts, see the book Microsoft Windows User Experience (Microsoft Press®, 1999), which details how shortcuts such as these should be used in Windows applications.

The tables that follow group the common editing shortcuts for Visual Studio based on function and when you're likely to use them in an editing session. Table 3-1 lists the file shortcuts. You'll use these to open new files or existing files and to save files as you work.

Table 3-1: Common File Shortcuts

Command

Keystroke

Named Command

New

Ctrl+N

File.NewFile

Open

Ctrl+O

File.OpenFile

Save

Ctrl+S

File.SaveSelectedItems

Save All

Ctrl+Shift+S

File.SaveAll

Print

Ctrl+P

File.Print

You'll notice that you're presented with a New File dialog box when you try to create a new file in Visual Studio. This might take a little getting used to if you prefer to see a new text document appear immediately. By selecting a specific file type when you create the new file, you enable much of the functionality associated with a particular language before you save the file. You can save some time when creating a new file by using the Command Window and adding the name and extension of the file you want to create. For example, if you want to create a file named UserMotion.cpp, press Ctrl+Alt+A to open the Command Window and then type File.NewFile UserMotion.cpp. Later in this chapter, in the section "Using the Command Window," we'll show you how to alias commands such as this one so that you can easily create the files you use most often.

Navigating in a document by using keystrokes is one of those skills you tend to learn without actually picking up a book or reading an article. We'll review the common navigation and selection keys and shortcuts here. They are listed in Table 3-2. Notice that selection involves holding down the Shift key and that moving to a larger selection for a particular key usually involves holding down the Ctrl key.

Table 3-2: Common Navigation and Selection Shortcuts

Movement

Movement Keystroke(s)

Selection Keystroke

Character

Right Arrow
Left Arrow

Shift+Right Arrow
Shift+Left Arrow

Word

Ctrl+Right Arrow
Ctrl+Left Arrow

Ctrl+Shift+Right Arrow
Ctrl+Shift+Left Arrow

Line

End
Home
Down Arrow
Up Arrow

Shift+End
Shift+Home
Shift+Down Arrow
Shift+Up Arrow

Code Pane

Page Down
Page Up

Shift+Page Down
Shift+Page Up

Document

Ctrl+End
Ctrl+Home

Ctrl+Shift+End
Ctrl+Shift+Home

Once you've selected text, you can copy or cut it to the Clipboard, and then you can paste it back into the Code Editor. The common editing shortcuts are listed in Table 3-3.

Table 3-3: Common Editing Shortcuts

Command

Keystroke

Named Command

Cut

Ctrl+X

Edit.Cut

Copy

Ctrl+C

Edit.Copy

Paste

Ctrl+V

Edit.Paste

Undo

Ctrl+Z

Edit.Undo

Redo

Ctrl+Y

Edit.Redo

Select current word

Ctrl+W

Edit.SelectCurrentWord

Select all

Ctrl+A

Edit.SelectAll

Finally, let's take a look at the shortcuts that you can use to transpose letters, words, and lines. You can use the shortcuts shown in Table 3-4 to swap the position of two items in the Code Editor. For example, if the cursor is positioned before the letters AB, pressing Ctrl+T will cause the letters to switch their order to BA. Typing Ctrl+Shift+T with the cursor adjacent to or in the word go in the string go boldly will result in a transposition to boldly go. The most useful shortcut in this group runs the command Edit.LineTranspose. Using the shortcut Alt+Shift+T swaps the line where the cursor is located with the next line, making it really easy to move a line of code down the page.

Table 3-4: Transposition Shortcuts

Command

Keystroke

Named Command

Transpose character

Ctrl+T

Edit.CharTranspose

Transpose word

Ctrl+Shift+T

Edit.WordTranspose

Transpose line

Alt+Shift+T

Edit.LineTranspose

These shortcuts should provide you with the functionality you need to perform a fair number of editing tasks without the mouse if you choose to work that way. There's nothing wrong with using the mouse for editing. It's not really much slower to use the mouse than to use shortcuts, but the extra second or two that it takes to go to the mouse can take you out of that creative groove you can get into when you're editing. For me, transitioning from using a mouse back to the keyboard takes a little more time than using a shortcut, so I try to use shortcuts whenever possible.

Custom Keyboard Shortcuts

Earlier we talked about toggling a window's Dockable state to add it to the center of the IDE. There's no shortcut assigned by default to the Window.Dockable command, but you might find that adding one would be handy for making a very data-heavy window easier to read.

To create a new shortcut in the IDE, press Ctrl+Alt+A to open the Command Window and type Tools.Options. This will bring up the Options dialog box (shown in Figure 3-3). Click the Keyboard item in the Environment folder to bring up the Keyboard page. This page lets you do a number of things with shortcuts in the IDE, such as create and edit shortcut keys, change the keyboard mapping scheme, and save a custom mapping scheme. The first time you add a custom shortcut to the IDE, you'll be prompted to save your mapping scheme with a custom name.

image from book
Figure 3-3: The keyboard page of the Options dialog box

To find the command you want to assign the new shortcut to, type part of the command name in the Show Commands Containing box. In this case, type dock, and Window. Dockable will show up in the command list.

Here's the tricky part. Nearly every possible keystroke shortcut has been taken in Visual Studio. You can overwrite keystrokes that you think you'll never use, but that isn't always the most satisfactory solution. For one thing, if you go to work on a different machine and you haven't updated the shortcuts, you might end up keying the wrong command, which can be both annoying and potentially harmful to whatever you're typing at the moment. Your best bet is to find an available keystroke and take maximum advantage of it.

Visual Studio allows you to create keystroke sequence shortcuts. To start a sequence, you hold down the Ctrl key and press another key. The IDE then waits for another stroke to determine which command to execute. I've found that Ctrl+0 (Ctrl+zero) hasn't been taken in Visual Studio by default. So I can chord all my personal commands based on this key sequence and assign the second key sequence to one that matches the command I'm trying to execute. For the Window.Dockable command, I assign the sequence Ctrl+0, Ctrl+D (Ctrl+zero, Ctrl+D) by typing that combination in the Press Shortcut Key(s) box. Be sure to save the new shortcut by clicking the Assign button.

While I have the Options dialog box open, I can add a keystroke shortcut for the Options dialog box itself by typing Tools.Options in the Show Commands Containing box and assigning the keystroke Ctrl+0, Ctrl+O. Now I can open the Options dialog box quickly at any time to customize my IDE.

You can assign keystroke shortcuts to named commands in the IDE, to add-ins that you create or install, and to macros that you create and save.

Other Keyboard Schemes

In addition to the keyboard shortcuts mentioned here, Visual Studio 2005 provides a number of other keyboard shortcut mappings for you to choose from. To select a new scheme, go to the Options dialog box (Tools.Options) and look on the Keyboard page where we earlier assigned some keyboard shortcuts. At the top of the page, you'll see a drop-down list that allows you to choose from a number of different keyboard mappings. These mappings include Visual C++ 2.0, Visual Studio 6, Emacs, and Brief. To use one of these mappings, select the one you want from the list and click OK. These mapping schemes are designed to make it easy for programmers to move from their preferred editor to Visual Studio 2005. For more information on this feature, see the Shortcut Keys topic in Visual Studio Help.

Note 

Emacs and Brief support in Visual Studio 2005 are really more than just key bindings. They are emulations that offer special text selection and indenting behavior in the IDE.

Understanding Tabs and Code Formatting

Code formatting is another one of those issues that developers tend to feel strongly about. When it comes to code formatting, the bottom line for most organizations is that some sort of standard should exist. The formatting options for each of the languages supported in the Visual Studio IDE are set in the Text Editor node of the Options dialog box. When you set options for All Languages, as shown in Figure 3-4, you override the settings for each individual language listed in the Text Editor folder.

image from book
Figure 3-4: Setting global Tabs options

As you can see in the figure, you can set Indenting to None, Block, or Smart. The behavior of these options is determined by the language and the Tabs settings below them. When None is the selected Indenting type, pressing Enter at the end of a line will start the next line at the leftmost space in the Code Editor. Block indenting sets the indent to the same space as the first character in the current line. This is a common generic setting that lets you indent manually but doesn't force you to key a lot of extra tabs to get to where you want to be. The Smart setting applies an indent by context. For example, pressing Enter after an open brace ({) in C# will automatically indent the next line.

The choice between using spaces or tab characters for indenting is usually a matter of personal preference or of the coding standard you want to apply. If you prefer that all the code you deal with consists of spaces rather than tabs, you can set that option globally when you customize your IDE. If you prefer spaces to tabs, keep in mind that Visual C# specifies tabs for indentation by default. You can view the white space in the document by using the Edit.ViewWhitespace command (Ctrl+R, Ctrl+W). Figure 3-5 shows a document in which the white space is visible. If you use tab characters in your source code, they will show up as right arrows. If you use spaces in your code, a single dot will show up for every space.

If you want to convert existing files from tabs to spaces or vice versa, select the desired option on the Tabs page of the Options dialog box and then click OK to close the dialog box. Then simply press Ctrl+K, Ctrl+D to apply the new formatting to the whole document.

The Formatting page of the Options dialog box, shown in Figure 3-6, controls a number of characteristics of code typed into a Code Pane. This page is available for most of the major languages supported in the IDE, but under Basic, the VB Specific subpage handles the customizations. The page in the figure shows the C/C++ formatting options.

Different options are available for different languages. Notice the Indent Braces check box in Figure 3-6. In C++, this check box is clear by default. This setting forces curly braces that enclose functions to be indented by one tab or by four spaces, depending on how you have your spaces and tabs configured. You can change the formatting of an entire document by setting this preference and pressing the Ctrl+A then Ctrl+K, Ctrl+F keystrokes that we used earlier.

image from book
Figure 3-5: Displaying white space in the Code Editor

image from book
Figure 3-6: The C/C++ Formatting page




Working with Microsoft Visual Studio 2005
Working with Microsoft Visual Studio 2005
ISBN: 0735623155
EAN: 2147483647
Year: 2006
Pages: 100

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net