Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Pro CSharp And The .NET 2.0 Platform (2005) [eng]

.pdf
Скачиваний:
92
Добавлен:
16.08.2013
Размер:
10.35 Mб
Скачать

44CHAPTER 2 BUILDING C# APPLICATIONS

3.Enter the directory containing the input files via the Initial folder text box (C:\TextPadTestApp in this example).

4.If you wish TextPad to capture the compiler output directly (rather than within a separate command window), select the Capture Output check box.

Figure 2-6 shows the complete compilation settings.

Figure 2-6. Specifying a custom Run command

At this point, you can either run your program by double-clicking the executable using Windows Explorer or leverage the Tools Run menu option to specify myApp.exe as the current command (see Figure 2-7).

Figure 2-7. Instructing TextPad to run myApp.exe

When you click OK, you should see the program’s output (“Hello from TextPad”) displayed in the Command Results document.

Associating Run Commands with Menu Items

TextPad also allows you to create custom menu items that represent predefined run commands. Let’s create a custom item under the Tools menu named “Compile C# Console” that will compile all C# files in the current directory:

1.Activate the Configure Preferences menu option and select Tools from the tree view control.

2.Using the Add button, select Program and specify the full path to csc.exe.

CHAPTER 2 BUILDING C# APPLICATIONS

45

