|
||||||
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 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# ApplicationThe IDE will create a simple blank form for the programmer, and this simplest of C# applications will consist of:
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# FormWhen it comes to adding components to a form the programmer must:
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:
And so from this humble start the C++ or Java programmer can quickly move on to produce much more complicated C# applications. SummaryThe new C# programmer needs two things:
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.
|
||||||
|
|
||||||
|
|
||||||