Windows Application - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Win App Calculator

Windows Application Calculator

shape Steps

To create a simple calculator follow the below steps.

shape Step 1

Design the form as shown in the below figure. In the above design, I have taken one text box control and all the remaining are button controls.

shape Step 2

Double click on the all the button controls and raise the click events. Now, write the following code. [csharp] 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 WindowsFormsApplication30 { public partial class Form1 : Form { int a, b; string s; double result; public Form1() { InitializeComponent(); } private void btn_1_Click(object sender, EventArgs e) { txtbx.Text = txtbx.Text + btn_1.Text; } private void btn_2_Click(object sender, EventArgs e) { txtbx.Text = txtbx.Text + btn_2.Text; } private void btn_3_Click(object sender, EventArgs e) { txtbx.Text = txtbx.Text + btn_3.Text; } private void btn_4_Click(object sender, EventArgs e) { txtbx.Text = txtbx.Text + btn_4.Text; } private void btn_5_Click(object sender, EventArgs e) { txtbx.Text = txtbx.Text + btn_5.Text; } private void btn_6_Click(object sender, EventArgs e) { txtbx.Text = txtbx.Text + btn_6.Text; } private void btn_7_Click(object sender, EventArgs e) { txtbx.Text = txtbx.Text + btn_7.Text; } private void btn_8_Click(object sender, EventArgs e) { txtbx.Text = txtbx.Text + btn_8.Text; } private void btn_9_Click(object sender, EventArgs e) { txtbx.Text = txtbx.Text + btn_9.Text; } private void btn_0_Click(object sender, EventArgs e) { txtbx.Text = txtbx.Text + btn_0.Text; } private void btn_add_Click(object sender, EventArgs e) { s = "+"; a = int.Parse(txtbx.Text); txtbx.Text = ""; } private void btn_sub_Click(object sender, EventArgs e) { s = "-"; a = int.Parse(txtbx.Text); txtbx.Text = ""; } private void btn_mul_Click(object sender, EventArgs e) { s = "*"; a = int.Parse(txtbx.Text); txtbx.Text = ""; } private void btn_div_Click(object sender, EventArgs e) { s = "/"; a = int.Parse(txtbx.Text); txtbx.Text = ""; } private void btn_sqrt_Click(object sender, EventArgs e) { s = "^"; a = int.Parse(txtbx.Text); txtbx.Text = ""; } private void btn_result_Click(object sender, EventArgs e) { b = int.Parse(txtbx.Text); switch (s) { case "+": result = a + b; txtbx.Text = result.ToString(); break; case "-": result = a - b; txtbx.Text = result.ToString(); break; case "*": result = a * b; txtbx.Text = result.ToString(); break; case "/": result = a / b; txtbx.Text = result.ToString(); break; case "^": result = Math.Pow(a, b); txtbx.Text = result.ToString(); break; } } private void btn_clear_Click(object sender, EventArgs e) { s = null; a = 0; b = 0; result = 0; txtbx.Text = ""; } private void btn_ON_Click(object sender, EventArgs e) { btn_0.Enabled = btn_1.Enabled = btn_2.Enabled = btn_3.Enabled = btn_4.Enabled = btn_5.Enabled = btn_6.Enabled = btn_7.Enabled = btn_8.Enabled = btn_9.Enabled = btn_add.Enabled = btn_clear.Enabled = btn_div.Enabled = btn_mul.Enabled = btn_sqrt.Enabled = btn_sub.Enabled = true; } private void btn_OFF_Click(object sender, EventArgs e) { btn_0.Enabled = btn_1.Enabled = btn_2.Enabled = btn_3.Enabled = btn_4.Enabled = btn_5.Enabled = btn_6.Enabled = btn_7.Enabled = btn_8.Enabled = btn_9.Enabled = btn_add.Enabled = btn_clear.Enabled = btn_div.Enabled = btn_mul.Enabled = btn_sqrt.Enabled = btn_sub.Enabled = false; } private void btn_delete_Click(object sender, EventArgs e) { string d = txtbx.Text; if (d.Length > 1) { d = d.Substring(0, d.Length - 1); } else { d = "0"; } txtbx.Text = d; } private void Form1_Load(object sender, EventArgs e) { } } } [/csharp]

shape Step 3

Press f5 to run the application. Now, perform the operation. The calculator works as real time calculator. The calculator looks like the following figure.