Home / Technology / Develop Windows Calculator using C#
calc_featured

Develop Windows Calculator using C#

After working extensively on Java and PHP back in college days, thought to try my hands on the famous Microsoft Technology: C#. Had been reading about .NET and C# for the past few days and after having practiced many basic programs, I decided to go a step further by trying My First Windows GUI based application using the C# knowledge I have accumulated.

Usually a Windows GUI based application will definitely contain the following three files:

  1. Program.cs: This is the main file which contains the main() function.
  2. form1.designer.cs: This is the file where the design related items are initialized, when you drag and drop a window item on the form, that item is automatically initialized in this file.
  3. form1.cs: This file contains the class, functions, codes etc that will drive the application.

So, it’s clear that all the ‘coding’ that is to be done, is done in the form1.cs file 😉

Moving on, the calculator developed is a very basic one and allows the user to perform simple arithmetic operations (Add, Subtract, Divide, Multiply). The application is developed using the basic inbuilt functions, and simplest of logic, hence might contain bugs. Anyways, I decided to pen down the code here so that it will help me in future and also many of you who want a simple way to create your First Windows Form Based Application !

Program.cs :

/*
* Basic Windows Form based Calculator Program using C#
* Developed By: Atulmaharaj
* File: Program.cs
*/</code></p>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace Test_Calc
{
static class Program
{
///
/// The main entry point for the application.
///

[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}

You can see that this file contains the main() method,thus this the entry point for the application.

Form1.designer.cs:

Well, the code is bit lengthy, hence you need to click to expand it. Nevertheless, these files are provided to download at the end of this post 🙂

/*
 * Basic Windows Form based Calculator Program using C#
 * Developed By: Atulmaharaj
 * File: Form1.Designer.cs
 */

namespace Test_Calc
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.button1 = new System.Windows.Forms.Button();
            this.button2 = new System.Windows.Forms.Button();
            this.button3 = new System.Windows.Forms.Button();
            this.button4 = new System.Windows.Forms.Button();
            this.button5 = new System.Windows.Forms.Button();
            this.button6 = new System.Windows.Forms.Button();
            this.button7 = new System.Windows.Forms.Button();
            this.button8 = new System.Windows.Forms.Button();
            this.button9 = new System.Windows.Forms.Button();
            this.button0 = new System.Windows.Forms.Button();
            this.button_minus = new System.Windows.Forms.Button();
            this.button12 = new System.Windows.Forms.Button();
            this.button13 = new System.Windows.Forms.Button();
            this.button14 = new System.Windows.Forms.Button();
            this.button16 = new System.Windows.Forms.Button();
            this.button10 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            //
            // textBox1
            //
            this.textBox1.Font = new System.Drawing.Font("Impact", 22F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.textBox1.Location = new System.Drawing.Point(12, 25);
            this.textBox1.Multiline = true;
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(260, 39);
            this.textBox1.TabIndex = 0;
            this.textBox1.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
            this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
            //
            // button1
            //
            this.button1.Location = new System.Drawing.Point(12, 91);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(36, 30);
            this.button1.TabIndex = 1;
            this.button1.Text = "1";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            //
            // button2
            //
            this.button2.Location = new System.Drawing.Point(65, 91);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(36, 30);
            this.button2.TabIndex = 2;
            this.button2.Text = "2";
            this.button2.UseVisualStyleBackColor = true;
            this.button2.Click += new System.EventHandler(this.button2_Click);
            //
            // button3
            //
            this.button3.Location = new System.Drawing.Point(117, 91);
            this.button3.Name = "button3";
            this.button3.Size = new System.Drawing.Size(36, 30);
            this.button3.TabIndex = 3;
            this.button3.Text = "3";
            this.button3.UseVisualStyleBackColor = true;
            this.button3.Click += new System.EventHandler(this.button3_Click);
            //
            // button4
            //
            this.button4.Location = new System.Drawing.Point(12, 136);
            this.button4.Name = "button4";
            this.button4.Size = new System.Drawing.Size(36, 30);
            this.button4.TabIndex = 4;
            this.button4.Text = "4";
            this.button4.UseVisualStyleBackColor = true;
            this.button4.Click += new System.EventHandler(this.button4_Click);
            //
            // button5
            //
            this.button5.Location = new System.Drawing.Point(65, 136);
            this.button5.Name = "button5";
            this.button5.Size = new System.Drawing.Size(36, 30);
            this.button5.TabIndex = 5;
            this.button5.Text = "5";
            this.button5.UseVisualStyleBackColor = true;
            this.button5.Click += new System.EventHandler(this.button5_Click);
            //
            // button6
            //
            this.button6.Location = new System.Drawing.Point(117, 136);
            this.button6.Name = "button6";
            this.button6.Size = new System.Drawing.Size(36, 30);
            this.button6.TabIndex = 6;
            this.button6.Text = "6";
            this.button6.UseVisualStyleBackColor = true;
            this.button6.Click += new System.EventHandler(this.button6_Click);
            //
            // button7
            //
            this.button7.Location = new System.Drawing.Point(12, 182);
            this.button7.Name = "button7";
            this.button7.Size = new System.Drawing.Size(36, 30);
            this.button7.TabIndex = 7;
            this.button7.Text = "7";
            this.button7.UseVisualStyleBackColor = true;
            this.button7.Click += new System.EventHandler(this.button7_Click);
            //
            // button8
            //
            this.button8.Location = new System.Drawing.Point(65, 182);
            this.button8.Name = "button8";
            this.button8.Size = new System.Drawing.Size(36, 30);
            this.button8.TabIndex = 8;
            this.button8.Text = "8";
            this.button8.UseVisualStyleBackColor = true;
            this.button8.Click += new System.EventHandler(this.button8_Click);
            //
            // button9
            //
            this.button9.Location = new System.Drawing.Point(117, 182);
            this.button9.Name = "button9";
            this.button9.Size = new System.Drawing.Size(36, 30);
            this.button9.TabIndex = 9;
            this.button9.Text = "9";
            this.button9.UseVisualStyleBackColor = true;
            this.button9.Click += new System.EventHandler(this.button9_Click);
            //
            // button0
            //
            this.button0.Location = new System.Drawing.Point(65, 231);
            this.button0.Name = "button0";
            this.button0.Size = new System.Drawing.Size(36, 30);
            this.button0.TabIndex = 10;
            this.button0.Text = "0";
            this.button0.UseVisualStyleBackColor = true;
            this.button0.Click += new System.EventHandler(this.button10_Click);
            //
            // button_minus
            //
            this.button_minus.Location = new System.Drawing.Point(185, 134);
            this.button_minus.Name = "button_minus";
            this.button_minus.Size = new System.Drawing.Size(36, 30);
            this.button_minus.TabIndex = 11;
            this.button_minus.Text = "-";
            this.button_minus.UseVisualStyleBackColor = true;
            this.button_minus.Click += new System.EventHandler(this.button_minus_Click);
            //
            // button12
            //
            this.button12.Location = new System.Drawing.Point(236, 134);
            this.button12.Name = "button12";
            this.button12.Size = new System.Drawing.Size(36, 30);
            this.button12.TabIndex = 12;
            this.button12.Text = "/";
            this.button12.UseVisualStyleBackColor = true;
            this.button12.Click += new System.EventHandler(this.button12_Click);
            //
            // button13
            //
            this.button13.Location = new System.Drawing.Point(236, 179);
            this.button13.Name = "button13";
            this.button13.Size = new System.Drawing.Size(36, 30);
            this.button13.TabIndex = 13;
            this.button13.Text = "x";
            this.button13.UseVisualStyleBackColor = true;
            this.button13.Click += new System.EventHandler(this.button13_Click);
            //
            // button14
            //
            this.button14.Location = new System.Drawing.Point(185, 179);
            this.button14.Name = "button14";
            this.button14.Size = new System.Drawing.Size(36, 30);
            this.button14.TabIndex = 14;
            this.button14.Text = "+";
            this.button14.UseVisualStyleBackColor = true;
            this.button14.Click += new System.EventHandler(this.button14_Click);
            //
            // button16
            //
            this.button16.Location = new System.Drawing.Point(185, 231);
            this.button16.Name = "button16";
            this.button16.Size = new System.Drawing.Size(87, 30);
            this.button16.TabIndex = 16;
            this.button16.Text = "=";
            this.button16.UseVisualStyleBackColor = true;
            this.button16.Click += new System.EventHandler(this.button16_Click);
            //
            // button10
            //
            this.button10.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(128)))), ((int)(((byte)(0)))));
            this.button10.Location = new System.Drawing.Point(185, 91);
            this.button10.Name = "button10";
            this.button10.Size = new System.Drawing.Size(87, 30);
            this.button10.TabIndex = 17;
            this.button10.Text = "AC";
            this.button10.UseVisualStyleBackColor = false;
            this.button10.Click += new System.EventHandler(this.button10_Click_1);
            //
            // Form1
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(284, 277);
            this.Controls.Add(this.button10);
            this.Controls.Add(this.button16);
            this.Controls.Add(this.button14);
            this.Controls.Add(this.button13);
            this.Controls.Add(this.button12);
            this.Controls.Add(this.button_minus);
            this.Controls.Add(this.button0);
            this.Controls.Add(this.button9);
            this.Controls.Add(this.button8);
            this.Controls.Add(this.button7);
            this.Controls.Add(this.button6);
            this.Controls.Add(this.button5);
            this.Controls.Add(this.button4);
            this.Controls.Add(this.button3);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.textBox1);
            this.Name = "Form1";
            this.Text = "First Calculator - Atulmaharaj";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Button button2;
        private System.Windows.Forms.Button button3;
        private System.Windows.Forms.Button button4;
        private System.Windows.Forms.Button button5;
        private System.Windows.Forms.Button button6;
        private System.Windows.Forms.Button button7;
        private System.Windows.Forms.Button button8;
        private System.Windows.Forms.Button button9;
        private System.Windows.Forms.Button button0;
        private System.Windows.Forms.Button button_minus;
        private System.Windows.Forms.Button button12;
        private System.Windows.Forms.Button button13;
        private System.Windows.Forms.Button button14;
        private System.Windows.Forms.Button button16;
        private System.Windows.Forms.Button button10;
    }
}

