Chapter 2. Visual Studio 2005

In Chapter 1, you learned that you can create your C# applications using Notepad. In this chapter, you’ll learn why you never will. Microsoft developed Visual Studio 2005 to facilitate the creation of Windows and web applications. You will find that this Integrated Development Environment (IDE) is a very powerful tool that will greatly simplify your work.

Visual Studio 2005 offers many advantages to the .NET developer, among them:

In addition, Microsoft has added these new features to VS2005:

Visual Studio 2005 is a highly useful tool that can save you hours of repetitive tasks. It is also a large and complex program, so it is impossible in this chapter to explore every nook and cranny. Instead, this chapter will lay the foundation for understanding and using Visual Studio 2005, and will point out some of the nastier traps you might run into along the way.

Before You Read Further

This chapter has a lot of information in it, and you won’t need all of it all at once. In fact, much of the information will not even apply to console applications, but will be valuable when you are ready to create Windows or web applications.

Many readers like to skim this chapter the first time through, and then come back for the details later. But it is your book, you paid for it (you did pay for it, didn’t you?), and so you are free to read the entire chapter, take notes as you go, skip it entirely, or otherwise use it to your best advantage.

Whether or not you read this chapter, I do strongly recommend you spend time (lots and lots of time) exploring Visual Studio in detail. You will forever be surprised at how much is in there and how much you can set it up to behave as you want; it is your principal development tool. Ignoring Visual Studio would be like a race car driver never looking under the hood. In time, you not only want to know how to change the oil, but you also want to understand how the valves work and why the linkage sticks.

Start Page

The Start Page is the first thing you see when you open Visual Studio 2005 (unless you configure it otherwise). From here, you can create new projects or open a project you worked on in a previous session. You can also find out what is new in .NET, access .NET newsgroups and web sites, search for help online, download useful code, or adjust Visual Studio 2005 to your personal requirements. Figure 2-1 shows a typical Start Page.

The Start Page includes a list of recent projects (clicking on one will open it), along with the ability to open any project on your computer or to create a new one. The Getting Started box provides links to features and helpful sites, and the large box on the right contains useful articles from MSDN online.

Projects and Solutions

A C# program is built from source files, which are text files containing the code you write. Source code files are named with the .cs extension. The HelloWorld.cs file you created in Chapter 1 is an example.

A typical Visual Studio 2005 application can have a number of other files (such as assembly information files, references, icons, data connections, and more). Visual Studio 2005 organizes these files into a container called a project.

Visual Studio 2005 provides two types of containers for your source code, folders, files, and related material: the project and the solution. A project is a set of files that work together to create an executable program (.exe) or a dynamic link library (.dll). Large, complex projects may contain multiple .dll files.

A solution is a set of related projects, although it may also have just one project. Each time you create a new project, Visual Studio 2005 either adds it to an existing solution or creates a new solution.

Solutions are defined within a file named for the solution, and have the extension .sln. The .sln file contains metadata, which is basically information about the data. The metadata describes the projects that compose the solution and information about building the solution.

There are a number of ways to open an existing solution. The simplest way is to select Open Project from the Start menu (which opens a project and its enclosing solution). Alternatively, you can open a solution in Visual Studio 2005 just by double-clicking the .sln file in Windows Explorer.

Typically, the build process results in the contents of a project being compiled into an executable (.exe) file or a dynamic link library (.dll) file. This book focuses on creating executable files.

The Visual Studio 2005 Integrated Development Environment (IDE) is centered around an editor. An editor is much like a word processor, except that it produces simple text (that is, text with no formatting, such as bold and italics). All source code files are simple text files.

The Visual Studio 2005 IDE also provides support for building Graphical User Interfaces (GUIs ) , which are integral to Windows and web projects. The following pages introduce some of the key features of the IDE.

The IDE is a Multiple Document Interface (MDI ) application. There is a main window, and within the main window are a number of smaller windows. The central window is the text editing window. Figure 2-3 shows the basic layout.

