0% found this document useful (0 votes)
19 views4 pages

Windows Form Program of Simple Calculator

This document contains the code for a simple calculator Windows form program written in C#. It includes code for number and operator buttons that populate a text box, code to parse the text box for calculations on button click, and code to handle errors like division by zero. The program takes two numbers and an operator as input, performs the calculation, and displays the output.

Uploaded by

Narendra Kamath
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views4 pages

Windows Form Program of Simple Calculator

This document contains the code for a simple calculator Windows form program written in C#. It includes code for number and operator buttons that populate a text box, code to parse the text box for calculations on button click, and code to handle errors like division by zero. The program takes two numbers and an operator as input, performs the calculation, and displays the output.

Uploaded by

Narendra Kamath
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

1. 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;
}
}

catch (Exception ae)


{
//
}
}
private void button6_Click(object sender, EventArgs e)
{
textBox1.Text += "4";
}
private void button5_Click(object sender, EventArgs e)
{
textBox1.Text += "5";
}
private void button4_Click(object sender, EventArgs e)
{
textBox1.Text += "6";
}
private void button9_Click(object sender, EventArgs e)
{
textBox1.Text += "1";
}
private void button8_Click(object sender, EventArgs e)
{
textBox1.Text += "2";
}
private void button7_Click(object sender, EventArgs e)
{
textBox1.Text += "3";
}
private void button13_Click(object sender, EventArgs e)
{
textBox1.Text += "0";
}
private void button14_Click(object sender, EventArgs e)
{
textBox1.Text += ".";
}
private void button11_Click(object sender, EventArgs e)
{
num1 = float.Parse(textBox1.Text);
textBox1.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:

You might also like