How to Make a Calculator in C#

By FoxLearn 7/22/2024 2:13:46 AM   23.34K
Creating a simple calculator in a C# Windows Forms Application involves designing a UI with buttons for numbers, operations, and a text box to display input and results.

How to Create a Calculator in C#

Open Visual Studio and create a new C# Windows Forms Application project.

Next, Open your form desginer, then drag and drop the necessary controls (buttons, text box) from the Visual Studio toolbox onto the form. You can design a simple calculator as shown below.

c# calculator

Design your calculator UI using buttons for numbers (0-9), operators (+, -, *, /), and other functionalities (like clear, equals).

Place a TextBox control at the top to display input and results. Set its TextAlign property to Right for a cleaner look.

How to create a simple calculator in C# WinForms

Double-click on each button in the designer to generate the default click event handler, then implement the button click event handlers to update the TextBox (txtResult) based on user input as the following c# code.

// c# variables to store operands and operator
double value;
string coperator;
bool check;

Create the PNumber method allows you to display the numeric value that you enter in the TextBox control.

//Handle number button
private void PNumber(object sender, EventArgs e)
{
    if ((coperator == "+") || (coperator == "-") || (coperator == "*") || (coperator == "/"))
    {
        if (check)
        {
            check = false;
            txtResult.Text = "0";
        }
    }
    Button b = sender as Button;
    if (txtResult.Text == "0")
        txtResult.Text = b.Text;
    else
        txtResult.Text += b.Text;
}

Create the POperator method allows you to convert text to numeric.

//Handle operator button
private void POperator(object sender, EventArgs e)
{
    Button b = sender as Button;
    //Convert text to number
    value = double.Parse(txtResult.Text);
    coperator = b.Text;
    txtResult.Text += b.Text;
    check = true;
}

Double-click on the Equal button to generate click event handlers. In the event handlers, write code to handle button clicks and perform calculations based on the user input.

// c# method to handle equals button
private void button20_Click(object sender, EventArgs e)
{
    try
    {
        switch (coperator)
        {
            case "+":
                txtResult.Text = (value + double.Parse(txtResult.Text)).ToString();
                break;
            case "-":
                txtResult.Text = (value - double.Parse(txtResult.Text)).ToString();
                break;
            case "*":
                txtResult.Text = (value * double.Parse(txtResult.Text)).ToString();
                break;
            case "/":
                txtResult.Text = (value / double.Parse(txtResult.Text)).ToString();
                break;
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

Add the button click event handler to the C button allows you to reset the result value to zero.

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

Add the button click event handler to the CE button allows you to clear memory and reset the result value to zero.

//Clear memory
private void button10_Click(object sender, EventArgs e)
{
    txtResult.Text = "0";
    value = 0;
}

This is only a simple calculator that helps you perform some calculations. The application built is designed to emulate the windows calculator and is done in c# .net

VIDEO TUTORIAL