Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Microsoft CSharp Programming For The Absolute Beginner (2002) [eng].pdf
Скачиваний:
39
Добавлен:
16.08.2013
Размер:
15.71 Mб
Скачать

Figure 6.10: The program says a Windows−style hi, without a line of code!

Running the Program

The .NET editor makes creating a simple GUI program very easy. When you click the Run icon, you see the working version of your program. Figure 6.10 features my version.

This program looks good and is very easy to write. You don’t add a single line of code. However, the program is based on code, so you need to understand how the code is built behind the scenes. Sometimes you will want to write code without the graphical editor You will learn how to do this in Chapter 8 “Arrays: The Soccer Game.” More often, you will find that the editor makes a mistake you must correct. Of course usually you will add more code so that your program does something. Looking at the code generated by the editor makes sense.

Examining the Code of a Windows Program

When you create a new Windows application, the editor shows you a graphical representation of the form, not the form itself. This graphical window is called the Visual Designer. Your program is still based on code. The Designer view simply gives you an alternative way of viewing and modifying the code. To see the underlying code, choose Code Window from the View menu. Figure 6.11 shows the editor displaying the code for the Hello GUI.

139

Figure 6.11: This code creates the Hello GUI program.

As you scroll through the code, you see that the editor wrote a lot of code. Although the editor writes this automatically, it is not perfect. I’ll show you the automatically generated code and explain what it is doing.

Adding New Namespaces

When you create a console program, the editor presumes that you will want access to the System namespace. In a Windows program, you need access to other namespaces as well. All the visual components are objects, and most of them live in the System.Windows.Forms namespace.

Additionally, all the objects used to position controls on a form belong in another important namespace, System.Drawing. When you create a Windows program, the C# editor automatically attaches references to these two critical namespaces, and a few others as well:

using System;

using System.Drawing; using System.Collections;

using System.ComponentModel; using System.Windows.Forms; using System.Data;

For this simple program, you need not worry about the other namespaces, but it doesn’t hurt to leave them in.

The System.Windows.Forms Namespace

Most of the controls visible on the toolbar are housed in an important namespace, System.Windows.Forms. If you include access to this namespace, your programs can use forms and all the other controls on the Toolbox. Not surprisingly, nearly every Windows−based program uses this namespace. Peruse the object browser or documentation to see all that this namespace has to offer. Be warned, though. The namespace is huge, and you don’t need to memorize all the

140

various elements. It’s just nice to know what’s there. Each of the components is a full−blown object, with properties, methods, and events. To demonstrate some features of the namespace, Figure 6.12 shows the documentation for the Form class.

Figure 6.12: The Form class is a very powerful part of the System. Windows.Forms namespace, with many useful characteristics.

In the Real World

Even the pros don’t memorize all these details; they usually have a help screen and keep a reference book around.

You need to know how to dig around and find out the characteristics of components because learning them all is impossible. The more important skill is knowing how to find what you need when you need it. You solve most of your GUI problems by thinking about what kind of component might help. If a control or other object isn’t the solution, a property or method of an object probably is. As you can see, understanding how objects work is the key to modern programming.

Inheritance and Related Components

When you look at the documentation for the Form class, you see that the form object supports a huge number of properties and methods (as well as events, which you’ll investigate shortly). However, the form object is even more powerful than it appears! If you look back at Figure 6.12, you can see a miniature outline showing how the form is related to some other objects. This shows the form object’s family tree. Because the form is a descendant of the ContainerControl class, it has features that enable it to hold other controls. This class is descended from the ScrollableControl class, which is inherited from Control.

Each ancestor of the Form class adds new capabilities to the object. This means that the Form can

141

act just like other controls but also has new features of its own. This is good for you. When you learn which features a Control has, for example, you have a head start on everything that derives from the control object. For example, the Control class has a backColor property that determines the object’s background color. Because almost all the widgets you can place on a form (and the form itself) have the Control class in their family tree, all of them have access to the BackColor property.

The authors of the .Net framework cleverly used inheritance to simplify their work. (Adding a BackColor property to every component is unnecessary because most components are derived from the Control class, which already has the property.) It also makes your job as a programmer easier. When you understand the inheritance pattern of an object, you know a lot about what it can do. You also have some understanding of unfamiliar components because they usually share characteristics with controls you already know.

The System.Drawing Namespace

Adding a reference to the System.Drawing namespace is necessary because this namespace provides access to certain drawing elements used in the visual designer. Most important of these is the Point class, which is used to determine the size and placement of objects on the screen. The System.Drawing namespace also defines Size and Color. You can look through this namespace in the documentation as well, but most of the classes you find there will not be useful until you have learned how to add event−handling capabilities to your programs.

The Other Namespaces

The editor provides references to other namespaces as well. They are used with enough frequency that the editor puts them in for you. For now, you can safely ignore them, but you will get a chance to play around with some of them later on.

Creating the Form Object

The editor creates all the code necessary to build a basic version of your form. Here is the code that handles basic form creation:

namespace HiGui

{

///<summary>

///Summary description for Form1.

///</summary>

public class Form1 : System.Windows.Forms.Form

{

private System.Windows.Forms.Label label1;

///<summary>

///Required designer variable.

///</summary>

private System.ComponentModel.Container components = null;

public Form1()

{

//

// Required for Windows Form Designer support

//

142

Соседние файлы в предмете Программирование на C++