# Simple Calculator Program in C#

Problem:

Create a gui (graphical user interface) program in c# that will allow the user to enter two numbers and choose what operation to use (addition, subtraction, division or multiplication), the program will then perform the operation selected by the user.

Tutorial Description:

We will create a program that will allow the user what mathematical operator to use. Based on that selection the program will compute and display the result.

Step by step tutorial on creating a simple calculator in c#

1. Kindly open your Microsoft Visual Studio 2010
2. On the File menu, select New Project.
3. Select C# as the programming language and select Windows Form Application and click OK.
4. Let us now design the form. Please add the following controls to our form:
1. 1 group box
3. 2 labels
4. 2 textboxes
5. 1 button
5. The design and look of the form.
6. We will first create a function. Kindly view the code of the form. To do that, right click Form1.cs in the Solution Explorer and select View Code.

The line of codes that you will see (except the // paste the function here ):

```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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// paste the function here
}
}
```
1. Now paste the code below in the place of // paste the function here
```public void checkvalue()
{
if (textBox1.Text == "")
{
textBox1.Focus();
}
else if (textBox2.Text == "")
{
textBox2.Focus();
}
}
```

Code Explanation:

The newly created function will actually validate or check the entry of the user. This will notify the user through a message box that he or she must not leave the field empty.

1. Next, double click the button and paste the code.
```checkvalue();
{
double sum;
sum= int.Parse(textBox1.Text) + int.Parse(textBox2.Text);
MessageBox.Show("sum: " + sum);
}
{
double product;
product = int.Parse(textBox1.Text) * int.Parse(textBox2.Text);
MessageBox.Show("product: " + product);
}
{
double quotient;
quotient = int.Parse(textBox1.Text) / int.Parse(textBox2.Text);
MessageBox.Show("quotient: " + quotient);
}
{
double difference;
difference = int.Parse(textBox1.Text) - int.Parse(textBox2.Text);
MessageBox.Show("difference: " + difference);
}
}
```

Note: you must have place the above code in between the lines of.

```private void button1_Click(object sender, EventArgs e)
{
// paste code here
}
```

Code Explanation:

When the user clicks the button it will first call the function checkvalue(), then it will determine if what is the radio button that have been selected and perform the necessary mathematical operator. The result will be then displayed in a form of message box.

Run the project