Basic Calculator in c#
Today we are going to create a small basic calculator in C#. This calculator will perform operations like Addition,Subtraction,Multiplication and Division. This is basically a Windows Application. I choose to use windows Application over the Console Application for this program because it is a User Interface and also looks good and interesting to use. Below is the C# Window Application for calculator program.
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 Windows_Form_Activities
{
public partial class Form1 : Form
{
int result;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if ((string.IsNullOrWhiteSpace(textBox1.Text)) || (string.IsNullOrWhiteSpace(textBox2.Text)))
{
MessageBox.Show("Please enter value first", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
else
{
result = Convert.ToInt32(textBox1.Text) + Convert.ToInt32(textBox2.Text);
MessageBox.Show("Addition is " + result);
}
}
private void sub_Click(object sender, EventArgs e)
{
if ((string.IsNullOrWhiteSpace(textBox1.Text)) || (string.IsNullOrWhiteSpace(textBox2.Text)))
{
MessageBox.Show("Please enter value first","Error",MessageBoxButtons.OK,MessageBoxIcon.Warning);
return;
}
else
{
result = Convert.ToInt32(textBox1.Text) - Convert.ToInt32(textBox2.Text);
MessageBox.Show("Subtraction is " + result);
}
}
private void mul_Click(object sender, EventArgs e)
{
if ((string.IsNullOrWhiteSpace(textBox1.Text)) || (string.IsNullOrWhiteSpace(textBox2.Text)))
{
MessageBox.Show("Please enter value first", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
else
{
result = Convert.ToInt32(textBox1.Text) * Convert.ToInt32(textBox2.Text);
MessageBox.Show("Multiplication is " + result);
}
}
private void div_Click(object sender, EventArgs e)
{
if ((string.IsNullOrWhiteSpace(textBox1.Text)) || (string.IsNullOrWhiteSpace(textBox2.Text)))
{
MessageBox.Show("Please enter value first", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
else
{
result = Convert.ToInt32(textBox1.Text) / Convert.ToInt32(textBox2.Text);
MessageBox.Show("Division is " + result);
}
}
}
}
Explaination
Basic Calculator in c#
Reviewed by LanguageExpert
on
March 17, 2018
Rating:
No comments