Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer

Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer

3.2 4
by Rod Stephens

A unique book-and-DVD package from preeminent programming instructor Rod Stephens

Visual C# has become a leading programming language, resulting in greater career opportunities for Visual C# programmers. This Wrox guide literally shows novice programmers how to program in C# with Microsoft Visual Studio, using both written and visual instruction.


…  See more details below


A unique book-and-DVD package from preeminent programming instructor Rod Stephens

Visual C# has become a leading programming language, resulting in greater career opportunities for Visual C# programmers. This Wrox guide literally shows novice programmers how to program in C# with Microsoft Visual Studio, using both written and visual instruction.

Easy-to-follow lessons reinforced with step-by-step instructions, screencasts, and supplemental exercises make it easy to master Visual C# programming regardless of your learning style. Each lesson begins with a discussion of a concept or technique and proceeds through step-by-step directions for using the technique Visual C# has surpassed Visual Basic as the most popular programming language

Coverage Includes:

  • Getting Started with the Visual Studio IDE
  • Creating Controls
  • Making Controls Arrange Themselves
  • Handling Events
  • Making Menus
  • Making Tool Strips and Status Strips
  • Using RichTextBoxes
  • Using Standard Dialogs
  • Creating and Displaying New Forms
  • Building Custom Dialogs
  • Using Variables and Performing Calculations
  • Debugging Code
  • Understanding Scope
  • Working with Strings
  • Working with Dates and Times
  • Using Arrays and Collections
  • Using Enumerations and Structures
  • Making Choices
  • Repeating Program Steps
  • Handling Errors
  • Preventing Bugs
  • Defining Classes
  • Initializing Objects
  • Fine-Tuning Classes
  • Overloading Operators
  • Using Interfaces
  • Making Generic Classes
  • Reading and Writing Files
  • Using File System Classes
  • Printing
  • Using the Clipboard
  • Providing Drag and Drop
  • Localizing Programs
  • Programming Databases
  • LINQ to Objects
  • LINQ to SQL
  • Drawing with GDI+
  • Making WPF Applications
  • Printing with WPF

Note: As part of the print version of this title, video lessons are included on DVD. For e-book versions, video lessons can be accessed at using a link provided in the interior of the e-book.

Read More

Product Details

Publication date:
Sales rank:
Product dimensions:
7.40(w) x 9.20(h) x 1.20(d)

Read an Excerpt

Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer

By Rod Stephens

John Wiley & Sons

Copyright © 2010 John Wiley & Sons, Ltd
All right reserved.

ISBN: 978-0-470-59690-6

Chapter One

Getting Started with the Visual Studio IDE

The Visual Studio integrated development environment (IDE) plays a central role in Visual C# development. In this lesson you explore the IDE. You learn how to configure it for Visual C# development, and you learn about some of the more useful of the IDE's windows and what they do. When you finish this lesson, you'll know how to create a new project. It may not do much, but it will run and will prepare you for the lessons that follow.


Before you can use C# to write the next blockbuster first-person Xbox game, you need to install it. So if you haven't done so already, install C#.

