In this blog we will learn that how to make a basic calculator check the source code:
Basically in normal calculators u can use only buttons to execute the calculator in .net application but here is a liitle change in the scenario i added a hotkeys in this calculator (Like '+' to add ,'/' to divide) after press the numlock on button on calculator u can use numpad keys to execute this calculator so use it and enjoy the difference
source code:
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 WindowsFormsApplication33
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string fun, no;
private void button2_Click(object sender, EventArgs e)
{
textBox1.Text += button2.Text;
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text += button1.Text;
}
private void button3_Click(object sender, EventArgs e)
{
if (textBox1.Text == string.Empty)
{
errorProvider1.SetError(textBox1, "Must enter the value to remove");
}
else
{
errorProvider1.Clear();
no = textBox1.Text;
int no1 = no.Length;
textBox1.Text = (no.Substring(0, no1 - 1));
}
}
private void button4_Click(object sender, EventArgs e)
{
textBox1.Text += button4.Text;
}
private void button5_Click(object sender, EventArgs e)
{
textBox1.Text += button5.Text;
}
private void button6_Click(object sender, EventArgs e)
{
textBox1.Text += button6.Text;
}
private void button7_Click(object sender, EventArgs e)
{
textBox1.Text += button7.Text;
}
private void button8_Click(object sender, EventArgs e)
{
textBox1.Text += button8.Text;
}
private void button9_Click(object sender, EventArgs e)
{
textBox1.Text += button9.Text;
}
private void button10_Click(object sender, EventArgs e)
{
textBox1.Text += button10.Text;
}
private void button11_Click(object sender, EventArgs e)
{
textBox1.Text += button11.Text;
}
private void button12_Click(object sender, EventArgs e)
{
textBox1.Text += button12.Text;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
errorProvider1.Clear();
if (textBox1.Text == "+" || textBox1.Text == "-" || textBox1.Text == "*" ||
textBox1.Text == "/")
{
textBox1.Text = "";
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
public void funcal()
{
if (textBox1.Text == "")
{
errorProvider1.SetError(textBox1, "Must enter the value to remove");
}
else
{
if (fun == "+")
{
textBox1.Text = Convert.ToString(Convert.ToDecimal(no) +
Convert.ToDecimal(textBox1.Text));
}
else if (fun == "-")
{
textBox1.Text = Convert.ToString(Convert.ToDecimal(no) -
Convert.ToDecimal(textBox1.Text));
}
else if (fun == "*")
{
textBox1.Text = Convert.ToString(Convert.ToDecimal(no) *
Convert.ToDecimal(textBox1.Text));
}
else if (fun == "/")
{
textBox1.Text = Convert.ToString(Convert.ToDecimal(no) /
Convert.ToDecimal(textBox1.Text));
}
}
}
private void button16_Click(object sender, EventArgs e)
{
no = textBox1.Text;
textBox1.Text = "";
fun = "+";
}
private void button15_Click(object sender, EventArgs e)
{
no = textBox1.Text;
textBox1.Text = "";
fun = "-";
}
private void button14_Click(object sender, EventArgs e)
{
no = textBox1.Text;
textBox1.Text = "";
fun = "*";
}
private void button13_Click(object sender, EventArgs e)
{
no = textBox1.Text;
textBox1.Text = "";
fun = "/";
}
private void button17_Click(object sender, EventArgs e)
{
funcal();
}
private void button18_Click(object sender, EventArgs e)
{
textBox1.Text = "";
no = "";
fun = "";
}
private void button16_KeyDown(object sender, KeyEventArgs e)
{
}
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
}
private void button16_KeyDown_1(object sender, KeyEventArgs e)
{
}
private void checkBox1_CheckedChanged_1(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
textBox1.ReadOnly = false;
checkBox1.Text = "Num Lock Off";
textBox1.Select();
}
else
{
textBox1.ReadOnly = true;
checkBox1.Text = "Num Lock On";
button17.Select();
}
}
private void textBox1_KeyDown_1(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Add)
{
no = textBox1.Text;
textBox1.Text = "";
fun = "+";
}
else if (e.KeyCode == Keys.Subtract)
{
no = textBox1.Text;
textBox1.Text = "";
fun = "-";
}
else if (e.KeyCode == Keys.Multiply)
{
no = textBox1.Text;
textBox1.Text = "";
fun = "*";
}
else if (e.KeyCode == Keys.Divide)
{
no = textBox1.Text;
textBox1.Text = "";
fun = "/";
}
else if (e.KeyCode == Keys.Enter)
{
funcal();
}
else if (e.KeyCode == Keys.Escape)
{
textBox1.Text = "";
no = "";
fun = "";
}
}
private void button20_Click(object sender, EventArgs e)
{
textBox1.Text = Convert.ToString(System.Math.Sqrt(Convert.ToDoubl
(textBox1.Text)));
}
private void button19_Click(object sender, EventArgs e)
{
textBox1.Text = Convert.ToString(Convert.ToDecimal(textBox1.Text) *
Convert.ToDecimal(textBox1.Text));
}
private void button21_Click(object sender, EventArgs e)
{
textBox1.Text = Convert.ToString(1 / Convert.ToDecimal(textBox1.Text));
}
private void button22_Click(object sender, EventArgs e)
{
}
}
}