Developing a Simple Windows Application phần 1 - Pdf 66

Developing a Simple Windows Application
In this section, you'll see how to create a simple Windows application using VS .NET.
This application will consist of a single form that contains a label and a button. When you
click the button, the text for the label will change to a quote from Shakespeare's play,
Macbeth. You'll also see how to compile and run the example application.
Creating the Windows Application
Start VS .NET by selecting Start ➣ Programs ➣ Microsoft Visual Studio .NET ➣
Microsoft Visual Studio .NET. To create a new Windows application, click the New
Project button on the Start page, or select File ➣ New ➣ Project.
Tip You can also create a new project by pressing Ctrl+Shift+N on your keyboard.
You'll see the New Project dialog box, which you use to select the type of project you
want to create. Because you're going to create a C# Windows application, select the
Visual C# Projects folder from the Project Types list, and select Windows Application
from the Templates area of the New Project dialog box. VS .NET will assign a default
name to your project; this default name will be WindowsApplication1, or something
similar. You can specify your own name for your project by changing the text in the
Name field; go ahead and enter MyWindowsApplication in the Name field, as shown in
Figure 6.1
.

Figure 6.1: Creating a C# Windows application in Visual Studio .NET

Note The Location field specifies the directory where the files for your new project are
stored. VS .NET will set a default directory, but you can change this by entering
your own directory. This default directory is the Documents and Settings directory
on your hard drive.
Click the OK button to continue. VS .NET will create a new subdirectory named
MyWindowsApplication in the directory specified in the Location field. Once VS .NET
creates the directory, along with some initial files for your project, VS .NET will display
a blank form, as shown in Figure 6.2
. You can think of the form as the canvas on which

Windows dialog boxes.

Web Forms The Web Forms category contains controls that are for web forms.
You can design web forms using VS .NET and deploy them to Microsoft's Internet
Information Server (IIS). These web forms may then be run over the Internet.

Components The Components category contains classes such as
FileSystemWatcher, which allows you to monitor changes in a computer's file
system. Other classes include EventLog, DirectoryEntry, DirectorySearcher,
MessageQueue, PerformanceCounter, Process, ServiceController, and Timer.
These allow you to perform various system operations.

Windows Forms The Windows Forms category contains controls that you can
add to a Windows form. These include labels, buttons, and text boxes, among
others. You'll use some of these controls in this chapter.

HTML The HTML category contains controls that you can add to a web form.
These include labels, buttons, tables, and images, among others.
In the next section
, you'll learn about the Properties window.
Working with the Properties Window
The Properties window contains aspects of a control that you can set. For example, you
can set the background color of your form using the BackColor property. Some other
properties of the form control include ForeColor (the foreground color) and
BackgroundImage (an image displayed in the background). Different types of controls
have different types of properties.
As you can see from Figure 6.2
shown earlier, the Properties window is to the right of the
blank form.


myButton and enter the following code in the myButton_Click() method:
myLabel.Text =
"Is this a dagger which I see before me,\n" +
"The handle toward my hand? Come, let me clutch thee.\n" +
"I have thee not, and yet I see thee still.\n" +
"Art thou not, fatal vision, sensible\n" +
"To feeling as to sight? or art thou but\n" +
"A dagger of the mind, a false creation,\n" +
"Proceeding from the heat-oppressed brain?"; Note If you're a Shakespeare fan, you'll recognize this line from the scene before
Macbeth kills King Duncan.
You've now finished your form. Build your project by selecting Build ➣ Build Solution,
or by pressing Ctrl+Shift+B on your keyboard.
To run your form, select Debug ➣ Start without Debugging, or press Ctrl+F5 on your
keyboard.
Tip You can take a shortcut when building and running your form: If you simply start
your form without first building it, VS .NET will check to see if you made any
changes to your form since you last ran it. If you did make a change, VS .NET will
first rebuild your project and then run it.
Figure 6.4
shows the running form after the Press Me! button is clicked.

Figure 6.4: The running form
Now that you've created and run the form, let's take a look at the code generated by VS
.NET for it. The C# code for your form is contained in the file Form1.cs file. You'll
examine this code in the next section
.
Examining the Code behind the Form


Nhờ tải bản gốc

Tài liệu, ebook tham khảo khác

Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status