This application will consist of a single form that contains a label and a button.. To create a new Windows application, click the New Project button on the Start page, or select File ➣
Trang 1Developing 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
Trang 2Click 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 you can place standard Windows controls, such as labels, text boxes, and buttons You'll
be adding controls to your form shortly
Figure 6.2: A blank form
In the next section, you'll learn about the Toolbox, which you use to add controls to your form
Working with the Toolbox
You add controls to your form by selecting the control from the Toolbox and dragging the control to your form You can also click and drag, or double-click on the control to drop a new one of that type onto your form As you can see in Figure 6.2 shown earlier, the Toolbox is to the left of the blank form
Note If you don't see the Toolbox, you can display it by selecting View ➣ Toolbox, or
by pressing Ctrl+Alt+X on your keyboard
You can see that the available items in the Toolbox are categorized into groups with names such as Data and XML Schema The Toolbox will show only categories that are relevant to the type of application you are developing The following list describes the contents of some of these categories:
• Data The Data category contains classes that allow you to access and store
information from a database The Data category includes the following classes: SqlConnection, SqlCommand, DataSet, and DataView, among others
Trang 3• XML Schema The XML Schema category contains classes that allow you to
access XML data
• Dialog Editor The Dialog Editor category contains controls that you can place on
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
Note If you don't see the Properties window, you can display it by selecting View ➣ Properties Window, or by pressing F4 on your keyboard
You set the property by clicking the area to the right of the property name Go ahead and click to the right of the BackColor property to view some of the colors to which you can set this property
In the next section, you'll learn how to add a label and button control to your form You'll also set a couple of the properties for those controls
Adding a Label and a Button Control
Trang 4To add a label and a button control to your form select the appropriate control from the Toolbox and drag it to your form For example, to add a label to your form, you select the label control from the Toolbox Once you've dragged a label to your form, you can resize
it by using the mouse or setting the Size property in the Properties window You can also click on the label in the Toolbox and drag it on your form
Make your label big enough so that that it stretches across the length of your form Next, add a button control below your label, as shown in Figure 6.3
Figure 6.3: The form with a label and button control
Next, you'll change some of the properties for your label and button You do this using the Properties window Set the Name property of your label to myLabel Set the Name and Text properties for your button to myButton and Press Me!, respectively Also, set the Text property of your form to My Form
Note You use the Name property when referencing a Windows control in C# code.
Next, you'll add a line of code to the myButton_Click() method This method is executed when myButton is clicked in your running form The statement you'll add to
myButton_Click() will set the Text property of myLabel to a string This string will
contain a line from Shakespeare's play, Macbeth To add the code, double-click
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" +
Trang 5"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
The Form1.cs file contains the code for your form This code is often referred to as the
code behind your form because you can think of it as being behind the visual design for
your form You can view the code for your form by selecting View ➣ Code, or by pressing the F7 key on your keyboard Listing 6.1 shows the contents of the Form1.cs file
Trang 6Listing 6.1: Form1.cs
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace MyWindowsApplication
{
/// <summary>
/// Summary description for Form1
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label myLabel;
private System.Windows.Forms.Button myButton;
/// <summary>
/// Required designer variable
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{