C# Projects - Calculator

Calculator

Open Visual Studio
c# projects calculator

Create a new project
visual studio 2019


Select Windows form app (.NET Framework)
visual studio 2019
Name the project as Calculator
c# projects calculator

Now Lets start to build our project.................

Change title


In the Right side of the window you can see the Properties box. Go to the properties and change the Text Form1 to Calculator
c# projects calculator

Change Icon

Scroll down the properties
there you can see Icon
c# projects calculator
Click on it. and select the icon image for your calculator app
c# projects calculator
c# projects calculator

Open the image.
c# projects calculator

Adding and customizing buttons

Go to toolbar in the left side of your Visual Studio Window
visual studio 2019
Drag and drop Button from there.
visual studio 2019

Resize the  button, Go to properties and select size.
I resize the button to 35X35(You can resize it as you wish)
c# projects calculator
c# projects calculator

Change the buton text and make the font bold from the properties
c# projects calculator

Arrange more buttons as shown in below picture
c# projects calculator
Then change the texts
c# projects calculator
Now we need a Text box
C# project calculator


c# project - calculator

change the textbox name in properties
I changed it into result_textbox
c# projects calculator

Now we want the text box filled with the number we clicked.
c# project calculator
First click on a button, here I clicked on '0'
c# project calculator

Select the Events option in properties.
c# project calculator
Change click event into 'button_click'.Then double click on it.
c# project calculator
Now we need to enter the event code here.our code is
            result_textBox.Text = result_textBox.Text + "0";
c# project calculator

This same event is repeated for the other numbers and '.' buttons so copy the click event we just written and paste it on all the numbers and '.'(dot).
c# project calculator

c# project calculator

Now we need to change the button_click code a little because in the case of '0' button we given the text "0" but in the cases of other numbers we need to give the corresponding texts.
Button button = (Button)sender;
            result_textBox.Text = result_textBox.Text + button.Text;
c# project calculator

just check the program by running it.
c# project calculator



We know the calculator text align is from right. here it is left by default so change it in the properties.
c# projects calculator

c# projects calculator

In the text box we need "0" as default.
c# project calculator
when a number is clicked the '0' must be cleared.So we can make a change in the code.
if (result_textBox.Text=="0")
result_textBox.Text.clear();
c# projects calculator

Now write the code for operator buttons (+ , - , * , /). 
Operator_click is the event name I given to the event.
c# project calculator

Now we go to the code section
First we need two variables declared in the starting of the code.
Double resultValue=0;
string operationPerformed="";
c# project calculator

Our operator_click code is
private void operator_click(object sender, EventArgs e)
        {
            Button button = (Button)sender;
            operationPerformed = button.Text;
            resultValue = Double.Parse(result_textBox.Text);
        }
c# projects calculator

Now we can give the code for equals button. I give the name "equals_click" for the event.

c# projects calculator

The code for the equals_click is,

 private void equals_click(object sender, EventArgs e)
        {


            
            if (operationPerformed == "+")
            {
                resultValue += Double.Parse(result_textBox.Text);
            }
            else if (operationPerformed == "-")
            {
                resultValue -= Double.Parse(result_textBox.Text);
            }
            else if (operationPerformed == "*")
            {
                resultValue *= Double.Parse(result_textBox.Text);
            }
            else if (operationPerformed == "/")
            {
                resultValue /= Double.Parse(result_textBox.Text);
            }
            else
            {
                resultValue = Double.Parse(result_textBox.Text);
            }
            result_textBox.Text = resultValue.ToString();
            
        }

In the above code we are using if else statement to identify the operator.

At last we need the CE and C buttons to be coded. The C is used to clear all and CE is used to clear the current entry from the textbox. For that we use the below code.
C# projects calculator

For clear_click(C):

private void clear_click(object sender, EventArgs e)
        {
            result_textBox.Text="0";
            resultValue = 0;
        }
c# projects calculator

For clearEntry_click(CE):

private void clear_click(object sender, EventArgs e)
        {
            result_textBox.Text="0";
          
        }
c# projects - calculator

Now the calculator application is ready, Run it.

c# projects calculator

making the installation setup for this application is uploaded shortly.