Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Manning - Windows Forms Programming With CSharp.pdf
Скачиваний:
72
Добавлен:
24.05.2014
Размер:
14.98 Mб
Скачать

line programs does the real work of compiling and linking programs. In this chapter, we will use the same command-line tools employed by Visual Studio .NET internally. This will allow us to focus on C# and Windows Forms concepts, and not discuss Visual Studio .NET until the next chapter.

If you have prior experience with Windows programming, you will see many similarities in the names of the .NET controls. This chapter will show some of these names, and introduce some new terms and features as well. If you are new to Windows programming, you’ll find this chapter a good foundation for the remainder of the book.

This chapter is a bit of a wild ride through .NET, so don’t worry too much about the details here. The concepts and topics in this chapter should become clearer as we progress through the book.

This chapter assumes you have successfully installed the Microsoft .NET Framework SDK on your computer.

1.1Programming in C#

Let’s create a blank form in C# to see how a program compiles and runs in the .NET Framework. Such a form is shown in figure 1.1. This is the most basic of Windows applications that can be created in .NET. You may be tempted to skip this section, but don’t: the remainder of this chapter builds on this most basic of forms, so you’ll want to have it ready.

Figure 1.1

Our first Windows Forms program produces this skeleton form. We'll build on this program throughout the rest of this chapter.

Crank up your favorite editor and type in the code shown in listing 1.1. If you’re not sure which editor to use, type this code into Notepad here and throughout the chapter. Save this file as “MyForm.cs” in a convenient directory. Note that “cs” is the standard extension used for C# files.

4

CHAPTER 1 GETTING STARTED WITH WINDOWS FORMS

Listing 1.1 Your first form

[assembly: System.Reflection.AssemblyVersion("1.1")]

namespace MyNamespace

{

public class MyForm : System.Windows.Forms.Form

{

public MyForm()

{

this.Text = "Hello Form";

}

public static void Main()

{

System.Windows.Forms.Application.Run(new MyForm());

}

}

}

To compile this program, we will use the C# compiler, called csc, for C sharp compiler. You will need a command prompt with the PATH environment set to access the

.NET Framework programs and libraries. You can define these settings by hand or via a batch program, or use the shortcut Microsoft provides to do this for you. We will employ the shortcut, which is available via the Start menu.

To reach this shortcut, click the Start menu, then Programs, then Microsoft Visual Studio .NET, then Visual Studio .NET Tools, then Visual Studio .NET Command Prompt. This item opens a command window and executes a batch file that sets the appropriate environment variables. With the default installation directories, this menu item executes the following command:

cmd /k "C:\Program Files\Microsoft Visual Studio .NET\

Common7\Tools\vsvars32.bat"

Open a Visual Studio .NET command prompt as previously described and compile the program using the following command.

> csc MyForm.cs /reference:System.dll

/reference:System.Windows.Forms.dll

The /reference switch specifies a library containing additional functionality for the program. In .NET, libraries as well as programs are referred to as assemblies. For our application, we reference the System assembly (System.dll) and the Windows Forms assembly (System.Windows.Forms.dll).1

1 Strictly speaking, the csc compiler automatically references all major System DLLs. As a result, the /reference switches here are not really needed. We use them here and throughout the chapter to be explicit about the libraries required by our program.

PROGRAMMING IN C#

5

Once this command completes, you should see a MyForm.exe file in your directory. Run the program using the myform command to see the result. You should see a window similar to figure 1.1.

> myform2

While our program is not very useful yet, it only took us a few lines of code to create a fully functional Windows application. Most of the work is done internally by the

.NET Framework and Windows. This includes drawing the outer portion of the window such as the title bar and frame; handling the taskbar and standard windows interactions such as minimize, maximize, move, resize, and close; and redrawing the window when the application is behind, in front of, or obscured by other windows.

Stand up, stretch, stifle a yawn, and go tell your neighbor that you just wrote your first .NET Windows Forms application.

We will add bells and whistles to this application, of course. But before we do, our fully functional program warrants some discussion. Let’s break down the parts of our code to examine how the .NET Framework executes our program.

The first line of the program simply sets the version number for the program to 1.1, matching the section number of the book.

[assembly: System.Reflection.AssemblyVersion("1.1")]

You can verify this by right-clicking the myform.exe file, selecting the Properties item, and then clicking the Version tab. We’ll look at version numbers more closely in chapter 2, so we will not discuss this line any further at this point.

1.1.1Namespaces and classes

The introduction discussed the use of namespaces in .NET to define a scope for a set of classes and other types. In our program we use the namespace keyword to declare a new namespace called MyNameSpace.

namespace MyNamespace

{

. . .

}

A namespace contains one or more types, such as the class MyForm in our program. A class defines a new data abstraction, in that it defines a class name and a collection of members for representing and operating on the class. A class is just one of the types possible in a namespace. We will discuss additional types further along in the book, or you can visit appendix A for a complete listing of the possible types.

Classes in C# support single inheritance, in that each class inherits from at most one other class. As a quick description of inheritance, suppose you wanted to design

2When you run this program, you will note that the console waits for the application to exit. This is because the compiler creates a console application by default. We will see how to create a Windowsbased application using the /target switch in chapter 5.

6

CHAPTER 1 GETTING STARTED WITH WINDOWS FORMS

a program to track the traffic patterns in a city. You might want to differentiate between cars, trucks, delivery vehicles, buses, and other types of vehicles. It would be beneficial to define a core set of functions that all types of vehicles would employ, and then define additional functions for each type of vehicle as required. With inheritance, a Vehicle class could define this base functionality, and subsequent classes, called derived classes, would define additional functions for each vehicle type. For example, you might have the following:

namespace Traffic

{

//The base Vehicle class class Vehicle

{

. . .

}

//The Car class is derived from the Vehicle class class Car : Vehicle

{

. . .

}

//The Bus class is derived from the Vehicle class class Bus : Vehicle

{

. . .

}

}

Back to our program, we define a class called MyForm that inherits from the Form class, which is found in the System.Windows.Forms namespace. The period notation is used to separate namespaces and classes, so that the complete, or fully qualified, name for the class is System.Windows.Forms.Form. We will see how to abbreviate this name later in the chapter.

namespace MyNamespace

{

public class MyForm : System.Windows.Forms.Form

{

. . .

}

}

The Form class is the cornerstone of Windows-based applications in .NET. It represents any type of window in an application, from dialog boxes to MDI (Multiple Document Interface) client windows. The Form class provides the ability to display, place controls within, and interact with an application window. We will discuss this class in detail in chapter 7, and dialog boxes and MDI applications in chapters 8 and 16, respectively. For now, simply understand that the Form class represents the application’s main window.

PROGRAMMING IN C#

7