3.If you wish, rename csc.exe to a more descriptive label (Compile C#) by clicking the tool name and then clicking OK.

4.Finally, activate the Configure Preferences menu option once again, but this time select Compile C# from the Tools node, and specify *.cs as the sole value in the Parameters field (see Figure 2-8).

Figure 2-8. Creating a Tools menu item

With this, you can now compile all C# files in the current directory using your custom Tools menu item.

Enabling C# Code Snippets

Before leaving behind the world of TextPad, there is one final free add-on you might wish to install. Navigate to http://www.textpad.com/add-ons/cliplibs.html and download csharp_1.zip using the C# clip library provided by Sean Gephardt. Extract the contained csharp.tcl file and place it in the Samples subdirectory. When you restart TextPad, you should find a new clip library named C Sharp Helpers available from the Clip Library drop-down list (see Figure 2-9). Double-clicking any item will insert the related C# code in the active document at the location of the cursor.

Figure 2-9. C# code snippets à la TextPad

46 CHAPTER 2 BUILDING C# APPLICATIONS

As you may agree, TextPad is a step in the right direction when contrasted to Notepad and the command prompt. However, TextPad does not (currently) provide IntelliSense capabilities for C# code, GUI designers, project templates, or database manipulation tools. To address such needs, allow me to introduce the next .NET development tool: SharpDevelop.

Building .NET Applications Using SharpDevelop

SharpDevelop is an open source and feature-rich IDE that you can use to build .NET assemblies using C#, VB .NET, Managed Extensions for C++, or CIL. Beyond the fact that this IDE is completely free, it is interesting to note that it was written entirely in C#. In fact, you have the choice to download and compile the *.cs files manually or run a setup.exe program to install SharpDevelop on your development machine. Both distributions can be obtained from http://www.icsharpcode. net/OpenSource/SD/Download.

Once you have installed SharpDevelop, the File New Combine menu option allows you to pick which type of project you wish to generate (and in which .NET language). In the lingo of SharpDevelop, a combine is a collection of individual projects (analogous to a Visual Studio solution). Assume you have created a C# Windows Application named MySDWinApp (see Figure 2-10).

Figure 2-10. The SharpDevelop New Project dialog box

Note Be aware that version 1.0 of SharpDevelop is configured to make use of the C# 1.1 compiler. To make use of the new C# 2005 language features and .NET Framework 2.0 namespaces, you will need to activate the Project Project options menu item and update the compiler version from the Runtime/Compiler option page.

CHAPTER 2 BUILDING C# APPLICATIONS

47

Learning the Lay of the Land: SharpDevelop

SharpDevelop provides numerous productivity enhancements and in many cases is as feature rich as Visual Studio .NET 2003 (but not currently as powerful as Visual Studio 2005). Here is a hit list of some of the major benefits:

Support for the Microsoft and Mono C# compilers

IntelliSense and code expansion capabilities

An Add Reference dialog box to reference external assemblies, including assemblies deployed to the GAC

A visual Windows Forms designer

Various project perspective windows (termed scouts) to view your projects

An integrated object browser utility (the Assembly Scout)

Database manipulation utilities

A C# to VB .NET (and vice versa) code conversion utility

Integration with the NUnit (a .NET unit test utility) and NAnt (a .NET build utility)

Integration with the .NET Framework SDK documentation

Impressive for a free IDE, is it not? Although this chapter doesn’t cover each of these points in detail, let’s walk through a few items of interest. If you require further details of SharpDevelop, be aware that it ships with very thorough documentation accessible from the Help Help Topics menu option.

The Project and Classes Scouts

When you create a new combine, you can make use of the Project Scout to view the set of files, referenced assemblies, and resource files of each project (see Figure 2-11).

Figure 2-11. The Project Scout

When you wish to reference an external assembly for your current project, simply right-click the References icon within the Project Scout and select the Add Reference context menu. Once you do, you may select assemblies directly from the GAC as well as custom assemblies via the

.NET Assembly Browser tab (see Figure 2-12).

48

CHAPTER 2 BUILDING C# APPLICATIONS

Figure 2-12. The SharpDevelop Add Reference dialog box

The Classes Scout provides a more object-oriented view of your combine in that it displays the namespaces, types, and members within each project (see Figure 2-13).

Figure 2-13. The Classes Scout

If you double-click any item, SharpDevelop responds by opening the corresponding file and placing your mouse cursor at the item’s definition.

The Assembly Scout

The Assembly Scout utility (accessible from the View menu) allows you to graphically browse the assemblies referenced within your project. This tool is split into two panes. On the left is a tree view control that allows you to drill into an assembly and view its namespaces and the contained types (see Figure 2-14).

CHAPTER 2 BUILDING C# APPLICATIONS

49

Figure 2-14. Viewing referenced assemblies using the Assembly Scout

The right side of the Assembly Scout utility allows you to view details of the item selected on the left pane. Not only can you view the basic details using the Info tab, but also you can also view the underlying CIL code of the item and save its definition to an XML file.

Windows Forms Designers

As you will learn later in this book, Windows Forms is a toolkit used to build desktop applications. To continue tinkering with SharpDevelop, click the Design tab located at the bottom of the MainForm.cs code window. Once you do, you will open the integrated Windows Forms designer.

Using the Windows Forms section of your Tools window, you can create a GUI for the Form you are designing. To demonstrate this, place a single Button type on your main Form by selecting the Button icon and clicking the designer. To update the look and feel of any GUI item, you can make use of the Properties window (see Figure 2-15), which you activate from the View Properties menu selection. Select the Button from the drop-down list and change various aspects of the Button type (e.g., BackColor and Text).

Figure 2-15. The Properties window

50 CHAPTER 2 BUILDING C# APPLICATIONS

Using this same window, you can handle events for a given GUI item. To do so, click the lightning bolt icon at the top of the Properties window. Next, select the GUI item you wish to interact with from the drop-down list (your Button in this case). Finally, handle the Click event by typing in the name of the method to be called when the user clicks the button (see Figure 2-16).

Figure 2-16. Handing events via the Properties window

Once you press the Enter key, SharpDevelop responds by generating stub code for your new method. To complete the example, enter the following statement within the scope of your event handler:

void ButtonClicked(object sender, System.EventArgs e)

{

// Update the Form's caption with a custom message. this.Text = "Stop clicking my button!";

}

At this point, you can run your program (using the Debug Run menu item). Sure enough, when you click your Button, you should see the Form’s caption update as expected.

That should be enough information to get you up and running using the SharpDevelop IDE. I do hope you now have a good understanding of the basics, though obviously there is much more to this tool than presented here.

Building .NET Applications Using Visual C# 2005

Express

During the summer of 2004, Microsoft introduced a brand-new line of IDEs that fall under the designation of “Express” products (http://msdn.microsoft.com/express). To date, there are six members of the Express family:

Visual Web Developer 2005 Express: A lightweight tool for building dynamic websites and XML web services using ASP.NET 2.0

Visual Basic 2005 Express: A streamlined programming tool ideal for novice programmers who want to learn how to build applications using the user-friendly syntax of Visual Basic .NET

Note

CHAPTER 2 BUILDING C# APPLICATIONS

51

Visual C# 2005 Express,Visual C++ 2005 Express, and Visual J# 2005 Express: Targeted IDEs for students and enthusiasts who wish to learn the fundamentals of computer science in their syntax of choice

SQL Server 2005 Express: An entry-level database management system geared toward hobbyists, enthusiasts, and student developers

Note At the time of this writing, the Express family products are available as public betas free of charge.

By and large, Express products are slimmed-down versions of their Visual Studio 2005 counterparts and are primarily targeted at .NET hobbyists and students. Like SharpDevelop, Visual C# 2005 Express provides various browsing tools, a Windows Forms designer, the Add References dialog box, IntelliSense capabilities, and code expansion templates. As well, Visual C# 2005 Express offers a few (important) features currently not available in SharpDevelop, including

An integrated graphical debugger

Tools to simplify access to XML web services

Because the look and feel of Visual C# 2005 Express is so similar to that of Visual Studio 2005 (and, to some degree, SharpDevelop) I do not provide a walk-through of this particular IDE here. If you do wish to learn more about the product, look up my article “An Introduction to Programming Using Microsoft Visual C# 2005 Express Edition” online at http://msdn.microsoft.com.

The Big Kahuna: Building .NET Applications Using

Visual Studio 2005

If you are a professional .NET software engineer, the chances are extremely good that your employer has purchased Microsoft’s premier IDE, Visual Studio 2005, for your development endeavors (http://msdn.microsoft.com/vstudio). This tool is far and away the most feature-rich and enterpriseready IDE examined in this chapter. Of course, this power comes at a price, which will vary based on the version of Visual Studio 2005 you purchase. As you might suspect, each version supplies a unique set of features.

My assumption during the remainder of this text is that you have chosen to make use of Visual Studio 2005 as your IDE of choice. Do understand that owning a copy of Visual Studio 2005 is not required for you to use this edition of the text. In the worst case, I may examine an option that is not provided by your IDE. However, rest assured that all of this book’s sample code will compile just fine when processed by your tool of choice.

Once you download the source code for this book from the Downloads area of the Apress website (http://www.apress.com), you may load the current example into Visual Studio 2005 by double-clicking the example’s *.sln file. If you are not using Visual Studio 2005, you will need to manually configure your IDE to compile the provided *.cs files.

52

CHAPTER 2 BUILDING C# APPLICATIONS

Learning the Lay of the Land: Visual Studio 2005

Visual Studio 2005 ships with the expected GUI designers, database manipulation tools, object and project browsing utilities, and an integrated help system. Unlike the IDEs we have already examined, Visual Studio 2005 provides numerous additions. Here is a partial list:

Visual XML editors/designers

Support for mobile device development (such as Smartphones and Pocket PC devices)

Support for Microsoft Office development

The ability to track changes for a given source document and view revisions

Integrated support for code refactoring

An XML-based code expansion library

Visual class design tools and object test utilities

A code definition window (which replaces the functionality of the Windows Forms Class Viewer, wincv.exe, which shipped with .NET 1.1 and earlier)

To be completely honest, Visual Studio 2005 provides so many features that it would take an entire book (a large book at that) to fully describe every aspect of the IDE. This is not that book. However, I do want to point out some of the major enhancements in the pages that follow. As you progress through the text, you’ll learn more about the Visual Studio 2005 IDE where appropriate.

The Solution Explorer Utility

If you are following along, create a new C# console application (named Vs2005Example) using the File New Project menu item. The Solution Explorer utility (accessible from the View menu) allows you to view the set of all content files and referenced assemblies that comprise the current project (see Figure 2-17).

Figure 2-17. Solution Explorer

Notice that the References folder of Solution Explorer displays a list of each assembly you have currently referenced (console projects reference System.dll, System.Data.dll, and System.Xml.dll by default). When you need to reference additional assemblies, right-click the References folder and select Add Reference. At this point, you can select your assembly from the resulting dialog box.

CHAPTER 2 BUILDING C# APPLICATIONS

53

Note Visual Studio 2005 now allows you to set references to executable assemblies (unlike Visual Studio .NET 2003, in which you were limited to *.dll code libraries).

Finally, notice an icon named Properties within Solution Explorer. When you double-click this item, you are presented with an enhanced project configuration editor (see Figure 2-18).

Figure 2-18. The Project Properties window

You will see various aspects of the Project Properties window as you progress through this text. However, if you take some time to poke around, you will see that you can establish various security settings, strongly name your assembly, insert string resources, and configure preand postbuild events.

The Class View Utility

The next tool to examine is the Class View utility, which you can load from the View menu. Like SharpDevelop, the purpose of this utility is to show all of the types in your current project from an object-oriented perspective. The top pane displays the set of namespaces and their types, while the bottom pane displays the currently selected type’s members (see Figure 2-19).