It’s clear from the above code that all that is added to the windows form, is initialized here. You can see all the buttons, textBox etc. have been initialized here. The pic below will make it more clear:

Calculator - Design
Calculator – Design

Now moving on to the main form1.cs file which is the heart of the program as it houses the main logic.


/*
* Basic Windows Form based Calculator Program using C#
* Developed By: Atulmaharaj
* File: Form1.cs
*/

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 Test_Calc
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

}
int num1 = 0;
int num2 = 0;
int num3 = 0;
char sign;

String s=null;

private void textBox1_TextChanged(object sender, EventArgs e)
{
textBox1.ReadOnly = true;
textBox1.BackColor = System.Drawing.SystemColors.Window;

}

private void button1_Click(object sender, EventArgs e)
{
s = s + "1";
textBox1.Text = s;

}

private void button2_Click(object sender, EventArgs e)
{
s= s + 2;
textBox1.Text = s;
}

private void button3_Click(object sender, EventArgs e)
{
s = s + 3;
textBox1.Text = s;
}

private void button4_Click(object sender, EventArgs e)
{
s = s + 4;
textBox1.Text = s;
}

private void button5_Click(object sender, EventArgs e)
{
s = s + 5;
textBox1.Text = s;
}

private void button6_Click(object sender, EventArgs e)
{
s = s + 6;
textBox1.Text = s;
}

