Inside Microsoft® Visual Studio® .NET 2003 [Electronic resources]

Brian Johnson

نسخه متنی -صفحه : 118/ 29
نمايش فراداده

Documents in the IDE

In Visual Studio .NET, everything you do revolves around the solution and the projects in the solution. In that way, Visual Studio .NET becomes your project management tool. What you're managing, for the most part, are source documents that comprise your projects and the tool windows that provide functionality inside the IDE. To create and edit the documents themselves, 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, the Web browser, and Help windows.

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 leave its Dockable setting on, essentially making the window a floating window. This technique is especially handy if you're working with multiple monitors.

Visual Studio .NET has a huge number of additional Code Editor features in the 7.0 and 7.1 versions. Among the new and enhanced features are outlining, code formatting, and my personal favorite, line numbering.

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

It's All About Text

The place where you write your code in Visual Studio .NET 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 .NET, 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, you're working in 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.

Figure 3-1. The parts of the Code Editor window

The Code Editor is where you type in code and text. Most of the other features shown in the figure can be toggled off to give you an unfettered view of the Code Editor. You can click and drag just above the scrollbar 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.

The Navigation Bar contains the two boxes at the top of the Code Editor. In Visual C#, these are the Types drop-down list and the Members drop-down list. In Visual Basic .NET, these boxes are called the Class Name and Method Name combo boxes. You can use the Navigation Bar to quickly jump to different parts of your code. In Visual Basic .NET, 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 hide and show blocks of code within a source file. We'll discuss this feature in some detail later in the chapter.

The area between the riost part of the outlining indicator and the Margin Indicator Bar is the selection margin. Clicking in the selection margin selects the adjacent line of code. When your mouse pointer is in the area of the selection margin, it changes from an arrow pointing northwest to one that's pointing northeast. 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 of doing this is that you end up selecting the same amount of white space in each line of code, giving you a nice clean block. (Trust me: for editors and writers, this feature is huge.) Using the selection margin to select an entire line with a single click can help reduce selection errors and can make it much easier to keep your code formatting when you copy text between files.

Figure 3-2. Selecting text using the selection margin

The Margin Indicator Bar 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 line is highlighted in the Code Editor.

Notice the tab at the top of the Code Editor window. When you're in tabbed view, you'll see a tab like this for every document and nondockable tool window that you have open. 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.

Lab: Navigating Between Views and Windows

Let's loosen up our fingers and take a look at how to quickly navigate between the Code Editor, the Designer window, Solution Explorer, and Class View.

Let's say we're in a project and that we're working on some code and some user interface design. Using the keyboard, press Ctrl+Alt+L to pop into Solution Explorer. Navigate to a C# code file, and press Enter. By default, any Windows Forms or Web Forms associated with the file will open; otherwise, the source file will open. To see the source code for the C# file, press F7. To pop back to the form, press Shift+F7.

Now click Ctrl+Alt+L again to pop back into Solution Explorer. Navigate to another file, and press Enter. You should have at least three tabs showing in the IDE. To jump between these open files, press Ctrl+Tab or Ctrl+F6.

Finally, to jump into Class View, use Ctrl+Shift+C.

These are all important keystroke combinations. They let you move around in the IDE without taking your hands off the keyboard. If you're the kind of programmer who likes to stick close to the keyboard, these shortcuts can save you a ton of time.

Typing and Shortcuts

If good editors are about anything, they're about 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 and newsgroups and on 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. (Not that I'm above the fray, given my belief that Visual Studio .NET is the One True Editor.)

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 masters an editor, 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 .NET 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 go to the mouse for. The idea behind these shortcuts is to improve your speed in the IDE, and they take some time and practice to learn. The information that follows is provided as a quick reference for programmers who are experienced with Windows shortcuts and as a tutorial for programmers who are used to working in a UNIX editor such as Vi or Emacs.

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, including most of the Microsoft keyboards.

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 .NET based on function and on when you're likely to use them in an editing session. Table 3-1 lists the file shortcuts. You'll use these to open a new file 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 .NET. 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, you press Ctrl+Alt+A to open the Command Window and then enter File.NewFile UserMotion.cpp. Later in this chapter, in the section "Using the Command Window," we'll show you how to alias commands like this one so you can easily create the files you use most often. In Chapter 4, we'll show you how to create a macro that creates a new file of the type you're most likely to be interested in when you press Ctrl+N.

Navigating in a document 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 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

Lab: Using the Clipboard Ring

