This article is about a programming language.C♯ is intended to be a simple, modern, general-purpose, object-oriented programming language.
This is a simplest calculator in using c sharp language and window form that allow user to perform the six mathematical operations.the step as follows:
1-we create a window form and name is simple calculator.
2-put 17 buttons and 1 textfield.
3-change the name of the buttons.
Labels Names
0 zerobutton
1 onebutton
2 twobutton
3 threebutton
4 fourbutton
5 fivebutton
6 sixbutton
7 sevenbutton
8 eightbutton
9 ninebutton
+ plusbutton
- minusbutton
/ dividebutton
* multiplybutton
% modbutton
+/- plusminusbutton
= equalbutton
C removebutton
4- For the textbox it "output textbox"
5-When you done in your design then we see code of calculator in c#.
6- Source code of simple calculator
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
{
public partial class CalcForm1 : Form
{
private decimal firstnumberdecimal = 0.0m;
private decimal secondnumberdecimal = 0.0m;
private string operatorstring = "+";
private decimal resultdecimal = 0.0m;
public CalcForm1()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void CalcForm1_Load(object sender, EventArgs e)
{
}
private void zerobutton_Click(object sender, EventArgs e)
{
textBox1.Text += "0";
}
private void onebutton_Click(object sender, EventArgs e)
{
RemoveZero(1);
}
private void RemoveZero(int number)
{
if (textBox1.Text == "0")
textBox1.Text = number.ToString();
else
textBox1.Text += number.ToString();
}
private void twobutton_Click(object sender, EventArgs e)
{
RemoveZero(2);
}
private void threebutton_Click(object sender, EventArgs e)
{
RemoveZero(3);
}
private void fourbutton_Click(object sender, EventArgs e)
{
RemoveZero(4);
}
private void fivebutton_Click(object sender, EventArgs e)
{
RemoveZero(5);
}
private void sixbutton_Click(object sender, EventArgs e)
{
RemoveZero(6);
}
private void sevenbutton_Click(object sender, EventArgs e)
{
RemoveZero(7);
}
private void eightbutton_Click(object sender, EventArgs e)
{
RemoveZero(8);
}
private void ninebutton_Click(object sender, EventArgs e)
{
RemoveZero(9);
}
private void clearbutton_Click(object sender, EventArgs e)
{
textBox1.Clear();
// textBox1.Text = "";
textBox1.Text = "0";
firstnumberdecimal = 0.0m;
secondnumberdecimal = 0.0m;
resultdecimal = 0.0m;
}
private void button7_Click(object sender, EventArgs e)
{
if (!textBox1.Text.Contains("."))
textBox1.Text += ".";
}
private void plusbutton_Click(object sender, EventArgs e)
{
supplyoperator("+");
}
private void supplyoperator(string
_operatorstring)
{
operatorstring = _operatorstring;
firstnumberdecimal = decimal.Parse(textBox1.Text);
textBox1.Text = "0";
}
private void subtractbutton_Click(object sender, EventArgs e)
{
supplyoperator("-");
}
private void multyplybutton_Click(object sender, EventArgs e)
{
supplyoperator("*");
}
private void dividebutton_Click(object sender, EventArgs e)
{
supplyoperator("/");
}
private void button8_Click(object sender, EventArgs e)
{
secondnumberdecimal = decimal.Parse(textBox1.Text);
switch (operatorstring)
{
case "+":
resultdecimal = firstnumberdecimal + secondnumberdecimal;
break;
case "-":
resultdecimal = firstnumberdecimal - secondnumberdecimal;
break;
case "*":
resultdecimal = firstnumberdecimal * secondnumberdecimal;
break;
case "/":
resultdecimal = firstnumberdecimal / secondnumberdecimal;
break;
case "%":
resultdecimal = firstnumberdecimal % secondnumberdecimal;
break;
}
textBox1.Text = resultdecimal.ToString();
}
private void plussubtractbutton_Click(object sender, EventArgs e)
{
if (!textBox1.Text.Contains("-"))
textBox1.Text = "-" + textBox1.Text;
else
textBox1.Text = textBox1.Text.Trim('-');
}
private void modbutton_Click(object sender, EventArgs e)
{
supplyoperator("%");
}
}
}
This is a simplest calculator in using c sharp language and window form that allow user to perform the six mathematical operations.the step as follows:
1-we create a window form and name is simple calculator.
2-put 17 buttons and 1 textfield.
3-change the name of the buttons.
Labels Names
0 zerobutton
1 onebutton
2 twobutton
3 threebutton
4 fourbutton
5 fivebutton
6 sixbutton
7 sevenbutton
8 eightbutton
9 ninebutton
+ plusbutton
- minusbutton
/ dividebutton
* multiplybutton
% modbutton
+/- plusminusbutton
= equalbutton
C removebutton
4- For the textbox it "output textbox"
5-When you done in your design then we see code of calculator in c#.
6- Source code of simple calculator
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
{
public partial class CalcForm1 : Form
{
private decimal firstnumberdecimal = 0.0m;
private decimal secondnumberdecimal = 0.0m;
private string operatorstring = "+";
private decimal resultdecimal = 0.0m;
public CalcForm1()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void CalcForm1_Load(object sender, EventArgs e)
{
}
private void zerobutton_Click(object sender, EventArgs e)
{
textBox1.Text += "0";
}
private void onebutton_Click(object sender, EventArgs e)
{
RemoveZero(1);
}
private void RemoveZero(int number)
{
if (textBox1.Text == "0")
textBox1.Text = number.ToString();
else
textBox1.Text += number.ToString();
}
private void twobutton_Click(object sender, EventArgs e)
{
RemoveZero(2);
}
private void threebutton_Click(object sender, EventArgs e)
{
RemoveZero(3);
}
private void fourbutton_Click(object sender, EventArgs e)
{
RemoveZero(4);
}
private void fivebutton_Click(object sender, EventArgs e)
{
RemoveZero(5);
}
private void sixbutton_Click(object sender, EventArgs e)
{
RemoveZero(6);
}
private void sevenbutton_Click(object sender, EventArgs e)
{
RemoveZero(7);
}
private void eightbutton_Click(object sender, EventArgs e)
{
RemoveZero(8);
}
private void ninebutton_Click(object sender, EventArgs e)
{
RemoveZero(9);
}
private void clearbutton_Click(object sender, EventArgs e)
{
textBox1.Clear();
// textBox1.Text = "";
textBox1.Text = "0";
firstnumberdecimal = 0.0m;
secondnumberdecimal = 0.0m;
resultdecimal = 0.0m;
}
private void button7_Click(object sender, EventArgs e)
{
if (!textBox1.Text.Contains("."))
textBox1.Text += ".";
}
private void plusbutton_Click(object sender, EventArgs e)
{
supplyoperator("+");
}
private void supplyoperator(string
_operatorstring)
{
operatorstring = _operatorstring;
firstnumberdecimal = decimal.Parse(textBox1.Text);
textBox1.Text = "0";
}
private void subtractbutton_Click(object sender, EventArgs e)
{
supplyoperator("-");
}
private void multyplybutton_Click(object sender, EventArgs e)
{
supplyoperator("*");
}
private void dividebutton_Click(object sender, EventArgs e)
{
supplyoperator("/");
}
private void button8_Click(object sender, EventArgs e)
{
secondnumberdecimal = decimal.Parse(textBox1.Text);
switch (operatorstring)
{
case "+":
resultdecimal = firstnumberdecimal + secondnumberdecimal;
break;
case "-":
resultdecimal = firstnumberdecimal - secondnumberdecimal;
break;
case "*":
resultdecimal = firstnumberdecimal * secondnumberdecimal;
break;
case "/":
resultdecimal = firstnumberdecimal / secondnumberdecimal;
break;
case "%":
resultdecimal = firstnumberdecimal % secondnumberdecimal;
break;
}
textBox1.Text = resultdecimal.ToString();
}
private void plussubtractbutton_Click(object sender, EventArgs e)
{
if (!textBox1.Text.Contains("-"))
textBox1.Text = "-" + textBox1.Text;
else
textBox1.Text = textBox1.Text.Trim('-');
}
private void modbutton_Click(object sender, EventArgs e)
{
supplyoperator("%");
}
}
}
7- Finally run your program and test your calculator program.
0 comments:
Post a Comment