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

Visual CSharp 2005 Express Edition (2006) [eng]

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

Chapter 3

Figure 3-4

3.Select the project you want to use. The project is displayed again, with the settings as they were in Figure 3-3. It’s time now to open the Properties pane.

4.Select View Properties, the Properties pane will now be displayed as shown in Figure 3-5.

34

Quick Start Creating Your First C# Express Windows Project

Figure 3-5

5.Place the mouse on the left side of the Solution Explorer so that the mouse changes to the two arrows going up and down. See Figure 3-6.

35

Chapter 3

Figure 3-6

6.Press and hold the left mouse button, dragging the border of the pane to the left.

7.Release the mouse button when you can see all of the text in the Solution Explorer. Next it is time to open and pin the Toolbox.

8.Place the cursor over the Toolbox tag, located on the left side of the IDE. The Toolbox pane slides out.

9.Highlight the pin located next to Close button of the pane. In Figure 3-7, the pin is highlighted and the Auto Hide text is displayed. If you click this button, the Toolbox pane remains open.

10.Click the auto hide pin.

You are now ready to work on the Windows application project.

Right now if you run this application, you will just get a blank form, which is not very exciting on the surface. But a blank form in this context isn’t completely unexciting when you consider what has been done. Despite being blank, C# Express has created a lot of Windows functionality — moving the form, resizing the form, minimizing, maximizing, and so on.

In a few pages, you will add a couple of controls and display the result. But first take a look at what is included when you created the form, and what is displayed in the Solution Explorer.

36

Quick Start Creating Your First C# Express Windows Project

Figure 3-7

Overview of the Solution Explorer

The Solution Explorer was discussed briefly in Chapter 1, “Starting Strong with Visual C# 2005 Express Edition,” explaining that it is the pane in the C# Express IDE that helps you maintain your projects.

Project Elements Controlled Using the Solution Explorer

With the Solution Explorer, you will:

View various files used in the project. Projects are made up of multiple types of files, with extensions. The next section describes some of those types of files and their purposes. Besides double-clicking on a file with the left mouse button to edit a file, you can right-click a file and perform various tasks depending on the type of file.

Use references. These references point to or include .NET assemblies, which are libraries of code that can be used to accomplish the tasks in your application. References can also be made to include other projects you have created and want to use in the current application. You will learn more about .NET assemblies in the Chapter 4, “Introducing .NET.” You can see the references set up by C# Express by default in Figure 3-9.

37

Chapter 3

Figure 3-8

Figure 3-9

Use project properties. Properties describe different aspects, or features, of whatever object they are on. For example, you can have properties that describe forms and controls, as explained further in the section titled “Discussion about Properties,” later in this chapter. In the case of the

38

Quick Start Creating Your First C# Express Windows Project

project properties, various files are stored that describe various aspects of the project. The good news is you don’t have to do anything with the files in the short term. Later on as you get more advanced in your programming skills, you may have to but not for a while.

Files Used in Windows Application Projects

Depending on which kind of project you are working with, you will have different file types displayed in the Solution Explorer. In the current type, you can see two files with the extension of .cs (C#). Notice also that even though both have the extension of cs, they have different icons representing the type of file they are.

You can create additional folders and store various types of the files, including graphic files or whatever you need for your project. Which files are used will totally depend on your project. Also, under the covers there are folders and files that are used for the project that aren’t displayed by default. An example is that forms consist of two files: the code file and the designer information file. You can see these files by choosing Project Show All Files. A plus (+) symbol appears by the form files. If you click the plus symbol, another file appears. In Figure 3-10 a couple of folders also are displayed.

Figure 3-10

For the most part, you really don’t need to see all the files necessary for the project, because C# Express handles most of them for you and only has you deal with the necessary ones. But it is nice to know that you have the capability to track additional files and check out the ones that are there in the Explorer.

Discussion about Properties

Before jumping into adding to the Windows project you created, we should discuss properties in greater detail. As mentioned, properties, also called attributes, describe something about their object. A good example is the properties of the form you created in the first Try It Out. You can see some of those properties listed in the property sheet in Figure 3-11.

You can set properties for various objects such as controls and forms during both design time, as shown in Figure 3-11, and runtime, when the application is running. The various categories of properties for forms and controls are detailed in Chapter 8 “Working with Forms and Controls.”

39

Chapter 3

Figure 3-11

Try It Out

Change the Caption in a Form

You can see in Figure 3-11 that the value types in a Text property of a form is actually the caption displayed in the title bar of a form. Taking the project you create at the start of the chapter:

1.Double-click Form1.cs in the Solution Explorer if it is not already opened.

2.In the Properties pane, locate the Text property of the form.

3.Type My First Form in the Text property. You can see this in Figure 3-12.

Figure 3-12

40

Quick Start Creating Your First C# Express Windows Project

When you leave the property, you will see the caption change at the top of the form displayed in the design mode.

4.Press F5 to run the application. C# Express tests and builds your application for you, then runs it. The final form is shown in Figure 3-13.

Figure 3-13

Not very exciting, but hey, you’ve got to start somewhere. Now it is time to add some controls to the form and make it a little more interesting. Click the Close button, which is the X in the top right corner of the form.

Adding Controls to the Form

A form doesn’t do much good by itself without any controls to display or input data with. There are hundreds of controls that you can use on forms. Some of the most common controls are shown in the following list. These controls also are used in the rest of this chapter.

Text boxes. Used for enabling input or display of text.

Labels. Used to display text.

Buttons. Also called command buttons, code can be attached to these to perform tasks.

Adding controls to your form using the designer is as simple as dragging and dropping them onto the form. Adding code to them and using events as described in Chapter 2 takes a little more work. Once you have added controls to a form, you can resize, move, and modify their properties as needed. These actions are discussed further in Chapter 8, “Working with Forms and Controls.”

Because the purpose of this chapter is to get you going with creating the form, let’s get busy. For the purpose of the remainder of this chapter, you will be adding three text boxes and a command button. In a separate Try It Out, you will then add code to the command button that will take the values entered into the first two text boxes and display them in the third text box.

41

Chapter 3

Try It Out

Adding Three Labels and Text Boxes, and a Button to a Form

Although this seems like an ambitious task, adding these controls is really simple. Using the form you have been using this whole chapter:

1.Make sure that the Common Controls is the displayed category in the Toolbox by clicking the minus symbols of the other categories of tools, if they are displayed.

2.Place the cursor over the Label control, displayed in the Toolbox, and hold down the left mouse button.

3.Drag and drop the label onto the form by dragging the control from the Toolbox onto the form and releasing the mouse button. You can see this in Figure 3-14, just before the mouse button is released.

Once the mouse is released, the Label control is placed on the form in the location you put it in.

Figure 3-14

42

Quick Start Creating Your First C# Express Windows Project

4.Locate the Text property of the Label control you place on the form.

5.Type Value 1 for the Text property. If you press Enter or move out of the property, the text in the label is updated, as shown in Figure 3-15.

6.Drag and drop a TextBox control using the steps just described for the Label control on the form next to the Label control.

After you release the mouse button, you will then see the TextBox control shown in Figure 3-16.

Figure 3-15

43

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