The Clipboard Ring is a tool you can use to track and use multiple copy operations in the IDE. The Clipboard Ring is available as a tab in the Toolbox. To practice with the Clipboard Ring, pin open the Toolbox and click the Clipboard Ring Toolbox tab. Open a code listing, and select some text. Copy that text to the Clipboard, and watch the Clipboard Ring. Copy a few more bits of text and notice that the most recently selected text is always at the top. You can double-click any of the Clipboard items to insert them into a file, but there's a much cooler way to use this tool. Press Ctrl+Shift+Insert, holding down Ctrl+Shift. This will insert the text at the top of the stack. Notice that the inserted text is still selected. Press Insert again, and the second item on the stack replaces the selected text. Press Insert a third time, and the text from the first copy operation is inserted. (You get the idea.) Press Insert a final time, and you're back to the text you last selected. You can cycle through the Clipboard Ring to easily find the copied text you're looking for.

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

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 .NET. 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 in at the moment. You're best bet is to find an available keystroke and take maximum advantage of it.

Visual Studio .NET now allows you to create chorded shortcuts. To start a chord, 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+, (Ctrl+comma) hasn't been taken in Visual Studio .NET by default. So I can chord all my personal commands off 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+,, Ctrl+D (Ctrl+comma, 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+,, 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.

Using the Keybindings Table Add-in

The only way to determine what shortcuts are assigned in Visual Studio .NET without resorting to code is to look at the commands assigned in the Options dialog box. That isn't the easiest way to map out what's going on, so Craig wrote a nice little add-in that lists all of the named commands and currently assigned shortcuts in the IDE.

The Keybindings Table add-in is available from the Visual Studio .NET Web site at http://msdn.microsoft.com/vstudio/downloads/automation.asp. Because this add-in is written in C++, you need to compile it using Visual Studio .NET or Visual C++ .NET. The compiled add-in is registered in Visual Studio .NET during the build process. After you restart Visual Studio .NET, you'll find that the add-in loaded by the IDE.

On the Visual Studio .NET Help menu, you'll find a new command named KeyMap. Choosing this command brings up the Keyboard Help dialog box, shown in Figure 3-4.

Figure 3-4. The Keyboard Help dialog box

The Keybindings Table add-in performs a number of useful functions. You can use this add-in to browse the keystroke shortcuts assigned to named commands in the IDE, or you can just browse all the commands available. You can also copy the table entries to the Clipboard. Just select an entry and click the Copy button. If you want to select multiple entries, hold down the Ctrl key and click another item in the list to select a range of entries. Once on the Clipboard, these entries can be pasted into Microsoft Excel for easier reading and sorting.

The Zen of 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 folder of the Options dialog box. When you set options for All Languages, as shown in Figure 3-5, you override the settings for each individual language listed in the Text Editor folder.

Figure 3-5. 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-6 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.

Figure 3-6. Displaying white space in the Code Editor

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 select all the code in the file by pressing Ctrl+A. Press Ctrl+K, Ctrl+F (Edit.FormatSelection) to apply the new formatting to the selection.

The Formatting page of the Options dialog box, shown in Figure 3-7, 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 page handles the customizations. The page in the figure shows the C# formatting options.

Figure 3-7. The C# Formatting page

Different options are available for different languages. The option to notice in Figure 3-7 is the Leave Open Braces On Same Line As Construct check box. In C#, this check box is clear by default. This setting forces an open curly brace added at the end of a line to be moved to the next line in the Code Editor and to be aligned with the beginning of the statement. The following line is then indented like this:

static void Main(string[] args)
{
//
// TODO: Add code to start application here
//
}

Selecting the Leave Open Braces On Same Line As Construct option causes your code to be formatted in slanted style, which is often called K&R for the style adopted by Kernighan and Ritchie in The C Programming Language (2nd ed., Prentice-Hall, 1988). In this style, the first brace in the block is located at the end of the statement, as in the following:

static void Main(string[] args) {
//
// TODO: Add code to start application here
//
}

Once you decide on the style characteristics you want to apply to your code, you can use the Edit.FormatSelection command to apply the style.

You might have noticed that the C# Language Specification employs a mixed style, similar to strict K&R styling, in which methods use the slanted style and classes use the straight style. When you use Edit.FormatSelection to apply your desired formatting, your style is applied to every selected block in your code. If you use the Edit.FormatDocument command, every block in the document is modified to match the selected style. To use a mixed style, select the Leave Open Braces On Same Line As Construct check box, but make sure that you don't apply your formatting to the entire document.