Calculator in C#.net (with if-else statement)
In this tutorial we will be creating a simple c#.net calculator with the options to add, Multiply, Divide, Subtract. I am using visual studio 2012, but you can use c# text editor that you like.
-> To start a new project goto Microsoft Visual Studio.
-> New -> Project -> Windows Form Application name Calculator in csharp (or whatever you want).
-> Design the form as follows.
Now add the following four instance Boolean variables and 2 instance double variable.
Now Double click the btnadd and write the following code:
Double click the btndivide and write the following code:
Double click the btnmultiply and write the following code:
Double click the btnequal and write the following code:
Double click the btnclear and write the following code:
Your coding window will then look like this:
In this tutorial we will be creating a simple c#.net calculator with the options to add, Multiply, Divide, Subtract. I am using visual studio 2012, but you can use c# text editor that you like.
-> To start a new project goto Microsoft Visual Studio.
-> New -> Project -> Windows Form Application name Calculator in csharp (or whatever you want).
-> Design the form as follows.
Set the following properties for it in the Properties Window:
Control
|
Name
|
Text
|
Other Property
|
button1
|
btn1
|
1
|
Size = 54, 35
|
button2
|
btn2
|
2
|
Size = 54, 35
|
button3
|
btn3
|
3
|
Size = 54, 35
|
button4
|
btn4
|
4
|
Size = 54, 35
|
button5
|
btn5
|
5
|
Size = 54, 35
|
button6
|
btn6
|
6
|
Size = 54, 35
|
button7
|
btn7
|
7
|
Size = 54, 35
|
button8
|
btn8
|
8
|
Size = 54, 35
|
button9
|
btn9
|
9
|
Size = 54, 35
|
button10
|
btn0
|
0
|
Size = 54, 35
|
button11
|
btndot
|
.
|
Size = 54, 35
|
button12
|
btnequal
|
=
|
Size = 54, 35
|
button13
|
btnadd
|
+
|
Size = 114, 35
|
button14
|
btnminus
|
-
|
Size = 114, 35
|
button15
|
btndivide
|
/
|
Size = 114, 35
|
button16
|
btnmultiply
|
*
|
Size = 114, 35
|
Button17
|
btnclear
|
Clear
|
Size = 294, 35
|
Textbox1
|
txtresult
|
Size = 294, 35
|
|
In this the form have a text box and 17 buttons for different uses.
After completing the design we can start the coding .
Double click the button and write the following code.
Code
for btn1
|
private void btn1_Click(object sender, EventArgs e)
{
txtresult.Text += btn1.Text;
}
|
Code
for btn2
|
private void
btn2_Click(object sender, EventArgs e)
{
txtresult.Text += btn2.Text;
}
|
Code
for btn3
|
private void btn3_Click(object
sender, EventArgs e)
{
txtresult.Text += btn3.Text;
}
|
Code
for btn4
|
private void btn4_Click(object
sender, EventArgs e)
{
txtresult.Text += btn4.Text;
}
|
Code
for btn5
|
private void btn5_Click(object
sender, EventArgs e)
{
txtresult.Text += btn5.Text;
}
|
Code
for btn6
|
private void btn6_Click(object
sender, EventArgs e)
{
txtresult.Text += btn6.Text;
}
|
Code
for btn7
|
private void btn7_Click(object
sender, EventArgs e)
{
txtresult.Text += btn7.Text;
}
|
Code
for btn8
|
private void btn8_Click(object
sender, EventArgs e)
{
txtresult.Text += btn8.Text;
}
|
Code
for btn9
|
private void btn9_Click(object
sender, EventArgs e)
{
txtresult.Text += btn9.Text;
}
|
Code
for btn0
|
private void btn0_Click(object
sender, EventArgs e)
{
txtresult.Text += btn0.Text;
}
|
Now add the following four instance Boolean variables and 2 instance double variable.
double t1;
double t2;
bool badd = false;
bool bminus = false;
bool bdivide = false;
bool bmultiply = false;
Code
for btnadd
|
private void
btnadd_Click(object sender, EventArgs e)
{
t1 += double.Parse(txtresult.Text);
txtresult.Clear();
badd =
true;
bminus
= false;
bdivide = false;
bmultiply = false;
}
|
Now Double click the btnminus and write the following code:
Code
for btnminus
|
private void
btnminus_Click(object sender, EventArgs e)
{
t1 += double.Parse(txtresult.Text);
txtresult.Clear();
badd =
false;
bminus
= true;
bdivide = false;
bmultiply = false;
}
|
Double click the btndivide and write the following code:
Code
for btndivide
|
private void
btndivide_Click(object sender, EventArgs e)
{
t1 += double.Parse(txtresult.Text);
txtresult.Clear();
badd =
false;
bminus
= false;
bdivide = true;
bmultiply = false;
}
|
Double click the btnmultiply and write the following code:
Code
for btnmultiply
|
private void
btnmultiply_Click(object sender, EventArgs e)
{
t1 += double.Parse(txtresult.Text);
txtresult.Clear();
badd =
false;
bminus
= false;
bdivide = false;
bmultiply = true;
}
|
Double click the btnequal and write the following code:
Code
for btnequal
|
private void
btnequal_Click(object sender, EventArgs e)
{
if (badd == true)
{
t2
= t1 + double.Parse(txtresult.Text);
}
else if (bminus == true)
{
t2
= t1 - double.Parse(txtresult.Text);
}
else if (bdivide == true)
{
t2 = t1 / double.Parse(txtresult.Text);
}
else if (bmultiply == true)
{
t2
= t1 * double.Parse(txtresult.Text);
}
else
{
MessageBox.Show("Syntex Error");
}
txtresult.Text = t2.ToString();
t1 =
0;
}
|
Double click the btnclear and write the following code:
Code
for btnclear
|
private void
btnclear_Click(object sender, EventArgs e)
{
txtresult.Clear();
}
|
Your coding window will then look like this:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Calculator_in_csharp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
double t1;
double t2;
bool badd = false;
bool bminus = false;
bool bdivide = false;
bool bmultiply = false;
private void btn9_Click(object sender, EventArgs e)
{
txtresult.Text += btn9.Text;
}
private void btn5_Click(object sender, EventArgs e)
{
txtresult.Text += btn5.Text;
}
private void btn1_Click(object sender, EventArgs e)
{
txtresult.Text += btn1.Text;
}
private void btn2_Click(object sender, EventArgs e)
{
txtresult.Text += btn2.Text;
}
private void btn3_Click(object sender, EventArgs e)
{
txtresult.Text += btn3.Text;
}
private void btn4_Click(object sender, EventArgs e)
{
txtresult.Text += btn4.Text;
}
private void btn6_Click(object sender, EventArgs e)
{
txtresult.Text += btn6.Text;
}
private void btn7_Click(object sender, EventArgs e)
{
txtresult.Text += btn7.Text;
}
private void btn8_Click(object sender, EventArgs e)
{
txtresult.Text += btn8.Text;
}
private void btn0_Click(object sender, EventArgs e)
{
txtresult.Text += btn0.Text;
}
private void btnclear_Click(object sender, EventArgs e)
{
txtresult.Clear();
}
private void btnadd_Click(object sender, EventArgs e)
{
t1 += double.Parse(txtresult.Text);
txtresult.Clear();
badd = true;
bminus = false;
bdivide = false;
bmultiply = false;
}
private void btnminus_Click(object sender, EventArgs e)
{
t1 += double.Parse(txtresult.Text);
txtresult.Clear();
badd = false;
bminus = true;
bdivide = false;
bmultiply = false;
}
private void btndivide_Click(object sender, EventArgs e)
{
t1 += double.Parse(txtresult.Text);
txtresult.Clear();
badd = false;
bminus = false;
bdivide = true;
bmultiply = false;
}
private void btnmultiply_Click(object sender, EventArgs e)
{
t1 += double.Parse(txtresult.Text);
txtresult.Clear();
badd = false;
bminus = false;
bdivide = false;
bmultiply = true;
}
private void btnequal_Click(object sender, EventArgs e)
{
if (badd == true)
{
t2 = t1 + double.Parse(txtresult.Text);
}
else if (bminus == true)
{
t2 = t1 - double.Parse(txtresult.Text);
}
else if (bdivide == true)
{
t2 = t1 / double.Parse(txtresult.Text);
}
else if (bmultiply == true)
{
t2 = t1 * double.Parse(txtresult.Text);
}
else
{
MessageBox.Show("Syntex
Error");
}
txtresult.Text = t2.ToString();
t1 = 0;
}
}
0 comments:
Post a Comment