FREE BOOK

Writing Windows C# Programs

Posted by Addison Wesley Free Book | C# Language July 21, 2009
The C# language has its roots in C++, Visual Basic, and Java. Both C# and VB.Net use the same libraries and compile to the same underlying code. Both are managed languages with garbage collection of unused variable space, and both can be used interchangeably. Both also use classes with method names that are very similar to those in Java, so if you are familiar with Java, you will have no trouble with C#.

Building a C# Application
 
Let's start by creating a simple console application-that is, one without any windows that just runs from the command line. Start the Visual Studio.NET program, and select File | New Project. From the selection box, choose C# Console application, as shown in Figure 3-1.
 
 
 
 Figure 3-1
The New Project selection window: selecting a console application.
 
 This will bring up a module with Main already filled in. You can type in the rest of the code as follows.
 
 Console.WriteLine("Hello C# World");
 
 You can compile this and run it by pressing F5.
 
 When you compile and run the program by pressing F5, a DOS window will appear and print out the message "Hello C# World" and then exit.
 
 The Simplest Window Program in C#
 
C# makes it very easy to create Windows GUI programs. In fact, you can create most of it using the Windows Designer. To do this, start Visual Studio.NET, select File | New Project, and select C# Windows Application. The default name (and filename) is WindowsApplication1, but you can change this before you close the New dialog box. This brings up a single form project, initially called Form1.cs. You can then use the Toolbox to insert controls, just as you can in Visual Basic.
 
The Windows Designer for a simple form with one text field and one button is shown in Figure 3-2.
 
 
 
 Figure 3-2 The Windows Designer in Visual Studio.NET
 
 You can draw the controls on the form by selecting the TextBox from the Toolbox, dragging it onto the form, and then doing the same with the button. Then, to create program code, we need only double-click on the controls. In this simple form, we want to click on the "Hello" button, which copies the text from the text field to the textbox we called txHi and clears the text field. So in the designer, we double-click on that button, and this code is automatically generated.
 
 
      private void btHello_Click(object sender, EventArgs e)
         {
             txHi.Text = "Hello there";
 
        }
 
Note that the Click routine passes in a sender object and an event object that you can query for further information. Under the covers, it also connects the event to this method. The running program is shown in Figure 3-3.
 
 
 
 Figure 3-3 The SimpleHello form after clicking the Say Hello button
 
While we only had to write one line of code inside the previous subroutine, it isinstructive to see how different the rest of the code is for this program.We first see that several libraries of classes are imported so the program can use them.
 
 
using System;
 
using System.Drawing;
 
using System.Collections;
 
using System.ComponentModel;
 
using System.Windows.Forms;
 
using System.Data;
 
 Most significant is the Windows.Forms library, which is common to all the .NET languages.
 
The code the designer generates for the controls is illuminating-and it isright out there in the open for you to change if you want. Essentially, each controlis declared as a variable and added to a container. Here are the control declarations.
 
 Note the event handler added to the btHello.Click event.
 
 
  private System.Windows.Forms.TextBox txHi;
 
  private System.Windows.Forms.Button btHello;
 
 
      private void InitializeComponent()
         {
             this.btHello = new System.Windows.Forms.Button();
             this.txHi = new System.Windows.Forms.TextBox();
             this.SuspendLayout();
             //
 
            // btHello
 
            //
 
            this.btHello.Location =
             new System.Drawing.Point(80, 112);
             this.btHello.Name = "btHello";
             this.btHello.Size = new System.Drawing.Size(64, 24);
             this.btHello.TabIndex = 1;
             this.btHello.Text = "Hello";
             this.btHello.Click +=
             new EventHandler(this.btHello_Click);
             //
 
            // txHi
 
            //
 
            this.txHi.Location =
             new System.Drawing.Point(64, 48);
             this.txHi.Name = "txHi";
             this.txHi.Size = new System.Drawing.Size(104, 20);
             this.txHi.TabIndex = 0;
             this.txHi.Text = "";
             //
 
            // Form1
 
            //
 
            this.AutoScaleBaseSize =
             new System.Drawing.Size(5, 13);
             this.ClientSize = new System.Drawing.Size(240, 213);
             this.Controls.AddRange(
             new System.Windows.Forms.Control[] {
 
this.btHello,
 
this.txHi});
             this.Name = "Form1";
             this.Text = "Hello window";
             this.ResumeLayout(false);
 
        }
 
 If you change this code manually instead of using the property page, the window designer may not work anymore. We'll look more at the power of this system after we discuss objects and classes in the next chapter.

Total Pages : 6 12345

comments