You can install the Express Edition at If you think you need some other version (for example, you're working on a big project and you need test management, source code control, and other team programming tools), go to vcsharp and install the version that's right for you.

It's a big installation, so it could take a while.


When you first run Visual Studio, it asks how you want to configure the IDE. You can pick settings for general development, Visual Basic, Visual C#, and so forth. Because you're going to be focusing on C# development, select that option.

These settings determine such things as what keystrokes activate certain development features. You can certainly write C# programs with the Visual C++ settings but we may as well be on the same page, so when I say, "Press F5," the IDE starts your program instead of displaying a code window or whatever Visual C++ thinks F5 means.

If you ever want to switch to different settings (for example, if you got carried away during installation and selected the general settings and now want the C# settings), you can always change them later.

To change the settings later, open the Tools menu and select Import and Export Settings to display the Import and Export Settings Wizard. You can use this tool to save your current settings, reload previously saved settings, or reset settings to default values.

To reset settings, select the Reset All Settings option on the wizard's first page and click Next.

On the next page, indicate whether you want to save your current settings. When you've made your choice, click Next to display the page shown in Figure 1-1. Select the Visual C# Development Settings choice and click Finish. (Then sit back and wait. Or better still, go get a coffee because this could take a while. Visual Studio has a lot of settings to reset, and it could take several minutes depending on how fast and busy your computer is.)


Now that you've installed C#, you're ready to get started. Launch Visual Studio by double-clicking its desktop icon or by selecting it from the system's Start menu.

To create a new project, press [Ctrl]+[Shift]+N to display the New Project dialog box shown in Figure 1-2. Alternatively, you can open the File menu, expand the New submenu, and select Project.

Expand the Visual C# project type folder on the left and select the template for the type of project that you want to build on the right. For most of this book, that will be a Visual C# Windows Forms Application.

Below the list of project types, you need to enter several pieces of information.

* Name - This is the application's name. Visual Studio creates a folder with this name to hold the program's files. It also sets some key values in the project to this name.

* Location - This is where you want Visual Studio to put the project's folder.

* Solution Name - If the Create Directory for Solution box is checked (which it is by default), Visual Studio creates a folder with this name at the location you entered. It then places the application's folder inside the solution's folder.

So if the Create Directory for Solution box is checked, you get a filesystem layout that looks like this:

SolutionFolder SolutionFiles

ApplicationFolder ApplicationFiles

If the Create Directory for Solution box is not checked, you get a filesystem layout that looks like this:

ApplicationFolder ApplicationFiles

An application contains a single program. A solution can contain several applications. A solution is useful when you want to build applications that go closely together. It's particularly useful if you want to build a library of routines plus an executable program to test the library.

The applications you build in this book are single programs so they don't really need to be inside a separate solution folder. Most of the time, I uncheck the Create Directory for Solution box to keep my filesystem simpler.

By default, Visual Studio places new projects in your Projects folder at some obscure location such as C:\Users\MyUserName\Documents\Visual Studio 2010\Projects. Later it can be hard finding these projects in Windows Explorer (for example, to make a copy). To make finding projects easier, set the location to something more intuitive such as the desktop or a folder on the desktop. The next time you create a new project, Visual Studio will initialize the location textbox to this same location, so from now on it'll be easy to find your projects.

If you open the New Project dialog box while you have another project open, you'll see an additional dropdown that lists the choices Create New Solution and Add to Solution. The first choice closes the current solution and creates a new one. The second choice adds the new application to the solution you currently have open. Normally you'll want to create a new solution.

After you display the New Project dialog box and enter a Name, Location, and Solution Name, click OK. The result should look like Figure 1-3.

If you have previously edited a project, you can quickly reload it from the File menu's Recent Projects submenu. You can also load a solution into the IDE by double-clicking the solution's .sln file.

The rest of this lesson deals with the features available in Visual Studio, some of which are displayed in Figure 1-3. Before you launch into an inventory of useful features, however, press F5 or open the Debug menu and select Start Debugging to run your new program. Figure 1-4 shows the result. Admittedly this first program isn't very fancy, but by the same token you didn't need to do much to build it.

This first program may not seem terribly impressive but there's a lot going on behind the scenes. C# has built a form with a bunch of useful features, including:

* A resizable border and draggable title bar. * Minimize, maximize, and close buttons in the upper-right corner. * A system menu in the upper-left corner that contains the commands Restore, Move, Size, Minimize, Maximize, and Close. * An icon in the system taskbar. * The ability to use [Alt]+[Tab] and Flip3D ([Win]+[Tab]) to move between the application and others. * Other standard window behaviors. For example, if you double-click the form's title bar it maximizes (or restores if it is already maximized), and if you press [Alt]+F4, the form closes.

Unless you're an absolute beginner to Windows, you probably take all of these features for granted, but providing them is actually a lot of work. Not too long ago you would have had to write around 100 lines of code to handle these sorts of issues. Now Visual Studio automatically builds a form that handles most of these details for you.

You can still get in and change the way things work if you want to (for example, you can set a form's minimum and maximum sizes) but usually you can ignore all of these issues and concentrate on your particular application, not the Windows decorations.


Sometimes you may want to copy a project. For example, you might want to save the current version and then make a new one to try things out. Or you may want to give a copy of the project to a friend or your programming instructor.

To make a copy, you might look in the File menu and see the Copy As commands. Don't be tempted! Those commands copy single files, not the entire project. Later when you try to open one of those files, you'll discover that Visual Studio cannot find all of the other project pieces that it needs and you'll be left with nothing usable.

To correctly copy a project, find the solution or application folder in Windows Explorer and copy the project's entire directory hierarchy. Alternatively, you can compress the project directory into a compressed or zipped file and then copy that. Just be sure that whatever copying method you use brings along all of the project's files.

Note that you can delete the bin and obj subdirectories if you like to save space. Visual Studio will re-create them when it needs them later.

Compressing a project into an archive is very useful because it keeps all of its files together in a package. In particular, if you ever need to e-mail a project to someone (for example, if you e-mail me at for help), you can remove the bin and obj directories, compress the project folder, and e-mail the package as a single file. (If you're sending the project to your instructor as part of an assignment, rename the compressed file so it contains your name and the name of the assignment, for example,


The Visual Studio IDE contains a huge number of menus, toolbars, windows, wizards, editors, and other components to help you build applications. Some of these, such as the Solution Explorer and the Properties window, you will use every time you work on a program. Others, such as the Breakpoints window and the Connect to Device dialog box, are so specialized that it may be years before you need them.

Figure 1-5 shows the IDE with a simple project loaded with some of the IDE's most important pieces marked. The following list describes those pieces.

1. Menus - The menus provide all sorts of useful commands. Exactly which commands are available, which are enabled, and even which menus are visible depends on what kind of editor is open in the editing area (#4). Some particularly useful menus include File (opening old projects and creating new ones), View (finding windows), Project (adding new forms and other items to a project), Debug (build, run, and debug the project), and Format (arrange controls on a form). 2. Toolbars - The toolbars provide shortcuts for executing commands similar to those in the menus. Use the Tools menu's Customize command to determine which toolbars are visible. 3. Solution Explorer - The Solution Explorer lists the files in the project. One of the most important is Form1.cs, which defines the controls and code for the form named Form1. If you double-click a file in the Solution Explorer, the IDE opens it in the editing area. 4. Editing Area - The editing area displays files in appropriate editors. Most often you will use this area to design a form (place controls on it and set their properties) and write code for the form, but you can also use this area to edit other files such as text files, bitmaps, and icons. 5. Toolbox - The Toolbox contains controls and components that you can place on a form. Select a tool and then click and drag to put a copy of the tool on the form. Notice that the Toolbox groups controls in tabs (All Windows Forms, Common Controls, Containers, Menus & Toolbars, and so on) to make finding the controls you need easier.

6. Properties Window - The Properties window lets you set control properties. Click a control on the Form Designer (shown in the editing area in Figure 1-5) to select it, or click and drag to select multiple controls. Then use the Properties window to set the control(s) properties. Notice that the top of the Properties window shows the name (label1) and type (System.Windows .Forms.Label) of the currently selected control. The currently selected property in Figure 1-5 is Text, and it has the value First Name:.

7. Property Description - The property description gives you a reminder about the current property's purpose. In Figure 1-5, it says that the Text property gives the text associated with the control. (Duh!) 8. Other Windows - This area typically contains other useful windows. The tabs at the bottom let you quickly switch between different windows.

Figure 1-5 shows a fairly typical arrangement of windows but Visual Studio is extremely flexible so you can rearrange the windows if you like. You can hide or show windows; make windows floating or docked to various parts of the IDE; make windows part of a tab group; and make windows automatically hide themselves if you don't need them constantly.

If you look closely at the right side of the title bar above one of the windows in Figure 1-5, for example, the Properties window, you'll see three icons: a dropdown arrow ([??]), a thumbtack ([??]), and an x.

If you click the dropdown arrow (or right-click the window's title bar), a menu appears with the following choices:

* Float - The window breaks free of wherever it's docked and floats above the IDE. You can drag it around and it will not re-dock. To make it dockable again, open the menu again and select Dock. * Dock - The window can dock to various parts of the IDE. I'll say more about this shortly. * Dock as Tabbed Document - The window becomes a tab in a tabbed area similar to #8 in Figure 1-5. Unfortunately, it's not always obvious which area will end up holding the window. To make the window a tab in a specific tabbed area, make it dockable and drag it onto a tab (described shortly). * Auto Hide - The window shrinks itself to a small label stuck to one of the IDE's edges and its thumbtack icon turns sideways ([??]) to indicate that the window is auto-hiding. If you float the mouse over the label, the window reappears. As long as the mouse remains over the expanded window, it stays put, but if you move the mouse off the window, it auto-hides itself again. Select Auto Hide again or click the sideways thumbtack to turn off auto-hiding. Auto-hiding gets windows out of the way so you can work in a bigger editing area. * Hide - The window disappears completely. To get the window back, you'll need to find it in the menus. You can find many of the most useful windows in the View menu, the View menu's Other Windows submenu, and the Debug menu's Windows submenu.

The thumbtack in a window's title bar works just like the dropdown menu's Auto Hide command does. Click the thumbtack to turn on auto-hiding. Expand the window and click the sideways thumbtack to turn off auto-hiding. (Turning auto-hiding off is sometimes called pinning the window.)

The x symbol in the window's title bar hides the window just like the dropdown menu's Hide command does.

In addition to using a window's title bar menu and icons, you can drag windows into new positions. As long as a window is dockable or part of a tabbed window, you can grab its title bar and drag it to a new position.

As you drag the window, the IDE displays little drop targets to let you dock the window in various positions. If you move the window so the mouse is over a drop target, the IDE displays a translucent blue area to show where the window will land if you drop it. If you drop when the mouse is not over a drop target, the window becomes floating.

Figure 1-6 shows the Properties window being dragged in the IDE. The mouse is over the right drop target above the editing area so, as the translucent blue area shows, dropping it there would dock the window to the right side of the editing area.

The drop area just to the left of the mouse represents a tabbed area. If you drop on this kind of target, the window becomes a tab in that area.


Excerpted from Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer by Rod Stephens Copyright © 2010 by John Wiley & Sons, Ltd. Excerpted by permission.
All rights reserved. No part of this excerpt may be reproduced or reprinted without permission in writing from the publisher.
Excerpts are provided by Dial-A-Book Inc. solely for the personal use of visitors to this web site.

Read More

Customer Reviews

Average Review:

Write a Review

and post it to your social network


Most Helpful Customer Reviews

See all customer reviews >

Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer 3.3 out of 5 based on 0 ratings. 4 reviews.
Anonymous More than 1 year ago
Anonymous More than 1 year ago
Anonymous More than 1 year ago
Anonymous More than 1 year ago