Simple Windows Application In C#

In .NET framework, we can develop and run a window application two ways.
(1) We can develop the application in text pad and run it from the command prompt.
(2) We can use VS.NET IDE to develop window application.

Download myForm.cs

The second way ? "Using VS.NET to develop windows application" is the most effective and efficient way. However, to understand how exactly, VS.NET works, it is essential for novice developers to first, develop and run simple application using a command prompt as explained in this article.

This article describes the development of a simple window application for adding two integers. This is a very simple application describing the core functionality without extensive formatting features. It is developed in Beta 2.0. The application developed is – "myForm.cs" to get "myForm.exe" after compilation. To run the Window application, type "myForm" on the command part

Before discussing the code, let's see how the result will look like.

The Result:

The Code:

Let's first add all the classes we will require to develop this application. Most of the classed required to develop a windows application are available in the Systems.Windows.Forms namespace. Systems.Drawing namespace helps us to manipulate the size of the form controls.using System;using System.Drawing;using System.Windows.Forms;We have extended our FORM from the System's Form class. We have defined two TextBoxes to receive inputs from the user and two buttons to ADD and to EXIT the form. Sum of the 2 integers accepted from the user is displayed at the center of the form using a label ? "lblResult". lblTitle displays the title of the Form.

Windows Forms framework employs delegates for handling events. Event hander must have the same parameters as the EventHandler delegate declaration. Typically, each event produces an event handler with a different event-object type for the second parameter.

public class frmFirstForm:Form
private Label lblResult;
private Label lblTitle;
private Button btnAdd;
private Button btnExit;
private TextBox txt1;
private TextBox txt2;
private EventHandler handler;

The objects are initialized. ClientSize defines the size of the form and is extended from the Form class. All the labels and textboxes initialized as shown below.

public frmFirstForm()
ClientSize = new System.Drawing.Size(400,300);
StartPosition = FormStartPosition.CenterScreen;
lblResult = new Label();
lblResult.Size = new Size(50,30);
lblResult.Location = new Point(175,110);
lblResult.Text=" ";
lblTitle = new Label();
lblTitle.Size = new Size(100,100);
lblTitle.Location = new Point(100,10);
lblTitle.Text=" Add two numbers ";
txt1 = new TextBox();
txt1.Size = new Size(80,30);
txt1.Location = new Point(50,40);
txt1.Text=" ";
txt2 = new TextBox();
txt2.Size = new Size(50,30);
txt2.Location = new Point(210,40);
txt2.Text=" ";

The buttons are initialized as shown below. The "click" event of the buttons attached to a new even handler.

handler = new EventHandler(oneFunction);
btnAdd = new Button();
btnAdd.Size = new Size(80,30);
btnAdd.Location = new Point(10,210);
btnAdd.Text=" Add ";
btnAdd.Click += handler;
btnExit = new Button();
btnExit.Size = new Size(80,30);
btnExit.Location = new Point(210,210);
btnExit.Text=" Exit ";
btnExit.Click += handler;

Only one function is used to perform both the operations ? (1) to add and display and (2) to exit the form.

private void oneFunction(Object source, EventArgs e)
lblResult.Text=" " +(Int32.Parse(txt1.Text)+Int32.Parse(txt2.Text));}

The Application.Run method makes the form shows and runs the form.

public static void Main()
Application.Run(new frmFirstForm());

The execution:

A screen shot of the execution is give below.


1) Professional c# by Simon Robinson et. al.

Twitter Digg Delicious Stumbleupon Technorati Facebook Email

No comments yet... Be the first to leave a reply!