How To Make Calculator In C#?

Introduction

Today in this article, I will show you how to make a simple basic calculator in C#.

What are Global variables in C#?

In C#, global variables refer to variables that are declared at the class level, outside of any method or constructor. These variables are accessible throughout the class, and their values can be used across different methods within the same class. Global variables are often used to store data that needs to be shared among multiple methods or to maintain state information within a class.

Example

// Global variables
private double FirstNumber = 0.0;
private double SecondNumber = 0.0;
private string OperatorString = "+";
private double Result = 0.0;

calculator

What are Button events?

Button events in C# are a fundamental aspect of creating interactive graphical user interfaces (GUIs) in Windows Forms, WPF, and other UI frameworks. Buttons are commonly used UI elements that allow users to trigger specific actions or functions in a software application when clicked. Events associated with buttons are used to capture and respond to user interactions, making applications responsive and interactive.

Example

// Zero button click event
private void ZeroButton_Click(object sender, EventArgs e)
{
    ZeroRemove(0);
}
// Zero remove function and here other number will add to the textbox
private void ZeroRemove(int number)
{
    if (CalcTextBox.Text == "0")
        CalcTextBox.Text = number.ToString();
    else
        CalcTextBox.Text += number.ToString();
}
// Three button click event
private void OneButton_Click(object sender, EventArgs e)
{
    ZeroRemove(1);
}
// Three button click event
private void TwoButton_Click(object sender, EventArgs e)
{
    ZeroRemove(2);
}
// Three button click event
private void ThreeButton_Click(object sender, EventArgs e)
{
    ZeroRemove(3);
}
private void FourButton_Click(object sender, EventArgs e)
{
    ZeroRemove(4);
}
private void FiveButton_Click(object sender, EventArgs e)
{
    ZeroRemove(5);
}
private void SixButton_Click(object sender, EventArgs e)
{
    ZeroRemove(6);
}
private void SevenButton_Click(object sender, EventArgs e)
{
    ZeroRemove(7);
}
private void EightButton_Click(object sender, EventArgs e)
{
    ZeroRemove(8);
}
private void NineButton_Click(object sender, EventArgs e)
{
    ZeroRemove(9);
}
// Clear button click event; here, clear the textbox
private void ClearButton_Click(object sender, EventArgs e)
{
    CalcTextBox.Clear();
    CalcTextBox.Text = "0";
    FirstNumber = 0.0;
    SecondNumber = 0.0;
    Result = 0.0;
}
// Here, put a period in the textbox
private void DoBtutton_Click(object sender, EventArgs e)
{
    if (!CalcTextBox.Text.Contains("."))
        CalcTextBox.Text += ".";
}
// Plus button
private void PlusButton_Click(object sender, EventArgs e)
{
    SuppliedOperator("+");
}
// It is a general function that receives the incoming operator
private

I have also attached the source code so you can download it and run it on your PC.


Similar Articles