To the left of the editing window are a number of tabbed windows that contain tools used when creating Windows and web applications. To the right of the editing window are both stacked and tabbed windows. Shown on top is the Solution Explorer , which allows you to examine and manipulate the files in the solution. Below the Solution Explorer are two tabbed windows: Dynamic Help and Properties . The Properties window is used extensively when creating web and Windows desktop applications. Dynamic help will examine what you are doing and offer help on related topics.

At the bottom of the IDE are five tabbed windows: Error List , Task List , Output , Find Results , and Find Symbol Results .

All of these windows, plus the Toolbox, are resizable and dockable. They can be resized by placing the mouse cursor over the edge you want to move. The cursor will change to a double-arrow resizing cursor, at which point you can drag the window edge one way or the other.

Right-clicking on the title bar of a dockable window pops up a menu with five mutually exclusive check items:

If you click the title bar of a window and drag it, it floats free. You can now place it where you want. Visual Studio 2005 provides guides to help you with locating the window. To see this at work, grab the Solution Explorer and pull it free of its current position. As you move about, the IDE positioning indicators appear, as shown in Figure 2-4.

As you click on each positioning indicator, a shadow appears to show you where the window would go if you release the mouse. Notice in the center of the editing window that there is a cluster of five indicators. If you choose the center square, the window will be tabbed with the current window. To put the Solution Explorer back where it belongs, hover over the Dynamic Help window; a five-part indicator will appear, and you can select the upper indicator to place Solution Explorer above the tabbed set of Properties and Dynamic Help.

You can also double-click on either the title bar or the tab to dock and undock the window. Double-clicking on the title while docked undocks the entire group. Double-clicking on the tab just undocks the one window, leaving the rest of the group docked.

You can run your application at any time by selecting either Start or Start Without Debugging from the Debug menu , or you can accomplish the same results by pressing either F5 or Ctrl-F5, respectively. You can also start the program by clicking the Start icon (▸) on the Standard toolbar.

For console applications , the advantage of running the program with Ctrl-F5 is that Visual Studio 2005 will open your application in a console window, display its results, and then add a line to press a key when you are ready, as shown in Figure 2-5. This keeps the window open until you’ve seen the results and pressed a key, at which point the window will close.

The program can be built (that is, the .exe and .dll files can be generated) by selecting a command under the Build menu . You have the option of building the entire solution or only the currently selected project.

The menus provide access to many of the commands and capabilities of Visual Studio 2005. The more commonly used menu commands are duplicated with toolbar buttons for ease of use.

The menus and toolbars are context-sensitive (the available selection is dependent on what part of the IDE is currently selected, and what activities are expected or allowed). For example, if the current active window is a code-editing window for a console window, the top-level menu commands are: File , Edit , View, Refactor, Project, Build, Debug, Data, Tools, Test, Window, Community, and Help.

Many of the menu items have keyboard shortcuts, listed adjacent to the menu item itself. These are comprised of one or more keys (referred to as a chord), pressed simultaneously. Shortcut keys can be a huge productivity boost, because you can perform common tasks quickly, without removing your hands from the keyboard.

The following sections describe some of the more important menu items and their submenus, focusing on those aspects that are interesting and different from common Windows commands.

The Edit menu contains the text editing and searching commands that one would expect, but also includes commands useful in editing code. The most useful are discussed next.

Visual Studio 2005 includes a number of advanced Find and Replace options that you’ll use frequently. The most common ones are discussed in this section.

Quick Find and Quick Replace . These are just slightly jazzed names for slightly jazzed versions of the typical Find and Replace. You can access Quick Find with Ctrl-F, and Quick Replace with Ctrl-H. Both commands call essentially the same dialog boxes, switchable by a tab at the top of the dialog box, as shown in Figure 2-6.

