C# - Simple Calculator

In this tutorial we will create a Simple Calculator using C#. C# is a general-purpose, object-oriented programming language. C# is expected to make it faster and less expensive to develop new application. The potential of C# is great when it comes in developing desktop application. It has a friendly environment for all new developers. So let's do the coding.

Getting Started

First you will have to download & install the Visual Studio. Visual Studios is an open source development feel free to create any application that you want. Here's the link for the Visual Studio https://www.visualstudio.com/

Application Design

We will now create the design for the application, first locate the designer file called form1.Designer.cs, this is the default name when you create a new windows form. Then write these codes inside your designer file.
  1. namespace Simple_Calculator
  2. {
  3.     partial class Form1
  4.     {
  5.         /// <summary>
  6.         /// Required designer variable.
  7.         /// </summary>
  8.         private System.ComponentModel.IContainer components = null;
  9.  
  10.         /// <summary>
  11.         /// Clean up any resources being used.
  12.         /// </summary>
  13.         /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
  14.         protected override void Dispose(bool disposing)
  15.         {
  16.             if (disposing && (components != null))
  17.             {
  18.                 components.Dispose();
  19.             }
  20.             base.Dispose(disposing);
  21.         }
  22.  
  23.         #region Windows Form Designer generated code
  24.  
  25.         /// <summary>
  26.         /// Required method for Designer support - do not modify
  27.         /// the contents of this method with the code editor.
  28.         /// </summary>
  29.         private void InitializeComponent()
  30.         {
  31.             this.btn_one = new System.Windows.Forms.Button();
  32.             this.btn_two = new System.Windows.Forms.Button();
  33.             this.btn_three = new System.Windows.Forms.Button();
  34.             this.btn_subtract = new System.Windows.Forms.Button();
  35.             this.btn_four = new System.Windows.Forms.Button();
  36.             this.btn_five = new System.Windows.Forms.Button();
  37.             this.btn_six = new System.Windows.Forms.Button();
  38.             this.btn_multiply = new System.Windows.Forms.Button();
  39.             this.btn_seven = new System.Windows.Forms.Button();
  40.             this.btn_eight = new System.Windows.Forms.Button();
  41.             this.btn_nine = new System.Windows.Forms.Button();
  42.             this.btn_divide = new System.Windows.Forms.Button();
  43.             this.btn_addition = new System.Windows.Forms.Button();
  44.             this.btn_dot = new System.Windows.Forms.Button();
  45.             this.btn_zero = new System.Windows.Forms.Button();
  46.             this.btn_equal = new System.Windows.Forms.Button();
  47.             this.btn_clearall = new System.Windows.Forms.Button();
  48.             this.btn_clear = new System.Windows.Forms.Button();
  49.             this.tbox_result = new System.Windows.Forms.TextBox();
  50.             this.lbl_display = new System.Windows.Forms.Label();
  51.             this.SuspendLayout();
  52.             //
  53.             // btn_one
  54.             //
  55.             this.btn_one.Font = new System.Drawing.Font("Arial", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  56.             this.btn_one.Location = new System.Drawing.Point(44, 377);
  57.             this.btn_one.Name = "btn_one";
  58.             this.btn_one.Size = new System.Drawing.Size(75, 71);
  59.             this.btn_one.TabIndex = 0;
  60.             this.btn_one.Text = "1";
  61.             this.btn_one.UseVisualStyleBackColor = true;
  62.             this.btn_one.Click += new System.EventHandler(this.btn_one_Click);
  63.             //
  64.             // btn_two
  65.             //
  66.             this.btn_two.Font = new System.Drawing.Font("Arial", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  67.             this.btn_two.Location = new System.Drawing.Point(158, 377);
  68.             this.btn_two.Name = "btn_two";
  69.             this.btn_two.Size = new System.Drawing.Size(75, 71);
  70.             this.btn_two.TabIndex = 1;
  71.             this.btn_two.Text = "2";
  72.             this.btn_two.UseVisualStyleBackColor = true;
  73.             this.btn_two.Click += new System.EventHandler(this.btn_two_Click);
  74.             //
  75.             // btn_three
  76.             //
  77.             this.btn_three.Font = new System.Drawing.Font("Arial", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  78.             this.btn_three.Location = new System.Drawing.Point(267, 377);
  79.             this.btn_three.Name = "btn_three";
  80.             this.btn_three.Size = new System.Drawing.Size(75, 71);
  81.             this.btn_three.TabIndex = 2;
  82.             this.btn_three.Text = "3";
  83.             this.btn_three.UseVisualStyleBackColor = true;
  84.             this.btn_three.Click += new System.EventHandler(this.btn_three_Click);
  85.             //
  86.             // btn_subtract
  87.             //
  88.             this.btn_subtract.Font = new System.Drawing.Font("Arial", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  89.             this.btn_subtract.Location = new System.Drawing.Point(388, 377);
  90.             this.btn_subtract.Name = "btn_subtract";
  91.             this.btn_subtract.Size = new System.Drawing.Size(75, 71);
  92.             this.btn_subtract.TabIndex = 3;
  93.             this.btn_subtract.Text = "-";
  94.             this.btn_subtract.UseVisualStyleBackColor = true;
  95.             this.btn_subtract.Click += new System.EventHandler(this.btn_subtract_Click);
  96.             //
  97.             // btn_four
  98.             //
  99.             this.btn_four.Font = new System.Drawing.Font("Arial", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  100.             this.btn_four.Location = new System.Drawing.Point(44, 273);
  101.             this.btn_four.Name = "btn_four";
  102.             this.btn_four.Size = new System.Drawing.Size(75, 71);
  103.             this.btn_four.TabIndex = 4;
  104.             this.btn_four.Text = "4";
  105.             this.btn_four.UseVisualStyleBackColor = true;
  106.             this.btn_four.Click += new System.EventHandler(this.btn_four_Click);
  107.             //
  108.             // btn_five
  109.             //
  110.             this.btn_five.Font = new System.Drawing.Font("Arial", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  111.             this.btn_five.Location = new System.Drawing.Point(158, 273);
  112.             this.btn_five.Name = "btn_five";
  113.             this.btn_five.Size = new System.Drawing.Size(75, 71);
  114.             this.btn_five.TabIndex = 5;
  115.             this.btn_five.Text = "5";
  116.             this.btn_five.UseVisualStyleBackColor = true;
  117.             this.btn_five.Click += new System.EventHandler(this.btn_five_Click);
  118.             //
  119.             // btn_six
  120.             //
  121.             this.btn_six.Font = new System.Drawing.Font("Arial", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  122.             this.btn_six.Location = new System.Drawing.Point(267, 273);
  123.             this.btn_six.Name = "btn_six";
  124.             this.btn_six.Size = new System.Drawing.Size(75, 71);
  125.             this.btn_six.TabIndex = 6;
  126.             this.btn_six.Text = "6";
  127.             this.btn_six.UseVisualStyleBackColor = true;
  128.             this.btn_six.Click += new System.EventHandler(this.btn_six_Click);
  129.             //
  130.             // btn_multiply
  131.             //
  132.             this.btn_multiply.Font = new System.Drawing.Font("Arial", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  133.             this.btn_multiply.Location = new System.Drawing.Point(388, 273);
  134.             this.btn_multiply.Name = "btn_multiply";
  135.             this.btn_multiply.Size = new System.Drawing.Size(75, 71);
  136.             this.btn_multiply.TabIndex = 7;
  137.             this.btn_multiply.Text = "*";
  138.             this.btn_multiply.UseVisualStyleBackColor = true;
  139.             this.btn_multiply.Click += new System.EventHandler(this.btn_multiply_Click);
  140.             //
  141.             // btn_seven
  142.             //
  143.             this.btn_seven.Font = new System.Drawing.Font("Arial", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  144.             this.btn_seven.Location = new System.Drawing.Point(44, 170);
  145.             this.btn_seven.Name = "btn_seven";
  146.             this.btn_seven.Size = new System.Drawing.Size(75, 71);
  147.             this.btn_seven.TabIndex = 8;
  148.             this.btn_seven.Text = "7";
  149.             this.btn_seven.UseVisualStyleBackColor = true;
  150.             this.btn_seven.Click += new System.EventHandler(this.btn_seven_Click);
  151.             //
  152.             // btn_eight
  153.             //
  154.             this.btn_eight.Font = new System.Drawing.Font("Arial", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  155.             this.btn_eight.Location = new System.Drawing.Point(158, 170);
  156.             this.btn_eight.Name = "btn_eight";
  157.             this.btn_eight.Size = new System.Drawing.Size(75, 71);
  158.             this.btn_eight.TabIndex = 9;
  159.             this.btn_eight.Text = "8";
  160.             this.btn_eight.UseVisualStyleBackColor = true;
  161.             this.btn_eight.Click += new System.EventHandler(this.btn_eight_Click);
  162.             //
  163.             // btn_nine
  164.             //
  165.             this.btn_nine.Font = new System.Drawing.Font("Arial", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  166.             this.btn_nine.Location = new System.Drawing.Point(267, 170);
  167.             this.btn_nine.Name = "btn_nine";
  168.             this.btn_nine.Size = new System.Drawing.Size(75, 71);
  169.             this.btn_nine.TabIndex = 10;
  170.             this.btn_nine.Text = "9";
  171.             this.btn_nine.UseVisualStyleBackColor = true;
  172.             this.btn_nine.Click += new System.EventHandler(this.btn_nine_Click);
  173.             //
  174.             // btn_divide
  175.             //
  176.             this.btn_divide.Font = new System.Drawing.Font("Arial", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  177.             this.btn_divide.Location = new System.Drawing.Point(388, 170);
  178.             this.btn_divide.Name = "btn_divide";
  179.             this.btn_divide.Size = new System.Drawing.Size(75, 71);
  180.             this.btn_divide.TabIndex = 11;
  181.             this.btn_divide.Text = "/";
  182.             this.btn_divide.UseVisualStyleBackColor = true;
  183.             this.btn_divide.Click += new System.EventHandler(this.btn_divide_Click);
  184.             //
  185.             // btn_addition
  186.             //
  187.             this.btn_addition.Font = new System.Drawing.Font("Arial", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  188.             this.btn_addition.Location = new System.Drawing.Point(388, 479);
  189.             this.btn_addition.Name = "btn_addition";
  190.             this.btn_addition.Size = new System.Drawing.Size(75, 71);
  191.             this.btn_addition.TabIndex = 15;
  192.             this.btn_addition.Text = "+";
  193.             this.btn_addition.UseVisualStyleBackColor = true;
  194.             this.btn_addition.Click += new System.EventHandler(this.btn_addition_Click);
  195.             //
  196.             // btn_dot
  197.             //
  198.             this.btn_dot.Font = new System.Drawing.Font("Arial", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  199.             this.btn_dot.Location = new System.Drawing.Point(267, 479);
  200.             this.btn_dot.Name = "btn_dot";
  201.             this.btn_dot.Size = new System.Drawing.Size(75, 71);
  202.             this.btn_dot.TabIndex = 14;
  203.             this.btn_dot.Text = ".";
  204.             this.btn_dot.UseVisualStyleBackColor = true;
  205.             //
  206.             // btn_zero
  207.             //
  208.             this.btn_zero.Font = new System.Drawing.Font("Arial", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  209.             this.btn_zero.Location = new System.Drawing.Point(158, 479);
  210.             this.btn_zero.Name = "btn_zero";
  211.             this.btn_zero.Size = new System.Drawing.Size(75, 71);
  212.             this.btn_zero.TabIndex = 13;
  213.             this.btn_zero.Text = "0";
  214.             this.btn_zero.UseVisualStyleBackColor = true;
  215.             this.btn_zero.Click += new System.EventHandler(this.btn_zero_Click);
  216.             //
  217.             // btn_equal
  218.             //
  219.             this.btn_equal.Font = new System.Drawing.Font("Arial", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  220.             this.btn_equal.Location = new System.Drawing.Point(44, 479);
  221.             this.btn_equal.Name = "btn_equal";
  222.             this.btn_equal.Size = new System.Drawing.Size(75, 71);
  223.             this.btn_equal.TabIndex = 12;
  224.             this.btn_equal.Text = "=";
  225.             this.btn_equal.UseVisualStyleBackColor = true;
  226.             this.btn_equal.Click += new System.EventHandler(this.btn_equal_Click);
  227.             //
  228.             // btn_clearall
  229.             //
  230.             this.btn_clearall.Font = new System.Drawing.Font("Arial", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  231.             this.btn_clearall.Location = new System.Drawing.Point(44, 573);
  232.             this.btn_clearall.Name = "btn_clearall";
  233.             this.btn_clearall.Size = new System.Drawing.Size(189, 71);
  234.             this.btn_clearall.TabIndex = 16;
  235.             this.btn_clearall.Text = "CE";
  236.             this.btn_clearall.UseVisualStyleBackColor = true;
  237.             this.btn_clearall.Click += new System.EventHandler(this.btn_clearall_Click);
  238.             //
  239.             // btn_clear
  240.             //
  241.             this.btn_clear.Font = new System.Drawing.Font("Arial", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  242.             this.btn_clear.Location = new System.Drawing.Point(267, 573);
  243.             this.btn_clear.Name = "btn_clear";
  244.             this.btn_clear.Size = new System.Drawing.Size(196, 71);
  245.             this.btn_clear.TabIndex = 17;
  246.             this.btn_clear.Text = "C";
  247.             this.btn_clear.UseVisualStyleBackColor = true;
  248.             this.btn_clear.Click += new System.EventHandler(this.btn_clear_Click);
  249.             //
  250.             // tbox_result
  251.             //
  252.             this.tbox_result.Font = new System.Drawing.Font("Arial", 30F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  253.             this.tbox_result.Location = new System.Drawing.Point(44, 42);
  254.             this.tbox_result.Multiline = true;
  255.             this.tbox_result.Name = "tbox_result";
  256.             this.tbox_result.Size = new System.Drawing.Size(419, 108);
  257.             this.tbox_result.TabIndex = 18;
  258.             this.tbox_result.Text = "0";
  259.             this.tbox_result.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
  260.             //
  261.             // lbl_display
  262.             //
  263.             this.lbl_display.AutoSize = true;
  264.             this.lbl_display.BackColor = System.Drawing.Color.White;
  265.             this.lbl_display.Font = new System.Drawing.Font("Arial", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  266.             this.lbl_display.ForeColor = System.Drawing.SystemColors.ActiveBorder;
  267.             this.lbl_display.Location = new System.Drawing.Point(57, 117);
  268.             this.lbl_display.Name = "lbl_display";
  269.             this.lbl_display.Size = new System.Drawing.Size(0, 23);
  270.             this.lbl_display.TabIndex = 19;
  271.             //
  272.             // Form1
  273.             //
  274.             this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 20F);
  275.             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  276.             this.BackColor = System.Drawing.Color.SkyBlue;
  277.             this.ClientSize = new System.Drawing.Size(521, 667);
  278.             this.Controls.Add(this.lbl_display);
  279.             this.Controls.Add(this.tbox_result);
  280.             this.Controls.Add(this.btn_clear);
  281.             this.Controls.Add(this.btn_clearall);
  282.             this.Controls.Add(this.btn_addition);
  283.             this.Controls.Add(this.btn_dot);
  284.             this.Controls.Add(this.btn_zero);
  285.             this.Controls.Add(this.btn_equal);
  286.             this.Controls.Add(this.btn_divide);
  287.             this.Controls.Add(this.btn_nine);
  288.             this.Controls.Add(this.btn_eight);
  289.             this.Controls.Add(this.btn_seven);
  290.             this.Controls.Add(this.btn_multiply);
  291.             this.Controls.Add(this.btn_six);
  292.             this.Controls.Add(this.btn_five);
  293.             this.Controls.Add(this.btn_four);
  294.             this.Controls.Add(this.btn_subtract);
  295.             this.Controls.Add(this.btn_three);
  296.             this.Controls.Add(this.btn_two);
  297.             this.Controls.Add(this.btn_one);
  298.             this.Name = "Form1";
  299.             this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
  300.             this.Text = "Simple Calculator";
  301.             this.ResumeLayout(false);
  302.             this.PerformLayout();
  303.  
  304.         }
  305.  
  306.         #endregion
  307.  
  308.         private System.Windows.Forms.Button btn_one;
  309.         private System.Windows.Forms.Button btn_two;
  310.         private System.Windows.Forms.Button btn_three;
  311.         private System.Windows.Forms.Button btn_subtract;
  312.         private System.Windows.Forms.Button btn_four;
  313.         private System.Windows.Forms.Button btn_five;
  314.         private System.Windows.Forms.Button btn_six;
  315.         private System.Windows.Forms.Button btn_multiply;
  316.         private System.Windows.Forms.Button btn_seven;
  317.         private System.Windows.Forms.Button btn_eight;
  318.         private System.Windows.Forms.Button btn_nine;
  319.         private System.Windows.Forms.Button btn_divide;
  320.         private System.Windows.Forms.Button btn_addition;
  321.         private System.Windows.Forms.Button btn_dot;
  322.         private System.Windows.Forms.Button btn_zero;
  323.         private System.Windows.Forms.Button btn_equal;
  324.         private System.Windows.Forms.Button btn_clearall;
  325.         private System.Windows.Forms.Button btn_clear;
  326.         private System.Windows.Forms.TextBox tbox_result;
  327.         private System.Windows.Forms.Label lbl_display;
  328.     }
  329. }
or also you create the layout by dragging the designating tools to the forms.

Creating the C# script

This code contains the function of the application. This code will will display some number and equation every time the buttons is clicked. To do that locate the csharp script, In my case the default script will be called Form1.cs. Then write these block of codes inside the Class of the form called Form1.
  1.   Double value = 0;
  2.         String operation = "";
  3.         bool operation_pressed = false;
  4.  
  5.         public Form1()
  6.         {
  7.             InitializeComponent();
  8.         }
  9.  
  10.         private void btn_one_Click(object sender, EventArgs e)
  11.         {
  12.             if (tbox_result.Text == "0" || operation_pressed) {
  13.                 tbox_result.Clear();
  14.             }
  15.             operation_pressed = false;
  16.             tbox_result.Text += "1";
  17.         }
  18.  
  19.         private void btn_two_Click(object sender, EventArgs e)
  20.         {
  21.             if (tbox_result.Text == "0" || operation_pressed)
  22.             {
  23.                 tbox_result.Clear();
  24.             }
  25.             operation_pressed = false;
  26.             tbox_result.Text += "2";
  27.         }
  28.  
  29.         private void btn_three_Click(object sender, EventArgs e)
  30.         {
  31.             if (tbox_result.Text == "0" || operation_pressed)
  32.             {
  33.                 tbox_result.Clear();
  34.             }
  35.             operation_pressed = false;
  36.             tbox_result.Text += "3";
  37.         }
  38.  
  39.         private void btn_four_Click(object sender, EventArgs e)
  40.         {
  41.             if (tbox_result.Text == "0" || operation_pressed)
  42.             {
  43.                 tbox_result.Clear();
  44.             }
  45.             operation_pressed = false;
  46.             tbox_result.Text += "4";
  47.         }
  48.  
  49.         private void btn_five_Click(object sender, EventArgs e)
  50.         {
  51.             if (tbox_result.Text == "0" || operation_pressed)
  52.             {
  53.                 tbox_result.Clear();
  54.             }
  55.             operation_pressed = false;
  56.             tbox_result.Text += "5";
  57.         }
  58.  
  59.         private void btn_six_Click(object sender, EventArgs e)
  60.         {
  61.             if (tbox_result.Text == "0" || operation_pressed)
  62.             {
  63.                 tbox_result.Clear();
  64.             }
  65.             operation_pressed = false;
  66.             tbox_result.Text += "6";
  67.         }
  68.  
  69.         private void btn_seven_Click(object sender, EventArgs e)
  70.         {
  71.             if (tbox_result.Text == "0" || operation_pressed)
  72.             {
  73.                 tbox_result.Clear();
  74.             }
  75.             operation_pressed = false;
  76.             tbox_result.Text += "7";
  77.         }
  78.  
  79.         private void btn_eight_Click(object sender, EventArgs e)
  80.         {
  81.             if (tbox_result.Text == "0" || operation_pressed)
  82.             {
  83.                 tbox_result.Clear();
  84.             }
  85.             operation_pressed = false;
  86.             tbox_result.Text += "8";
  87.         }
  88.  
  89.         private void btn_nine_Click(object sender, EventArgs e)
  90.         {
  91.             if (tbox_result.Text == "0" || operation_pressed)
  92.             {
  93.                 tbox_result.Clear();
  94.             }
  95.             operation_pressed = false;
  96.             tbox_result.Text += "9";
  97.         }
  98.  
  99.         private void btn_zero_Click(object sender, EventArgs e)
  100.         {
  101.             if (tbox_result.Text == "0" || operation_pressed)
  102.             {
  103.                 tbox_result.Clear();
  104.             }
  105.             operation_pressed = false;
  106.             tbox_result.Text += "0";
  107.         }
  108.  
  109.         private void btn_clearall_Click(object sender, EventArgs e)
  110.         {
  111.             tbox_result.Text = "0";
  112.             lbl_display.Text = "";
  113.         }
  114.  
  115.         private void btn_addition_Click(object sender, EventArgs e)
  116.         {
  117.             operation = "+";
  118.             value = Double.Parse(tbox_result.Text);
  119.             operation_pressed = true;
  120.             lbl_display.Text = value + " " + operation;
  121.         }
  122.  
  123.         private void btn_subtract_Click(object sender, EventArgs e)
  124.         {
  125.             operation = "-";
  126.             value = Double.Parse(tbox_result.Text);
  127.             operation_pressed = true;
  128.             lbl_display.Text = value + " " + operation;
  129.         }
  130.  
  131.         private void btn_multiply_Click(object sender, EventArgs e)
  132.         {
  133.             operation = "*";
  134.             value = Double.Parse(tbox_result.Text);
  135.             operation_pressed = true;
  136.             lbl_display.Text = value + " " + operation;
  137.         }
  138.  
  139.         private void btn_divide_Click(object sender, EventArgs e)
  140.         {
  141.             operation = "/";
  142.             value = Double.Parse(tbox_result.Text);
  143.             operation_pressed = true;
  144.             lbl_display.Text = value + " " + operation;
  145.         }
  146.  
  147.         private void btn_equal_Click(object sender, EventArgs e)
  148.         {
  149.             lbl_display.Text = "";
  150.  
  151.             if (operation != "") {
  152.                 switch (operation)
  153.                 {
  154.                     case "+":
  155.                         tbox_result.Text = (value + Double.Parse(tbox_result.Text)).ToString();
  156.                         break;
  157.                     case "-":
  158.                         tbox_result.Text = (value - Double.Parse(tbox_result.Text)).ToString();
  159.                         break;
  160.                     case "*":
  161.                         tbox_result.Text = (value * Double.Parse(tbox_result.Text)).ToString();
  162.                         break;
  163.                     case "/":
  164.                         tbox_result.Text = (value / Double.Parse(tbox_result.Text)).ToString();
  165.                         break;
  166.  
  167.                     default:
  168.                         break;
  169.                 }
  170.             }
  171.             operation = "";
  172.         }
  173.  
  174.         private void btn_clear_Click(object sender, EventArgs e)
  175.         {
  176.             if (tbox_result.TextLength > 1)
  177.             {
  178.                 tbox_result.Text = tbox_result.Text.Substring(0, tbox_result.TextLength - 1);
  179.             }
  180.             else {
  181.                 tbox_result.Text = "0";
  182.             }
  183.          
  184.         }
Try to run the application and see if it works. There you go we successfully create a Simple Calculator using C#. I hope that this tutorial help you understand on how to develop an application using C#. For more updates and tutorials just kindly visit this site. Enjoy Coding!!!

Add new comment