Thursday, February 9, 2012

How to create a simple WinForms Project?

Step 1: Start up Visual Studio.
Step 2: Create a new Windows Forms project called HelloWorld.
Step 3: Drag a Label control onto the Form and set it's Text property to "What's Your Name:'.
Step 4: Drag a TextBox control onto the Form and name it txtName.
Step 5: Drag a Label control onto the form, clear it's Text property and name lblGreeting.
Step 6: Drag a Button control onto the form and name it btnExit.
Step 7: Drag a Button control onto the form and name it btnGreeting.
Step 8: Right click away from the Form and select View Code from the pop-up menu.

When you first created the Windows Forms project, Visual Studio started you off with a blank form called Form1 and created this program.cs file:

using System;
using System.Windows.Forms;
namespace HelloWorld
  static class Program
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    static void Main()
      Application.SetCompatibleTextRenderingDefault( false );
      Application.Run( new Form1() );
When you selected View Code from the pop-up menu you got something like this:

using System;
using System.Text;
using System.Windows.Forms;
namespace HelloWorld
  public partial class Form1 : Form
    public Form1()
Notice the using System.Text; line at the top of the form1.cs file, that is the namespace that includes the StringBuilder class that we are using.

With just that much code, if you run the form, it will resize, minimize, and close with the close button, but the btnExit and btnGreeting buttons will accomplish nothing yet.

To make the btnExit accomplish what it should be doing, add this to your Form1 class

    private void btnExit_Click( object sender, EventArgs e )

To make the btnGreeting accomplish what it should be doing, add this to your Form1 class:

    private void btnGreeting_Click( object sender, EventArgs e )
      StringBuilder sb = new StringBuilder();
      sb.Append( "Hello " );
      if ( txtName.Text.Length != 0 )
        sb.Append( txtName.Text );
        sb.Append( "World" );
      lblGreeting.Text = sb.ToString();

Now you can run the form again.

Try hitting the Get Greeting button without entering anything in the Text Box.

You should see that the lblGreeting was set to "Hello World".

Try putting your name into the Text Box and then hit the Get Greeting button again.

This time you should see "Hello <your name>"

Now, let's talk about what the btnGreeting_Click method was actually doing:

Since we are building a two part string, we used a StringBuilder class and named it sb. If we had left it at that we would have had a null object since all classes are objects. So that's why sb = new StringBuilder();

The next thing we do is sb.Append( "Hello " ); This appends the word "Hello" followed by a space to our empty sb.

What comes next is a test on the length of the string entered into the Text Box.

if( txtName.Text.Lenght != 0 )

If this condition is true, we append the txtName.Text onto our sb, with
sb.Append( txtName.Text);

If this condition is false, we append "World" onto our sb, with sb.Append( "World" );

Finally, we set the lblGreeting's Text property to the contents of our sb, with
lblGreeting.Text = sb.ToString();

Now we'll talk a little about the btnExit_Click method:

This event handler only has one thing to do, cause the application to exit. To accomplish this task, we use the Application class that is part of the Form class that our Form1 class inhereted from. Application.Exit();

There is only one more addition to make to make our form's title bar say "Hello World" instead of "Form1":

In the Form1 constructor, public Form1() after the call to InitializeComponent() add this line:
Text = "Hello World";

Congradulations, you've now created your first Windows Forms project!

But what was that program.cs file doing? In every Windows Forms project there has to be a starting place, and that starting place is a static method called Main in a class called Program. Now the class called Program is enclosed within a namespace called  HelloWorld.

Up above the namespace and class we have two necessary using statements;
using System;
using System.Windows.Forms;

These pull in the namespaces needed by our HelloWorld project.

The next two lines just set up some default actions on the Appliction class. It's the third line that very important for our purposes:
Application.Run( new Form1() );

When we say new Form1() we are getting a new instance of the class Form1. So this third line is telling the Appliction to Run a new Form1(); Which calls the constructor Form1() which calls InitializeCompentent(); which puts all of our controls on the form.

And now you understand how a Windows Forms project gets started.

No comments:

Post a Comment