How to Create Basic Calculator in C#
Basic Calculator- a word which we have heard several times and used it more than 1000 times to calculate, work or entertain. Have you ever wondered that how it works or how can we create a calculator through programming.
In this article we will let you know how to develop and customize a calculator desktop application in visual basic ASP.net using C#.If you are interested in asp.net development, then this software is must. You can download Visual basic by following this link.
All you need to do is to Install visual studio and follow these instructions. We are going to develop a customized calculator app in Windows Form Application.
How to Create Basic Calculator in C#
Start Visual Studio and click Visual C# from left hand sidebar. Then from middle field select the Windows Form Application, change its name, Location and solution name from bottom of the tab according to your preferences or leave it as it is. Before clicking OK please do check that ‘Create Directory for Solution’ is checked and ‘Add to Source Control’ is unchecked. Then click ‘OK’ .
After few seconds, a form will open named as ‘Form1.cs[Design]’ and in this form a window named Form1 will appear. This Form1 window is the design part of our application i.e. ‘Calculator’. For coding part you can double click on this window, a new tab will get open which contains the coding part i.e. Form1.cs.
Now coming to designer part, click on the ToolBox tab align at left hand side of your screen. If not found you can also select it from View tab, 15th from top. Select the required Text Box and Buttons from the given list in the ToolBox Panel. You can drag and drop or double click to get the required tool on your design form. Complete the design as shown in image below or do it according to your creativity.
After completing the designing phase, double click on any button to get its click event in the coding part.
Do it one by one or simply copy this visual basic 2010 calculator code in the coding part but don’t forget to add the click event in properties tab.
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace balliacity_Calc { public partial class Form1 : Form { public Form1() { InitializeComponent(); } int temp=0; string op="op"; double val=0.0; private void result() { if (op == "+") { textBox1.Text = (val + Convert.ToDouble(textBox1.Text)).ToString(); } else if (op == "-") { textBox1.Text = (val - Convert.ToDouble(textBox1.Text)).ToString(); } else if (op == "*") { textBox1.Text = (val * Convert.ToDouble(textBox1.Text)).ToString(); } else if (op == "/") { textBox1.Text = (val / Convert.ToDouble(textBox1.Text)).ToString(); } } private void button1_Click(object sender, EventArgs e) { if (textBox1.Text != "") { if (op != "op") { result(); } val = Convert.ToDouble(textBox1.Text); op = "+"; textBox2.Text = textBox1.Text + op; textBox1.Text = ""; } } private void button2_Click(object sender, EventArgs e) { if (textBox1.Text != "") { if (op != "op") { result(); } val = Convert.ToDouble(textBox1.Text); op = "-"; textBox2.Text = textBox1.Text + op; textBox1.Text = ""; } } private void button3_Click(object sender, EventArgs e) { if (textBox1.Text != "") { if (op != "op") { result(); } val = Convert.ToDouble(textBox1.Text); op = "*"; textBox2.Text = textBox1.Text + op; textBox1.Text = ""; } } private void button4_Click(object sender, EventArgs e) { if (textBox1.Text != "") { if (op != "op") { result(); } val = Convert.ToDouble(textBox1.Text); op = "/"; textBox2.Text = textBox1.Text + op; textBox1.Text = ""; } } private void button16_Click(object sender, EventArgs e) { if (textBox1.Text != "") { if (op == "+") { textBox1.Text = (val + Convert.ToDouble(textBox1.Text)).ToString(); } else if (op == "-") { textBox1.Text = (val - Convert.ToDouble(textBox1.Text)).ToString(); } else if (op == "*") { textBox1.Text = (val * Convert.ToDouble(textBox1.Text)).ToString(); } else if (op == "/") { textBox1.Text = (val / Convert.ToDouble(textBox1.Text)).ToString(); } val = 0; } } private void Number1_Click(object sender, EventArgs e) { temp = 1; textBox1.Text =textBox1.Text+ temp.ToString(); } private void button6_Click(object sender, EventArgs e) { temp = 2; textBox1.Text = textBox1.Text + temp.ToString(); } private void button7_Click(object sender, EventArgs e) { temp = 3; textBox1.Text = textBox1.Text + temp.ToString(); } private void button8_Click(object sender, EventArgs e) { temp = 4; textBox1.Text = textBox1.Text + temp.ToString(); } private void button9_Click(object sender, EventArgs e) { temp = 5; textBox1.Text = textBox1.Text + temp.ToString(); } private void button10_Click(object sender, EventArgs e) { temp = 6; textBox1.Text = textBox1.Text + temp.ToString(); } private void button11_Click(object sender, EventArgs e) { temp = 7; textBox1.Text = textBox1.Text + temp.ToString(); } private void button12_Click(object sender, EventArgs e) { temp = 8; textBox1.Text = textBox1.Text + temp.ToString(); } private void button13_Click(object sender, EventArgs e) { temp = 9; textBox1.Text = textBox1.Text + temp.ToString(); } private void button14_Click(object sender, EventArgs e) { temp = 0; textBox1.Text = textBox1.Text + temp.ToString(); } private void button15_Click(object sender, EventArgs e) { textBox1.Text = textBox1.Text + "."; } private void button5_Click(object sender, EventArgs e) { val = 0; op = "op"; textBox2.Text=textBox1.Text = ""; } private void label1_Click(object sender, EventArgs e) { System.Diagnostics.Process.Start("http://sourcecodessite.com"); } } }
After completing all these steps your Basic Calculator is ready, which Adds, Subtracts, Multiplies and Divide both Integer values as well as Float values.
You can also download the complete Appliction here and Install it in your PC.
Note: Microsoft .Net Framework 4 Client Profile is required to run this Application correctly. Visual Studio Installs this framework by Default.
Its a good and superb
Shukria
Happy to help 😉