private void button7_Click(object sender, EventArgs e)
{
s = s + 7;
textBox1.Text = s;
}

private void button8_Click(object sender, EventArgs e)
{
s = s + 8;
textBox1.Text = s;
}

private void button9_Click(object sender, EventArgs e)
{
s = s + 9;
textBox1.Text = s;
}

private void button10_Click(object sender, EventArgs e)
{
s = s + 0;
textBox1.Text = s;
}

private void button_minus_Click(object sender, EventArgs e)
{
int.TryParse(textBox1.Text, out num1);

if (num1 == 0)
{
MessageBox.Show("Error !! Please input a number first.", "Error !!", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
else
{
textBox1.Text = "-";
sign = char.Parse("-");
s = null;
}
}

private void button12_Click(object sender, EventArgs e)
{
int.TryParse(textBox1.Text, out num1);

if (num1 == 0)
{
MessageBox.Show("Error !! Please input a number first.", "Error !!", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
else
{
textBox1.Text = "/";
sign = char.Parse("/");
s = null;
}
}

private void button14_Click(object sender, EventArgs e)
{
int.TryParse(textBox1.Text, out num1);

if (num1 == 0)
{
MessageBox.Show("Error !! Please input a number first.", "Error !!", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
else
{
textBox1.Text = "+";
sign = char.Parse("+");
s = null;
}
}

private void button13_Click(object sender, EventArgs e)
{
int.TryParse(textBox1.Text, out num1);

if (num1 == 0)
{
MessageBox.Show("Error !! Please input a number first.", "Error !!", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
else
{
textBox1.Text = "x";
sign = char.Parse("x");
s = null;
}
}

private void button16_Click(object sender, EventArgs e)
{
int.TryParse(textBox1.Text,out num2);
if(num2==0)
{
MessageBox.Show("Error !! Please input a number first.","Error !!",MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
switch (sign)
{
case '-':
num3 = num1 - num2;
textBox1.Text = num3.ToString();
break;

case '/':
num3 = num1 / num2;
textBox1.Text = num3.ToString();
break;

case '+':
num3 = num1 + num2;
textBox1.Text = num3.ToString();
break;

case 'x':
num3 = num1 * num2;
textBox1.Text = num3.ToString();
break;

}

}

private void button10_Click_1(object sender, EventArgs e)
{
s = null;
textBox1.Text = s;
}
}
}

So, that’s it ! A simple et petit code to develop a simple Windows GUI Calculator. 

Here is the link to download the files(Program.cs, Form1.cs, Form1.designer.cs and the application.exe) that will help you. All you need to do is, create a new Windows Form Based Application using C#  in Visual Studio and then you can paste the above source codes in the respective files and Click on Build and Voila !

I hope that you found this post helpful in some way or the other. Well you can definitely assume this to be a Christmas Gift for you 😛

Merry Christmas !! Ho Ho Ho…

About Atulmaharaj

Maintaining the blog for the past 3 years, I'm a Foodie, Techie, Cricket Enthu, Traveler, Blogger and a Cook ! You'll find posts within a wide range of topics from food to technology. Favorite Quote: "Traveling is like reading a book, one who hasn't traveled, hasn't turned a page.

Get Amazing Stories

Get a weekly digest of all the posts from the blog delivered right to your inbox, just a click away, Sign Up Now - We promise, we'll never spam.
Name
Email address
Secure and Spam free...
%d bloggers like this: