Getting Started with C# Application Development

How to Create a C# Form for Windows

© Mark Alexander Bain

Apr 1, 2009
Getting Started with C#, Mark Alexander Bain
In just a few minutes a new programmer can easily create an new application in C# by using the Microsoft .NET Framework and a suitable IDE.

C# (or C Sharp) is one of Microsoft’s .NET family and is becoming increasingly popular amongst programmers (according to the the TIOBE Programming Community Index). Its programming style is largely based on C++ but with a fair amount of Java thrown in for good measure. This means that anyone with a background in either language will not find it too difficult to migrate to C# and, in fact, anyone completely new to programming will not find the learning curve to be too steep.

However, the potential C# programmer will need to carry out a couple of tasks before they can actually start programming.

Getting Ready for C#

Before creating a new C# application the programmer requires two things:

  • The Microsoft .NET Framework
  • A suitable programming IDE (Integrated Design Environment)

The Microsoft .NET Framework is the architecture within which Windows applications can operate. It is pre-installed in some versions of Windows but the most up to date version is freely available from Microsoft ASP.NET Essential Downloads.

Once the Microsoft .NET Framework is installed then the programmer can install an IDE with which they can develop their applications. Again these are freely available on the Internet, for example:

Once the programmer has installed the IDE of their choice then they are ready to create their first C# application – in this case a simple Windows form.

A Very Simple C# Application

The IDE will create a simple blank form for the programmer, and this simplest of C# applications will consist of:

  • Details of any classes to be included
  • A new class extending the Windows form class

Therefore the code to display an empty form will be something like:

using System;
using System.Windows.Forms;
public class MainForm : Form {
public static void Main() {
Application.Run(new MainForm());
}
public MainForm() {
}
}

It’s worth noting at this point that C# allows partial classes. These enable the programmer to use multiple files for the class definition. However, whichever method is uses, if the C# is built at this point then it will display a simple, blank form. The Next stage is to add components to the form.

Adding Components to a C# Form

When it comes to adding components to a form the programmer must:

  • Define the components to be added (such as labels, text boxes and buttons)
  • Define the location of each component on the form
  • Add any text for the buttons and labels
  • Add any functionality for components (such as buttons)
  • Make the components visible

The resulting code is slightly more complex (but still very simple):

using System;
using System.Windows.Forms;
using System.Drawing;
public class MainForm : Form {
//Define variables for the components
private Label lblText;
private TextBox txtIP1;
private TextBox txtIP2;
private Button cmdDoCalc;
public static void Main() {
Application.Run(new MainForm());
}
public MainForm() {
//Set the title of the form
this.Text = "The Simplest C# Application";
//Create the components
this.lblText = new Label();
this.txtIP1 = new TextBox();
this.txtIP2 = new TextBox();
this.cmdDoCalc = new Button();
//Set the position of the component
txtIP1.Location = new Point(0,0);
txtIP2.Location = new Point(0,25);
cmdDoCalc.Location = new Point(0,50);
lblText.Location = new Point(0,75);
//Set the text and the action of the button
cmdDoCalc.Text = "+";
cmdDoCalc.Click += new EventHandler(this.DoCalc);
//Add some text to the label
lblText.Text = "Hello World!";
//Show the components
this.Controls.Add(txtIP1);
this.Controls.Add(txtIP2);
this.Controls.Add(cmdDoCalc);
this.Controls.Add(lblText);
}
//The action for the button
public void DoCalc(object Sender, EventArgs e) {
lblText.Text = Convert.ToString (
Convert.ToDouble(txtIP1.Text) +
Convert.ToDouble(txtIP2.Text)
);
}
}

If the form is built now then it will show:

  • Two text input boxes
  • A button which, when clicked, will add the contents of the boxes and display the result

And so from this humble start the C++ or Java programmer can quickly move on to produce much more complicated C# applications.

Summary

The new C# programmer needs two things:

  • The Microsoft .NET Framework
  • An IDE (Integrated Design Environment)

Both of which are freely available from a number of web sites. The IDE will create a blank form for the programmer who then extends the basic form class, adding any required components and actions. In just a few minutes they will have a simple, but effective, form that can be further extended quickly and easily by the programmer.


The copyright of the article Getting Started with C# Application Development in C Programming is owned by Mark Alexander Bain. Permission to republish Getting Started with C# Application Development in print or online must be granted by the author in writing.


Getting Started with C#, Mark Alexander Bain
Download the Microsoft .NET Framework, Mark Alexander Bain
Creating a New C# Windows Form, Mark Alexander Bain
Building a C# Windows Form, Mark Alexander Bain
A C# Windows Application, Mark Alexander Bain


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo