Windows Form Program of Simple Calculator
Windows Form Program of Simple Calculator
using System;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Simple_Calculator
{
public partial class Form1 : Form
{
float num1, num2;
char sp_txt = ' ';
public Form1()
{
InitializeComponent();
label2.ForeColor = Color.Black;
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text += "7";
}
private void button2_Click(object sender, EventArgs e)
{
textBox1.Text += "8";
}
private void button3_Click(object sender, EventArgs e)
{
textBox1.Text += "9";
}
private void button10_Click(object sender, EventArgs e)
{
num1 = float.Parse(textBox1.Text);
textBox1.Text += "+";
sp_txt = '+';
}
private void button16_Click(object sender, EventArgs e)
{
try
{
String[] s1 = textBox1.Text.Split(sp_txt);
label2.ForeColor = Color.Black;
num2 = float.Parse(s1[1]);
float c = 0;
switch (sp_txt)
{
case '+':
c = num1 + num2;
break;
case '-':
c = num1 - num2;
break;
case '*':
c = num1 * num2;
break;
case '/':
if (num2 == 0)
{
label2.ForeColor = Color.Red;
label2.Text = "The denominator cannot be zero !";
MessageBox.Show("The denominator cannot be zero !", "Division by zero");
}
else
{
c = num1 / num2;
}
break;
}
textBox1.Text = "" + c;
if (textBox1.Text.Contains('E') && textBox1.Text.Contains('+'))
{
String[] samp = textBox1.Text.Split('E');
//Console.WriteLine("" + samp[0] + " " + samp[1]);
samp[0] = samp[0].Remove(1, 1);
samp[1] = samp[1].Remove(0, 1);
//Console.WriteLine("" + samp[0] + " " + samp[1]);
String converted_text = "" + samp[0];
String zeros = "";
for (inti = 0; i<int.Parse(samp[1]); i++)
{
zeros += "0";
}
converted_text += zeros;
textBox1.Text = "" + converted_text;
}
}
sp_txt = '-';
}
private void button12_Click(object sender, EventArgs e)
{
num1 = float.Parse(textBox1.Text);
textBox1.Text += "*";
sp_txt = '*';
}
private void button15_Click(object sender, EventArgs e)
{
num1 = float.Parse(textBox1.Text);
textBox1.Text += "/";
sp_txt = '/';
}
private void button17_Click(object sender, EventArgs e)
{
label2.ForeColor = Color.Black;
label2.Text = "OK";
textBox1.Text = "";
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
Output: