C# Calculator Codes and Step-by-Step Explanation

07-01-2019 16:04
C# Calculator Codes and Step-by-Step Explanation

Making a C# calculator is among the beginner's practices for those who are just learning to program. Simply put together the C# codes you wrote in Visual Studio for the calculator you can make.


Now, let's make a calculator together.


Step 1: Creating a Form in Visual Studio


If you still haven't installed Visual Studio, the most widely used program for C#, you can download it here. If you know a little English, we recommend that you use English. There is also a Turkish version of Visual Studio. Even though Visual Studio is available for Mac, C# does not work on Mac computers. You need the Windows operating system.


After opening Visual Studio, choose New Project to create a Windows Forms Application. An empty Form1 box will appear on the screen.


Step 2: Placing the items

You see Toolbox on the left side of the screen and Properties on the right side. You can change the name of your form to Calculator from the properties of Form1 on the right, and place an icon if you want. You can make some changes in the properties section to organize the calculator. For example, if you change the StartPosition property to CenterScreen, it will appear in the middle of the screen when you start your calculator. Changing the FormBorderStyle property to FixedDialog allows you to fix the size of your calculator.


However, since we will focus on the codes of the calculator in this article, we will cut these features short for now.


Add a TextBox and a Button by dragging and dropping from the Toolbox on the left and adjust the button's size, font, text size as you want. Then copy-paste the button and place it as follows. You can write the numbers and symbols on the buttons by changing the Text property. Finally you need to get a calculator image.



Step 3: Coding

Now it's time to write the code. As you improve your C# knowledge and skills, you can explore or try different calculator codes. In this article, we will share the simplest C# calculator codes with you. To write code in Visual Studio, simply double-click the elements. When you click on the items, the area where you can write the code for what you want to do and what action you want to perform on this item opens.


The part that continues as Button1, Button2 below may vary depending on your order of buttons. Make sure you match the button with the code correctly. Coding is not learned without making mistakes! Don't be afraid to make mistakes.


If you are determined to learn C#, you can move forward with more confident steps with the book called Object Oriented Programming with C# prepared by Fahrettin Erdinç. The book, supported by video tutorials, is a great resource for those who want to learn programming from scratch.



The C# calculator codes we wrote are as follows:


using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;


namespace WindowsFormsApp2

{

public partial class Form1 : Form

{

double FirstNumber;

string Operation;

public Form1()

{

InitializeComponent();

}


private void button1_Click(object sender, EventArgs e)

{

if (textBox1.Text == "0" && textBox1.Text==null)

{

textBox1.Text = "1";

}

else

{

textBox1.Text = textBox1.Text + "1";

}

}


private void button2_Click(object sender, EventArgs e)

{

if (textBox1.Text == "0" && textBox1.Text == null)

{

textBox1.Text = "0";

}

else

{

textBox1.Text = textBox1.Text + "0";

}

}


private void button3_Click(object sender, EventArgs e)

{

if (textBox1.Text == "0" && textBox1.Text == null)

{

textBox1.Text = "2";

}

else

{

textBox1.Text = textBox1.Text + "2";

}

}


private void button4_Click(object sender, EventArgs e)

{

if (textBox1.Text == "0" && textBox1.Text == null)

{

textBox1.Text = "3";

}

else

{

textBox1.Text = textBox1.Text + "3";

}

}


private void button5_Click(object sender, EventArgs e)

{

if (textBox1.Text == "0" && textBox1.Text == null)

{

textBox1.Text = "4";

}

else

{

textBox1.Text = textBox1.Text + "4";

}

}


private void button6_Click(object sender, EventArgs e)

{

if (textBox1.Text == "0" && textBox1.Text == null)

{

textBox1.Text = "5";

}

else

{

textBox1.Text = textBox1.Text + "5";

}

}


private void button7_Click(object sender, EventArgs e)

{

if (textBox1.Text == "0" && textBox1.Text == null)

{

textBox1.Text = "6";

}

else

{

textBox1.Text = textBox1.Text + "6";

}

}


private void button8_Click(object sender, EventArgs e)

{

if (textBox1.Text == "0" && textBox1.Text == null)

{

textBox1.Text = "7";

}

else

{

textBox1.Text = textBox1.Text + "7";

}

}


private void button9_Click(object sender, EventArgs e)

{

if (textBox1.Text == "0" && textBox1.Text == null)

{

textBox1.Text = "8";

}

else

{

textBox1.Text = textBox1.Text + "8";

}

}


private void button10_Click(object sender, EventArgs e)

{

if (textBox1.Text == "0" && textBox1.Text == null)

{

textBox1.Text = "9";

}

else

{

textBox1.Text = textBox1.Text + "9";

}

}


private void button11_Click(object sender, EventArgs e)

{

textBox1.Text = textBox1.Text + ",";

}


private void button12_Click(object sender, EventArgs e)

{

FirstNumber = Convert.ToDouble(textBox1.Text);

textBox1.Text = null;

Operation = "+";

}


private void button13_Click(object sender, EventArgs e)

{

FirstNumber = Convert.ToDouble(textBox1.Text);

textBox1.Text = null;

Operation = "-";

}


private void button14_Click(object sender, EventArgs e)

{

FirstNumber = Convert.ToDouble(textBox1.Text);

textBox1.Text = null;

Operation = "*";

}


private void button15_Click(object sender, EventArgs e)

{

FirstNumber = Convert.ToDouble(textBox1.Text);

textBox1.Text = null;

Operation = "/";

}


private void button16_Click(object sender, EventArgs e)

{

double SecondNumber;

double result;


SecondNumber = Convert.ToDouble(textBox1.Text);


if (Operation=="+")

{

Result = (FirstNumber + SecondNumber);

textBox1.Text = Convert.ToString(Result);

FirstNumber = Result;

}

if (Operation == "-")

{

Result = (FirstNumber - SecondNumber);

textBox1.Text = Convert.ToString(Result);

FirstNumber = Result;


}

if (Operation == "*")

{

Result = (FirstNumber * SecondNumber);

textBox1.Text = Convert.ToString(Result);

FirstNumber = Result;

}

if (Operation == "/")

if(SecondNumber==0)

{

textBox1.Text = "Not applicable";

}

else

{

Result = (FirstNumber / SecondNumber);

textBox1.Text = Convert.ToString(Result);

FirstNumber = Result;

}

}


private void button17_Click(object sender, EventArgs e)

{

textBox1.Text = null;

}

}

}

If you want to have more information about the subject, you can review our Object Oriented Programming with C# book.


IdeaSoft® | E-Ticaret paketleri ile hazırlanmıştır.