Tuesday, 7 January 2014

GUI Event Handling in C# with Calculator Concept

GUI Event Handling in C# with Calculator Concept

This is a simple calculator program that was written using Visual Studio.NET and C#.  The calculator is a good program to learn how to use Button and Textbox events. Event handling in Windows Forms (.NET frame work that supports GUI application) employ the .NET event handling model described earlier.  The application has one class, Form1, derived from System.Windows.Forms.Form class. Class Form1 is derived from Form class. for the GUI controls (Textbox, Button, etc.) are not available on my From means cannot use drag and drop  concept with Toolbox from IDE which name is From1  However, you still need to define the class for  each  Button and Textbox …………………………….

The first parameter (sender) in the above declaration specifies the object that fired the event. The second parameter (e) of the above declaration holds data that can be used in the event handler. 

public void add(object sender ,EventArgs e)
{

}

Code for From1.cs  ……………………….

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Calculator
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

TextBox tno = new TextBox();
Button b1 = new Button();
Button b2 = new Button();
Button b3 = new Button();
Button b4 = new Button();
Button b5 = new Button();
Button b6 = new Button();
Button b7 = new Button();
Button b8 = new Button();
Button b9 = new Button();
Button b0 = new Button();
Button bplus = new Button();
Button bsub = new Button();
Button bmul = new Button();
Button bdiv = new Button();
Button bequal = new Button();
Button bdot = new Button();

double temp;
double temp2;
double temp3;
char opr;
private void Form1_Load(object sender, EventArgs e)
{
InitializeComponentwithFrom();

b1.Click += Mybutton;
b2.Click += Mybutton;
b3.Click += Mybutton;
b4.Click += Mybutton;
b5.Click += Mybutton;
b6.Click += Mybutton;
b7.Click += Mybutton;
b8.Click += Mybutton;
b9.Click += Mybutton;
b0.Click += Mybutton;
bdot.Click +=MyDot;
bplus.Click += add;
bsub.Click += sub;
bdiv.Click +=div;
bmul.Click += mul;
bequal.Click += result;
}
// create method  for  Button TextBox...................
private void InitializeComponentwithFrom()
{
this.Controls.Add(tno);
tno.Left = 20;
tno.Width = 200;
tno.Top = 10;
tno.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
this.Controls.Add(b1);
b1.Left = 20;
b1.Top = 50;
b1.Text = "1";
b1.Size = new System.Drawing.Size(35, 30);
this.Controls.Add(b2);
b2.Left = 70;
b2.Top = 50;
b2.Text = "2";
b2.Size = new System.Drawing.Size(35, 30);
this.Controls.Add(b3);
b3.Left = 120;
b3.Top = 50;
b3.Text = "3";
b3.Size = new System.Drawing.Size(35, 30);
this.Controls.Add(b4);
b4.Left = 170;
b4.Top = 50;
b4.Text = "4";
b4.Size = new System.Drawing.Size(35, 30);
this.Controls.Add(b5);
b5.Left = 20;
b5.Top = 100;
b5.Text = "5";
b5.Size = new System.Drawing.Size(35, 30);
this.Controls.Add(b6);
b6.Left = 70;
b6.Top = 100;
b6.Text = "6";
b6.Size = new System.Drawing.Size(35, 30);
this.Controls.Add(b7);
b7.Left = 120;
b7.Top = 100;
b7.Text = "7";
b7.Size = new System.Drawing.Size(35, 30);
this.Controls.Add(b8);
b8.Left = 170;
b8.Top = 100;
b8.Text = "8";
b8.Size = new System.Drawing.Size(35, 30);
this.Controls.Add(b9);
b9.Left = 20;
b9.Top = 150;
b9.Text = "9";
b9.Size = new System.Drawing.Size(35, 30);
this.Controls.Add(b0);
b0.Left = 70;
b0.Top = 150;
b0.Text = "0";
b0.Size = new System.Drawing.Size(35, 30);
this.Controls.Add(bplus);
bplus.Left = 120;
bplus.Top = 150;
bplus.Text = "+";
bplus.Size = new System.Drawing.Size(35, 30);
this.Controls.Add(bsub);
bsub.Left = 170;
bsub.Top = 150;
bsub.Text = "-";
bsub.Size = new System.Drawing.Size(35, 30);
this.Controls.Add(bmul);
bmul.Left = 20;
bmul.Top = 200;
bmul.Text = "*";
bmul.Size = new System.Drawing.Size(35, 30);
this.Controls.Add(bdiv);
bdiv.Left = 70;
bdiv.Top = 200;
bdiv.Text = "-";
bdiv.Size = new System.Drawing.Size(35, 30);
this.Controls.Add(bdot);
bdot.Left = 120;
bdot.Top = 200;
bdot.Text = ".";
bdot.Size = new System.Drawing.Size(35, 30);
this.Controls.Add(bequal);
bequal.Left = 170;
bequal.Top = 200;
bequal.Text = "=";
bequal.Size = new System.Drawing.Size(35, 30);
}



bool dot = true;
//code  for dot opertor
public void MyDot(object obj, EventArgs ed)
{
if (dot == true)
{
Button btn = (Button)obj;
tno.Text += btn.Text;
dot = false;
}
}
//code for how to show text of each button 1,2,3..........
public void Mybutton(object o1, EventArgs e1)
{
Button btn = (Button)o1;
tno.Text += btn.Text;
}
//code  for + opertor
public void add(object o1,EventArgs e1)
{
dot = true;
temp = Convert.ToDouble(tno.Text);
tno.Text = "";
opr='+';
}
//code  for - opertor
public void sub(object o1, EventArgs e1)
{
dot = true;
temp3 = Convert.ToDouble(tno.Text);
tno.Text = "";
opr='-';
}
//code  for * opertor
public void mul(object o1, EventArgs e1)
{
dot = true;
temp3 = Convert.ToDouble(tno.Text);
tno.Text = "";
opr = '*';
}
//code  for / opertor
public void div(object o1, EventArgs e1)
{
dot = true;
temp3 = Convert.ToDouble(tno.Text);
tno.Text = "";
opr = '/';
}
//code  for equal Button
public void result(object o1, EventArgs e1)
{
switch (opr)
{
case '+':
{

temp2 = Convert.ToDouble(tno.Text);
double res = temp + temp2;
tno.Text = res.ToString();

break;
}
case '-':
{
temp3 = Convert.ToDouble(tno.Text);
double res1 = temp - temp3;
tno.Text = res1.ToString();
dot = true;
break;
}
case '*':
{

temp2 = Convert.ToDouble(tno.Text);
double res = temp * temp2;
tno.Text = res.ToString();

break;
}
case '/':
{
temp3 = Convert.ToDouble(tno.Text);
double res1 = temp / temp3;
tno.Text = res1.ToString();
dot = true;
break;
}
}
temp = Convert.ToDouble(tno.Text);
}
}
}



0 comments:

Post a Comment