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.

click to expand
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 you can place standard Windows controls, such as labels, text boxes, and buttons. You'll be adding controls to your form shortly.

click to expand
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.

  • 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

To 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.

click to expand
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" +   "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.

click to expand
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.

Listing 6.1: Form1.cs

start example
 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()     {       //       // Required for Windows Form Designer support       //       InitializeComponent();       //       // TODO: Add any constructor code after InitializeComponent call       //     }     /// <summary>     /// Clean up any resources being used.     /// </summary>     protected override void Dispose(bool disposing)     {       if(disposing)       {         if (components != null)         {           components.Dispose();         }       }       base.Dispose(disposing);     }     #region Windows Form Designer generated code     /// <summary>     /// Required method for Designer support - do not modify     /// the contents of this method with the code editor.     /// </summary>     private void InitializeComponent()     {       this.myLabel = new System.Windows.Forms.Label();       this.myButton = new System.Windows.Forms.Button();       this.SuspendLayout();       //       // myLabel       //       this.myLabel.Location = new System.Drawing.Point(8, 8);       this.myLabel.Name = "myLabel";       this.myLabel.Size = new System.Drawing.Size(288, 184);       this.myLabel.TabIndex = 0;       this.myLabel.Text = "label1";       //       // myButton       //       this.myButton.Location = new System.Drawing.Point(120, 200);       this.myButton.Name = "myButton";       this.myButton.Size = new System.Drawing.Size(72, 24);       this.myButton.TabIndex = 1;       this.myButton.Text = "Press Me!";       this.myButton.Click += new System.EventHandler(this.myButton_Click);       //       // Form1       //       this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);       this.ClientSize = new System.Drawing.Size(304, 237);       this.Controls.AddRange(new System.Windows.Forms.Control[] {         this.myButton,         this.myLabel});       this.Name = "Form1";       this.Text = "My Form";       this.ResumeLayout(false);     }     #endregion     /// <summary>     /// The main entry point for the application.     /// </summary>     [STAThread]     static void Main()     {       Application.Run(new Form1());     }     private void myButton_Click(object sender, System.EventArgs e)     {       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?";     }   } } 
end example

As you can see, the Form1 class is derived from the System.Windows.Forms.Form class. The Form class represents a Windows form.

Note 

The System.Windows.Forms namespace contains the various classes for creating Windows applications. Most of the classes in this namespace are derived from the System.Windows.Forms.Control class; this class provides the basic functionality for the controls you can place on a form.

The Form1 class declares two private objects named myLabel and myButton, which are the label and button controls you added to your form earlier. Because the myLabel and myButton objects are private, this means that they are accessible only in the Form1 class.

Access modifiers enable you to specify the degree to which a class member is available outside the class. You can also use an access modifier to specify the degree to which the class itself is available.

Table 6.1 shows the access modifiers in decreasing order of availability: public is the most accessible, and private the least.

Table 6.1: ACCESS MODIFIERS

ACCESS MODIFIER

ACCESSIBILITY

public

Member accessible without restriction.

protected internal

Member accessible only within the class, a derived class, or class in the same program (or assembly).

internal

Member accessible only within the class or class in the same program (or assembly).

protected

Member accessible only within the class or derived classes.

private

Member accessible only within the class. This is the default.

The Form1 class constructor calls the InitializeComponent() method. This method adds myLabel and myButton to the form and sets the properties for those objects. These properties include the Location (the position in the form), Name, Size, TabIndex (the order in which the control is accessed using the Tab key), and Text. For example, the following code sets the properties of myLabel:

 this.myLabel.Location = new System.Drawing.Point(8, 8); this.myLabel.Name = "myLabel"; this.myLabel.Size = new System.Drawing.Size(288, 184); this.myLabel.TabIndex = 0; this.myLabel.Text = "label1"; 

You'll notice that the InitializeComponent() method is within #region and #endregion preprocessor directives. These directives enclose an area of code that may be hidden in VS .NET's code editor, leaving only the text that immediately follows #region visible. Figure 6.5 shows how the hidden code appears in VS .NET.

click to expand
Figure 6.5: Hiding code in VS .NET using the #region directive

To view hidden code, all you have to do is to click the plus icon to the left of the code. Figure 6.6 shows the code within the #region and #endregion directives.

click to expand
Figure 6.6: Viewing hidden code in VS .NET

The Main() method runs the form by calling the Application.Run() method. The Application class is static and provides a number of methods you can use in your Windows programs. Because this class is static, you don't create an instance of this class, and its members are always available within your form. When the Run() method is called, your form waits for events from the mouse and keyboard. One example of an event is the clicking of the button in your form.

The myButton_Click() method is the method you modified earlier that sets the Text property of myLabel to a string containing the quote from Macbeth. When myButton is clicked, the myButton_Click() method is called and the text in myLabel is changed; you saw this when you ran your form earlier.

In the next section, you'll learn about the VS .NET Solution Explorer.

Working with the Solution Explorer

You can use the VS .NET Solution Explorer to view the items in your project, such as the namespace for your project. Of course, a project may contain more than one namespace. To view the Solution Explorer, you select View Solution Explorer.

Tip 

You can also view the Solution Explorer by pressing Ctrl+Alt+L on your keyboard.

You can use Solution Explorer to view the following items in a project's namespace:

  • References References include other namespaces and classes to which your form's code refers. You can employ the using statement to reference other namespaces and classes.

  • Icon File An icon file has the extension .ico. You use an icon file to set the image displayed in Windows Explorer for your application.

  • Assembly File An assembly file contains the metadata for your application's assembly. An assembly is collection of code for your application.

  • Code Files A code file is a program source file, such as the code for a form. You saw an example of this in the earlier "Examining the Code behind the Form" section.

Figure 6.7 shows the Solution Explorer for this example.

click to expand
Figure 6.7: The Solution Explorer

As you can see from Figure 6.7, you can expand or collapse the items shown in the Solution Explorer by clicking the plus or minus icon, respectively. You can also display the properties for an item in Solution Explorer: When you have the Properties window displayed, selecting an item in Solution Explorer will also display the properties for that item. For example, in Figure 6.7, the properties for the MyWindowsApplication project are displayed; you can see that the project file is MyWindowsApplication.csproj.

In the next section, you'll learn about the VS .NET Class View.

Working with the Class View

You use the VS .NET Class View to examine the classes, methods, and objects in your project. To see the Class View, you select View Class View.

Tip 

You can also see the Class View by pressing Ctrl+Shift+C on your keyboard.

Figure 6.8 shows the Class View for the example.

click to expand
Figure 6.8: The Class View

As you can see from Figure 6.8, you can view the classes, methods, and objects for the example. You can also view the properties for a selected item in the Properties window. For example, Figure 6.8 also shows the properties for the Form1 class.

Next, you'll be introduced to the other types of Windows controls.




Mastering C# Database Programming
Mastering the SAP Business Information Warehouse: Leveraging the Business Intelligence Capabilities of SAP NetWeaver
ISBN: 0764596373
EAN: 2147483647
Year: 2003
Pages: 181

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net