[Home]  [Edit this page]  [Recent Changes]  [Special Pages]  [Help
CSharpandWindowsForms

A Basic Introduction To Windows Forms using C#

By Alexander Schunk

----------------------------------------------------------------------

This is a short introduction into Windows Forms application programming using C#. Note: if you want to reproduce this application by your own, you need at least Visual C# Studio 2003 Standard.

Windows Forms Basics

Before you start writing Windows Forms apps, I shall give a basic overview of the Namespaces and Classes, that are commonly used for writing Windows Forms applications.

The .NET framework contains hundreds of classes, yet all frameworks have an origin so does .NET. The mother of all .NET Namespaces and classes is the object class. All other important Namespaces or classes are derived from it, either direct or indirect. The object class implements all core methods and properties you need to write C# applications.

However, you will seldom use methods or properties of this class directly. Instead, you will use derived classes like System.Console; to write console applications or System.Windows.Forms to write Windows Forms applications.

Namespaces versus Classes

In the lines above, I have used the terms namespaces and classes almost as a synonym which, of course, not correct. A Namespace can contain classes but a class cannot contain a Namespace. So, within the .NET framework hierarchie, a Namespace has a higher position than a class. In fact, a class is at the bottom and a Namespace at the top of this hierarchie, if we agree that the whole .NET framework is one big namespace, splitted into several smaller namespaces.

To get an idea of how this works, consider the following:

 //Somewhere in the .NET framework hierarchie
Namespace System
{
  //class definitions
  Namespace System.Windows
   {
     //class definitions
     Namespace System.Drawing
      {
        //class definitions
      }
   }
}


 //Somewhere in your code
 //telling the compiler that we use the Namespace System.Windows.Forms
using System.Windows.Forms;
class HelloWindows
{
  
  public HelloWindows
   {
     InitializeComponent();
    }
  public void Main(string args[])
   {
   }
}


As you see above, you need to tell the compiler what class of the .NET framework you are going to use. However, in praxis, this line will be added automatically by Visual Studio if you switch from Design view to Code view.

Your first Windows Forms program using Visual Studio

Now we are going to write our first Windows Forms application. It will not do much, yet it helps getting a basic idea of the inner live of a Windows Forms application. If you want to create this app on you own, you need to start Visual Studio, then create a New Windows Forms applications and switch to Design view. Here, open the Toolbox and drag and drop 2 labels and a button on the form. You are free to give the controlls whatever name you which, but they should reflect their task. If you have dragged the button on the form, double click on it. Visual Studio responds by generating a new method called ExitButton_Click(), if you have named the button Exitbutton.

When finished with designing you might save this program as HelloWindows and then switch to the code view and press F5 to compile and run your application.

If you have finished designing your form, you should have the following lines of code:

using System;
using System.Drawing;
using System.Collection;
using System.ComponentModell;
using System.Windows.Forms;
using System.Data;
namespace HelloWindows
{
   //Visual Studio generated code 
  public class Form1 : System.Windows.Forms.Form1
  {
    //Instanciating the controls dragged on the form
    private System.Windows.Forms.Label lblUser;
    private System.Windows.Forms.label lblDate;
    private System.Windows.Forms.Button Exitbutton;
 
    private System.ComponentModell.Container components = null;
     //The main constructor of the Form 
    public Form1()
     {
       InitializeComponent();
     }
    //Dispose the window and release all ressources 
   protected override void Dispose(booll disposing)
    {
     if(disposing)
       if(components != null)
         components.dispose();
     base.Dispose(disposing);
    }
  //Code generated by Form Designer 
  [STAThread]
   static void Main()
    {
     Application.Run(new Form1());
    }
  
   private void ExitButton_Click(object sender, System.EventArgs e)
    {
     Close();
    }
   private void Form1_Load(object sender, System.EventArgs e)
   {
     lblUser.Text = "Hallo" + Environment.Username;
     lblDate.Text = "Today's Date is" + DateTime.Now.ToShortString(); 
   }
   
  }
}


Explanation of the Code

This is, of course, not all the code generated by Visual Studio, however this helps to seize our Windows Forms application. You may note that I have added some more comments in the code. The first thing Visual Studio does is including the necessary namespaces it needs to create the form. I will not discuss all of them in detail, because only 2 of them are really releveant to us.

After including the namespaces, Visual Studio creates a new object of the controlls you have dragged on the Form. Remember, you dragged 2 labels on it. I have named them lblUser and lblDate to reflect what they stand for.

private System.Windows.Forms.Label lblUser;
private System.Windows.Forms.label lblDate;
private System.Windows.Forms.Button Exitbutton;


Having constructed the controlls, Visual Studio adds the main constructor of the Form which has the same name as our application. This concept is similar to the one of C++ or Java.

public Form1()
     {
       InitializeComponent();
     }


The constructor calls a method named InitializeComponent(). In this method, all initializations take place, like creating form layout, controls layout etc. You can see this code if you click on the plus sign at the left side in the code view of Visual Studio.

When the constructor has performed his task, Visual Studio calls the Forms Main function. Within the Main() function, Visual Studio creates an Application object which calls its Run() function to execute the application.

static void Main()
    {
     Application.Run(new Form1());
    }


Then we leave the Visual Studio generated code and come to our own created functions. So far, we have created only 1 function, which is the ExitButton_Click() function. This function was created when you double clicked on the Button in the Designer.

private void ExitButton_Click(object sender, System.EventArgs e)
    {
     Close();
    }


This function closes the application. To do this, I have added the Close() method. However, the Close() method does not close the application, instead, the applications close procedure takes place in the Dispose() function. You can proove this by creating another form without a special exit button and click on exit in the systems menu. The application will close as well.

protected override void Dispose(bool disposing)
    {
     if(disposing)
       if(components != null)
         components.dispose();
     base.Dispose(disposing);
    }




last edited (April 11, 2006) by Paranoicus, Number of views: 23210, Current Rev: 37 (Diff)

[Edit this page]  [Page history]  [What links here]  [Discuss this topic]  [Printer Friendly]  

Members

Username:

Password:


Register
Forgot Password?




Programmers Heaven - for .NET, Java, C/C++ and WEB Developers!
© 1996-2008 Community Networks Ltd. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Tore Nestenius at .NET Consultant - Synchron Data.