The search string defaults to the text currently selected in the code window, or, if nothing is selected, to the text immediately after the current cursor location.

The Look in: drop-down offers the choice of the Current Document, All Open Documents, the Current Project, and the current method.

Search options can be expanded or collapsed by clicking on the plus/minus button next to Find Options. By default, Search hidden text is checked, which allows the search to include code sections currently collapsed in the code window. The Use checkbox allows the use of either regular expressions or wildcards .

If the Use checkbox is checked, then the Expression Builder button to the right of the Find what textbox becomes enabled, providing a very handy way to insert valid regular expression or wildcard characters.

Once a search string has been entered in the Find what: text box, the Find Next button becomes enabled. In Quick Find mode, there is also a Bookmark All button, which finds all occurrences of the search string and places a bookmark (described below) next to the code.

In Quick Replace mode, there is also a Replace with text box, and buttons for replacing either a single occurrence or all occurrences of the search string.

Find in Files . Find in Files (Ctrl-Shift-F) is a very powerful search utility that finds text strings anywhere in a directory or in subdirectories (subfolders). It presents the dialog box shown in Figure 2-7. Checkboxes present several self-explanatory options, including the ability to search using either wildcards or regular expressions.

Find Symbol . Clicking the Find Symbol command (Alt-F12) will bring up the Find Symbol dialog box , which allows you to search for symbols (such as namespaces, classes, and interfaces) and their members (such as properties, methods, events, and variables). It also allows you to search in external components for which the source code is not available.

The search results will be displayed in a window labeled Find Symbol Results. From there, you can move to each location in the code by double-clicking on each result.

Visual Studio 2005 allows you to outline, or collapse and expand, sections of your code to make it easier to view the overall structure. When a section is collapsed, it appears with a plus sign in a box along the left edge of the code window. Clicking on the plus sign expands the region.

You can nest the outlined regions, so that one section can contain one or more other collapsed sections. There are several commands to facilitate outlining (shown in Table 2-3).

The default behavior of outlining can be set using the Tools → Options menu item. Go to Text Editor, and then the specific language for which you want to set the options. The outlining options can be set for VB.NET under Basic → VB Specific, for Visual Basic 2005 under Visual Basic 2005 → Formatting, and for C++ under C/C++ → Formatting.

Microsoft IntelliSense technology makes the lives of programmers much easier. It has real-time, context-sensitive help available, which appears right under your cursor. Code completion automatically completes your thoughts for you, drastically reducing your typing. Drop-down-lists provide all methods and properties possible in the current context, available at a keystroke or mouse click.

Unlike previous versions of Visual Studio, IntelliSense now works in all code windows, including not only the Visual Basic 2005 code-behind files, but also within both server- (i.e., script) and client-side (i.e., HTML) code in content files.

The default IntelliSense features can be configured by going to Tools → Options and then the language-specific pages under Text Editor.

Most of the IntelliSense features appear as you type inside a code window, or allow the mouse to hover over a portion of the code. In addition, the Edit → IntelliSense menu item offers numerous commands, the most important of which are shown in Table 2-4.

The member list presents itself when you type a dot operator following any class or member name.

Every member of the class is listed, and each member’s type is indicated by an icon. There are icons for methods, fields, properties, events, and so forth. In addition, each icon may have a second icon overlaid to indicate the accessibility of the member: public, private, protected, and so on. If there is no accessibility icon, then the member is public.

Two of the subcommands under the IntelliSense menu item, Insert Snippet and Surround With, tap into a great feature to reduce typing and minimize errors: code snippets . A code snippet is a chunk of code that replaces an alias. A short alias is replaced with a much longer code snippet. For example, the alias switch would be replaced with:

    switch ( switch_on )
    {
       default:
    }

with the expression switch_on highlighted in yellow and the cursor in place, ready to type in your own expression. In fact, all the editable fields will be highlighted, and you can use the Tab key to navigate through them, or Shift-Tab to go backwards. Any changes made to the editable field are immediately propagated to all the instances of that field in the code snippet. Press Enter or Esc to end the field editing and return to normal editing.

