Create A Calculator Using C# Application

Introduction

In this blog, I am going to show you a simple Calculator using C# Application. Here, you are able to Add, Subtract, Divide, and Multiply the numbers using C# Windows Application Code.

This is not a perfect calculator, but it is able to perform most of the operations like Addition, Subtraction, multiplication, and Division of the numbers.

Please follow the steps given below to create a calculator using C# Application.

Step 1. Open your Visual Studio, create a New Project, and name it Calculator.

NewProject

Step 2. Click the OK button, which redirects you to the Next Page, where you are able to create a page.

CreatePage

Step 3. Click Form, go to the Properties, and change the Text properties name to Calculator.

Properties

Step 4. From the ToolBox, drag and drop a Textbox. Go to Properties of TextBox.

TExtbox

Step 5. In the image given below, we are able to change the Properties of TextBox. If you want to resize your Textbox, then change the Multiline Properties to True.

If you want to change the color of the Textbox, then go to the Background color properties and change it. If you want to change the Alignment, go to the TextAlign properties and change it to Center.

Modifiers

Step 6. Below the Textbox, you can drag and drop multiple buttons and resize it. Once you resize all the buttons, then you can change the text of the button.

Example. 1,2 3,4,5,6,7,8

Text Property

Step 7. Finally, the page looks as shown below.

Final Page

Step 8. You can add the code inside the button.

private void button8_Click(object sender, EventArgs e)  
{  
    textBox1.Text = textBox1.Text + 2;  
}  

private void button9_Click(object sender, EventArgs e)  
{  
    textBox1.Text = textBox1.Text + 1;  
}  

private void button13_Click(object sender, EventArgs e)  
{  
    textBox1.Text = textBox1.Text + 3;  
}  

private void button3_Click(object sender, EventArgs e)  
{  
    textBox1.Text = textBox1.Text + 6;  
}  

private void button2_Click(object sender, EventArgs e)  
{  
    textBox1.Text = textBox1.Text + 5;  
}  

private void button10_Click(object sender, EventArgs e)  
{  
    textBox1.Text = textBox1.Text + 4;  
}  

private void button6_Click(object sender, EventArgs e)  
{  
    textBox1.Text = textBox1.Text + 9;  
}  

private void button7_Click(object sender, EventArgs e)  
{  
    textBox1.Text = textBox1.Text + 8;  
}  

private void button1_Click(object sender, EventArgs e)  
{  
    textBox1.Text = textBox1.Text + 7;  
}  

private void button5_Click(object sender, EventArgs e)  
{  
    textBox1.Text = textBox1.Text + 0;  
}  

private void button16_Click(object sender, EventArgs e)  
{  
    textBox1.Text = textBox1.Text + 0 + 0;  
}  

private void btnPlus_Click(object sender, EventArgs e)  
{  
    num1 = float.Parse(textBox1.Text);  
    textBox1.Clear();  
    textBox1.Focus();  
    count = 2;  
     
}  

private void btnminus_Click(object sender, EventArgs e)  
{  
    if (textBox1.Text != "")  
    {  
        num1 = float.Parse(textBox1.Text);  
        textBox1.Clear();  
        textBox1.Focus();  
        count = 1;  
    }  
}  

private void btnmultiply_Click(object sender, EventArgs e)  
{  
    num1 = float.Parse(textBox1.Text);  
    textBox1.Clear();  
    textBox1.Focus();  
    count = 3;  
}  

private void btndivide_Click(object sender, EventArgs e)  
{  
    num1 = float.Parse(textBox1.Text);  
    textBox1.Clear();  
    textBox1.Focus();  
    count = 4;  
}  

private void btnequal_Click(object sender, EventArgs e)  
{  
    compute(count);  
}  
public void compute(int count)  
{  
    switch (count)  
    {  
        case 1:  
            ans = num1 - float.Parse(textBox1.Text);  
            textBox1.Text = ans.ToString();  
            break;  
        case 2:  
            ans = num1 + float.Parse(textBox1.Text);  
            textBox1.Text = ans.ToString();  
            break;  
        case 3:  
            ans = num1 * float.Parse(textBox1.Text);  
            textBox1.Text = ans.ToString();  
            break;  
        case 4:  
            ans = num1 / float.Parse(textBox1.Text);  
            textBox1.Text = ans.ToString();  
            break;  
        default:  
            break;  
    }  
}  

private void btnC_Click(object sender, EventArgs e)  
{  
    textBox1.Clear();  
    count = 0;   
}  

private void button15_Click(object sender, EventArgs e)  
{  
    int c = textBox1.TextLength;  
    int flag = 0;  
    string text = textBox1.Text;  
    for (int i = 0; i < c; i++)  
    {  
        if (text[i].ToString() == ".")  
        {  
            flag = 1; break;  
        }  
        else  
        {  
            flag = 0;  
        }  
    }  
    if (flag == 0)  
    {  
        textBox1.Text = textBox1.Text + ".";  
    }  
} 

Before starting with the code, you should declare the variable globally.

public Form1()  
{  
    InitializeComponent();  
}  
float num1, ans;  
int count;  

Output

Final Output