CodeProject>hi... this is my first attempt at building an application in microsoft visual studio 2010 working on the .net framework 4 and C#.
here is a utility calculator implementing basic calculator functions.
step 1: create a new project and select windows forms application.
step 2: right click the form name icon in the solution explorer window(located on right top corner by default... or add the window from the view) and add a new form. name it calci (a calci.cs form will open)
step 3:drag and drop the text box and the buttons from the toolbox to the form and click on them to execute the button functions.
step 4: in the calci.cs form add the following default namespace directives
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;
step 5: start by writing the basic button definitions in the calci.cs form page.
the code :
// initialize the form attributes by the following constructor.
public partial class calci : Form
{
int lenex1=0,j = 0,m=0,o=0,p=0,q=0;
string op1;
decimal exans, ex1 = 0;
char ope='\0';
public calci()
{
InitializeComponent();
}
//double click the buttons on the design view screen and add the following code to the button functions.
private void btn1_Click(object sender, EventArgs e)
{
txtboxinput.Text+= btn1.Text;
}
private void btn2_Click(object sender, EventArgs e)
{
txtboxinput.Text+= btn2.Text;
}
private void btn3_Click(object sender, EventArgs e)
{
txtboxinput.Text+= btn3.Text ;
}
private void btn4_Click(object sender, EventArgs e)
{
txtboxinput.Text+= btn4.Text;
}
private void btn5_Click(object sender, EventArgs e)
{
txtboxinput.Text+= btn5.Text;
}
private void btn6_Click(object sender, EventArgs e)
{
txtboxinput.Text+= btn6.Text;
}
private void btn7_Click(object sender, EventArgs e)
{
txtboxinput.Text+= btn7.Text;
}
private void btn8_Click(object sender, EventArgs e)
{
txtboxinput.Text+= btn8.Text;
}
private void btn9_Click(object sender, EventArgs e)
{
txtboxinput.Text+= btn9.Text;
}
private void btn0_Click(object sender, EventArgs e)
{
txtboxinput.Text+= btn0.Text;
}
the calci.cs[design] page appears as follows:
step 6: now add more buttons to the form to execute the various functions of a claculator.
i have used the basic functions like
addition , subtraction , multiplication , division .
step 7: writing the code for the calculator functions as well as the clear button , ON button and decimal button :
private void btnpt_Click(object sender, EventArgs e) //decimal button def.
{
if (j == 0)
{
txtboxinput.Text += btnpt.Text;
j = 1;
}
}
private void btnclear_Click(object sender, EventArgs e) //clear button def.
{
txtboxinput.Text = " "; //restores all buttons back to their initial state
lenex1 = 0;
j = 0;
m = 0;
o = 0;
p = 0;
q = 0;
ex1 = 0;
ope = '\0';
}
private void btnplus_Click(object sender, EventArgs e) //plus button def.
{
if (m == 0)
{
int i = 0, len = txtboxinput.Text.Length;
j = 0;
ope = '+';
op1 = txtboxinput.Text.Substring(i);
ex1 = Convert.ToDecimal(op1);
lenex1 = txtboxinput.Text.Length;
txtboxinput.Text += btnplus.Text;
//Pass(lenex1);
m =o=p=q= 1;
}
}
private void btnminus_Click(object sender, EventArgs e) //minus button def.
{
if (o == 0)
{
int i = 0, len = txtboxinput.Text.Length;
j = 0;
ope = '-';
op1 = txtboxinput.Text.Substring(i);
ex1 = Convert.ToDecimal(op1);
lenex1 = txtboxinput.Text.Length;
txtboxinput.Text += btnminus.Text;
//Pass(lenex1);
m = o = p = q = 1;
}
}
private void btnmultiply_Click(object sender, EventArgs e) //multiply button def.
{
if (p == 0)
{
int i = 0, len = txtboxinput.Text.Length;
j = 0;
ope = '*';
op1 = txtboxinput.Text.Substring(i);
ex1 = Convert.ToDecimal(op1);
lenex1 = txtboxinput.Text.Length;
txtboxinput.Text += btnmultiply.Text;
//Pass(lenex1);
m = o = p = q = 1;
}
}
private void btndivide_Click(object sender, EventArgs e) // divide button def.
{
if (q == 0)
{
int i = 0, len = txtboxinput.Text.Length;
j = 0;
ope = '/';
op1 = txtboxinput.Text.Substring(i);
ex1 = Convert.ToDecimal(op1);
lenex1 = txtboxinput.Text.Length;
txtboxinput.Text += btndivide.Text;
//Pass(lenex1);
m = o = p = q = 1;
}
}
private void btnequal_Click(object sender, EventArgs e) //equal button def.
{
int ans = 0;
decimal final;
int l = txtboxinput.Text.Length;
string dec;
// txtboxinput.Text = Convert.ToString( ope);
//txtboxinput.Text = "hell";
try
{
ans = lenex1 + 1;
dec = txtboxinput.Text.Substring(ans);
final = val2(Convert.ToDecimal(dec));
txtboxinput.Text = Convert.ToString(final);
j = m = o = p = q = 0;
}
catch (Exception ex)
{
txtboxinput.Text = "0";
throw ex;
}
}
decimal val2(decimal ex2) //calculation function
{
//txtboxinput.Text = "switch";
switch (ope) //switch case executing the functions using operands ex1 and ex2
{
case '+':exans = ex1 + ex2;
break;
case '-':exans = ex1-ex2;
break;
case '*': exans = ex1 * ex2;
break;
case '/' : if (ex2 != 0)
{
exans = ex1 / ex2;
}
else
{
exans = 0;
}
break;
}
return exans;
}
private void btnON_Click(object sender, EventArgs e) // on button def.
{
txtboxinput.Enabled = true; //all the controls have been disabled and
//are enabled at the click of ON button
btn1.Enabled = true;
btn2.Enabled = true;
btn3.Enabled = true;
btn4.Enabled = true;
btn5.Enabled = true;
btn6.Enabled = true;
btn7.Enabled = true;
btn8.Enabled = true;
btn9.Enabled = true;
btn0.Enabled = true;
btnplus.Enabled = true;
btnminus.Enabled = true;
btnmultiply.Enabled = true;
btndivide.Enabled = true;
btnpt.Enabled = true;
btnequal.Enabled = true;
btnclear.Enabled = true;
lenex1 = 0;
j = 0;
m = 0;
o = 0;
p = 0;
q = 0;
ex1 = 0;
ope = '\0';
}
now the calci.cs[design] looks like :
tip : to add the background image open the calci.cs[design] and add the properties window by selecting from "VIEW". in the properties window locate the backgroundimage property . browse and add the desired image. It appears as follows:
thanks for reading...
any doubts or suggestions are welcome.
for feedback you can comment on this blog
mail me at: akrita.ag@gmail.com