To do a straight alias replacement, either select Insert Snippet from the menu, or more easily, press Ctrl-K, Ctrl-X. Alternatively, just type an alias in the code window and an IntelliSense menu will pop up with a list of aliases, with the current one highlighted.

Alternatively, a code snippet can surround highlighted lines of code, say with a for construct. To surround lines of code with a code snippet construct, highlight the code, then either select Surround With from the menu, or press Ctrl-K, Ctrl-S.

The View menu is a context-sensitive menu that provides access to the myriad of windows available in the Visual Studio 2005 IDE. You will probably keep many of these windows open all the time; others you will use rarely, if at all.

The View menu is context-sensitive. For example, with an ASP.NET content file on the work surface, the first three menu items will be Code, Designer, and Markup, while the Code and Designer menu items will be omitted if looking at a code-behind file.

When the application is running, a number of other windows, primarily used for debugging, become visible or available. These windows are accessed via the Debug → Windows menu item, not from the View menu item.

Visual Studio 2005 can store several different window layouts. In particular, it remembers a completely different set of open windows during debug sessions than it does during normal editing. These layouts are stored per-user, not per-project or per-solution.

The Class View (Ctrl-Shift-C) shows all the classes in the solution in a hierarchical manner. A typical Class View, somewhat expanded, is shown in Figure 2-8.

As with the Solution Explorer, any item in the class view can be right-clicked, which exposes a pop-up menu with a number of context-sensitive menu items. This can provide a convenient way to sort the display of classes in a project or solution, or to add a method, property, or field to a class.

The button on the left above the class list allows for sorting the classes listed, either alphabetically, by type, by access, or grouped by type. Clicking on the button itself sorts by the current sort mode, while clicking on the down arrow next to it presents the other sort buttons and changes the sort mode.

The button on the right above the class list allows you to create virtual folders for organizing the classes listed. These folders are saved as part of the solution in the .suo file.

These folders are virtual (that is, they are illusory). They are only used for viewing the list. As such, they have no effect on the actual items. Items copied to the folder are not physically moved, and if the folders are deleted, the items in them are not lost. Note that if you rename or delete an object from the code that is in a folder, you may need to manually drag the item into the folder again to clear the error node.

The Properties window (F4) displays all the properties for the currently selected item. Some of the properties (such as Font) may have subproperties, indicated by a plus sign next to their entries in the window. The property values on the right side of the window are editable.

One thing that can be confusing is that certain items have more than one set of properties. For example, a Form content file can show two different sets of properties, depending on whether you select the source file in the Solution Explorer or the form as shown in the Design view.

A typical Properties window is shown in Figure 2-9.

The name and type of the current object is displayed in the field at the top of the window. In Figure 2-9, it is an object named btnPage2 of type Button, contained in the System.Web.UI.WebControls namespace.

Most properties can be edited in place in the Properties window. The Font property has subproperties that may be set directly in the window by clicking on the plus sign to expand its subproperties, and then editing the subproperties in place.

The Properties window has several buttons just below the name and type of the object. The first two buttons on the left toggle the list by category or alphabetically. The next two buttons from the left toggle between displaying properties for the selected item and displaying events for the selected item. The rightmost button displays property pages for the object, if there are any.

The box below the list of properties displays a brief description of the selected property.

The Tools menu presents commands accessing a wide range of functionality, ranging from connecting to databases to accessing external tools to setting IDE options. Some of the more useful commands are described in the following sections.

The Code Snippets Manager command (Ctrl-K, Ctrl-B) brings up the Code Snippets Manager dialog box, which allows you to maintain the code snippets (described previously in the "IntelliSense" section). This dialog box allows you to add or remove code snippets for any of the supported languages. You can also import code snippets and search online for code snippets.