How to create a basic calculator with c#
//SOURCE CODE for Form1.cs
using System;
using System.Windows.Forms;
namespace WindowsFormsTutorial
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void buttonCalculate_Click(object sender, EventArgs e)
{
decimal value1=0;
decimal value2=0;
decimal result = 0;
textBox3.Text = "";
if (decimal.TryParse(textBox1.Text, out value1) == false)
{
MessageBox.Show("Error! Failed to parse! Please do not insert stupid crap.","ERROR");
}
else if (decimal.TryParse(textBox2.Text, out value2) == false)
{
MessageBox.Show("Error! Failed to parse! Please do not insert stupid crap.", "ERROR");
}
else if(radioAdd.Checked==true)
{
result = value1 + value2;
}
else if (radioDivide.Checked == true)
{
if (value2 != 0)
{
result = value1 / value2;
}
else { textBox3.Text = "Undefined - divide by "; }
}
else if (radioMultiply.Checked == true)
{
result = value1 * value2;
}
else if (radioSubtract.Checked == true)
{
result = value1 - value2;
}
else if (radioMod.Checked == true)
{
result = value1 % value2;
}
textBox3.Text += result;
}
}
}