Simple Calculator Program in C#


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
    2. 4 radio buttons
    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()
// paste the function here
  1. Now paste the code below in the place of // paste the function here
public void checkvalue()
if (textBox1.Text == "")
MessageBox.Show("Please enter an integer");
else if (textBox2.Text == "")
MessageBox.Show("Please enter an integer");

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.
if (radioButton1.Checked == true)
double sum;
sum= int.Parse(textBox1.Text) + int.Parse(textBox2.Text);
MessageBox.Show("sum: " + sum);
else if (radioButton2.Checked == true)
double product;
product = int.Parse(textBox1.Text) * int.Parse(textBox2.Text);
MessageBox.Show("product: " + product);
else if (radioButton3.Checked == true)
double quotient;
quotient = int.Parse(textBox1.Text) / int.Parse(textBox2.Text);
MessageBox.Show("quotient: " + quotient);
else if (radioButton4.Checked == true)
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




, , , , ,

Post navigation