Click here to Skip to main content
15,867,686 members
Articles / Programming Languages / C# 4.0
Article

A Calculator via Visual C

Rate me:
Please Sign up or sign in to vote.
1.79/5 (9 votes)
13 May 2010CPOL2 min read 28.4K   5   9
An Article the Demonstrates How to Build a Windows Forms Calculator

A Calculator via Visual C#

Preface

The .NET Framework provides a long list of Math APIs for calculation purposes. Our goal now is to build a calculator using some basic math operations and some of those APIs. Normally when working with Windows Forms we use a button to fire an event that warrants an event handler. But how do use the Button control to enter numeric character input into a TextBox control, and then further use other buttons to perform operations needed on the entered input? First off, there are 10, or 0 – 9 digits, that must have their  text properties set to those integers to appear on the buttons. The other buttons need to have their text properties set to the operation in which they are to perform when the user clicks them. With all of those buttons, we should group them. This means that we need to use two GroupBox controls to contain the input field and the Button controls. We then need to declare some local variables, particularly to indicate the status of an operation. Sounds like a Boolean data type. We then need to write a method that calculates the total of the particular operation performed. Because there will be several different types of cases at hand, we’ll need to use the switch statement.

Examine this code that compiles as a console application:

using System;
public class Program
 {  
   public static void Main()
   {
    Console.WriteLine("Abs({0}) = {1}", -55, Math.Abs(-55));
    Console.WriteLine("Ceiling({0}) = {1}", 55.3, Math.Ceiling(55.3));
    Console.WriteLine("Pow({0},{1}) = {2}", 2, 16, Math.Pow(2, 16));
    Console.WriteLine("Round({0},{1}) = {2}",
    10.55358, 2, Math.Round(10.55358, 2));
    Console.WriteLine("Sin({0}) = {1}", 323.333, Math.Sin(323.333));
    Console.WriteLine("Cos({0}) = {1}", 323.333, Math.Cos(323.333));
    Console.WriteLine("Tan({0}) = {1}", 323.333, Math.Tan(323.333));
    Console.WriteLine("Sqrt({0}) = {1}", 65536, Math.Sqrt(65536));
      }
     }

Here we are using the .NET Framework Math APIs directly. It is obviously very simple to type in an API and plug in a value. Here is the result:

Abs(-55) = 55
Ceiling(55.3) = 56
Pow(2,16) = 65536
Round(10.55358,2) = 10.55
Sin(323.333) = 0.248414709883854
Cos(323.333) = -0.968653772982546
Tan(323.333) = -0.256453561440193
Sqrt(65536) = 256

Now if choose to get the square root of 65,536 (2 to the 16th power), we can just press a button:

                Capture.PNG   

I used this example because there are two possible values for a data bit: 1 or a 0. There are 8 bits in a byte. 2 raised to the 8th power = 256, which is 16 squared. 65,536 is equal to one segment of memory. Anyway, here is the code for the calculator. Try and expand on it by adding some trigonometric functions. That is the purpose of this article. This code is not split into partial classes. The downloadable file is, however. Unzip it the solution container into a newly made folder in your Projects directory, and then double-click the solution file.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;



    public  class Form1 : Form
    {
      private System.Windows.Forms.GroupBox groupBox1;
        private System.Windows.Forms.TextBox txtInput;
        private System.Windows.Forms.GroupBox groupBox2;
        private System.Windows.Forms.Button button23;
        private System.Windows.Forms.Button button22;
        private System.Windows.Forms.Button button21;
        private System.Windows.Forms.Button button20;
        private System.Windows.Forms.Button button19;
        private System.Windows.Forms.Button button18;
        private System.Windows.Forms.Button button17;
        private System.Windows.Forms.Button button16;
        private System.Windows.Forms.Button button15;
        private System.Windows.Forms.Button button14;
        private System.Windows.Forms.Button button13;
        private System.Windows.Forms.Button button12;
        private System.Windows.Forms.Button button11;
        private System.Windows.Forms.Button button10;
        private System.Windows.Forms.Button button9;
        private System.Windows.Forms.Button button8;
        private System.Windows.Forms.Button button7;
        private System.Windows.Forms.Button button6;
        private System.Windows.Forms.Button button5;
        private System.Windows.Forms.Button button4;
        private System.Windows.Forms.Button button3;
        private System.Windows.Forms.Button button2;
        private System.Windows.Forms.Button button1;

         public Form1()
        {
            InitializeComponent();
        }
        //variables to hold operands
        private double valHolder1;
        private double valHolder2;
        //Varible to hold temporary values
        private double tmpValue;
        //True if "." is use else false
        private bool hasDecimal = false;
        private bool inputStatus = true;
        //variable to hold Operater
        private string calcFunc;

     private System.ComponentModel.IContainer components = null;

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

        

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.txtInput = new System.Windows.Forms.TextBox();
            this.groupBox2 = new System.Windows.Forms.GroupBox();
            this.button23 = new System.Windows.Forms.Button();
            this.button22 = new System.Windows.Forms.Button();
            this.button21 = new System.Windows.Forms.Button();
            this.button20 = new System.Windows.Forms.Button();
            this.button19 = new System.Windows.Forms.Button();
            this.button18 = new System.Windows.Forms.Button();
            this.button17 = new System.Windows.Forms.Button();
            this.button16 = new System.Windows.Forms.Button();
            this.button15 = new System.Windows.Forms.Button();
            this.button14 = new System.Windows.Forms.Button();
            this.button13 = new System.Windows.Forms.Button();
            this.button12 = new System.Windows.Forms.Button();
            this.button11 = new System.Windows.Forms.Button();
            this.button10 = new System.Windows.Forms.Button();
            this.button9 = new System.Windows.Forms.Button();
            this.button8 = new System.Windows.Forms.Button();
            this.button7 = new System.Windows.Forms.Button();
            this.button6 = new System.Windows.Forms.Button();
            this.button5 = new System.Windows.Forms.Button();
            this.button4 = new System.Windows.Forms.Button();
            this.button3 = new System.Windows.Forms.Button();
            this.button2 = new System.Windows.Forms.Button();
            this.button1 = new System.Windows.Forms.Button();
            this.groupBox1.SuspendLayout();
            this.groupBox2.SuspendLayout();
            this.SuspendLayout();
            // 
            // groupBox1
            // 
            this.groupBox1.Controls.Add(this.txtInput);
            this.groupBox1.Location = new System.Drawing.Point(39, 28);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(274, 34);
            this.groupBox1.TabIndex = 0;
            this.groupBox1.TabStop = false;
            // 
            // txtInput
            // 
            this.txtInput.Location = new System.Drawing.Point(7, 8);
            this.txtInput.Name = "txtInput";
            this.txtInput.Size = new System.Drawing.Size(261, 20);
            this.txtInput.TabIndex = 0;
            // 
            // groupBox2
            // 
            this.groupBox2.Controls.Add(this.button23);
            this.groupBox2.Controls.Add(this.button22);
            this.groupBox2.Controls.Add(this.button21);
            this.groupBox2.Controls.Add(this.button20);
            this.groupBox2.Controls.Add(this.button19);
            this.groupBox2.Controls.Add(this.button18);
            this.groupBox2.Controls.Add(this.button17);
            this.groupBox2.Controls.Add(this.button16);
            this.groupBox2.Controls.Add(this.button15);
            this.groupBox2.Controls.Add(this.button14);
            this.groupBox2.Controls.Add(this.button13);
            this.groupBox2.Controls.Add(this.button12);
            this.groupBox2.Controls.Add(this.button11);
            this.groupBox2.Controls.Add(this.button10);
            this.groupBox2.Controls.Add(this.button9);
            this.groupBox2.Controls.Add(this.button8);
            this.groupBox2.Controls.Add(this.button7);
            this.groupBox2.Controls.Add(this.button6);
            this.groupBox2.Controls.Add(this.button5);
            this.groupBox2.Controls.Add(this.button4);
            this.groupBox2.Controls.Add(this.button3);
            this.groupBox2.Controls.Add(this.button2);
            this.groupBox2.Controls.Add(this.button1);
            this.groupBox2.Location = new System.Drawing.Point(39, 80);
            this.groupBox2.Name = "groupBox2";
            this.groupBox2.Size = new System.Drawing.Size(274, 223);
            this.groupBox2.TabIndex = 1;
            this.groupBox2.TabStop = false;
            // 
            // button23
            // 
            this.button23.Location = new System.Drawing.Point(203, 186);
            this.button23.Name = "button23";
            this.button23.Size = new System.Drawing.Size(40, 23);
            this.button23.TabIndex = 22;
            this.button23.Text = "=";
            this.button23.UseVisualStyleBackColor = true;
            this.button23.Click += new System.EventHandler(this.button23_Click);
            // 
            // button22
            // 
            this.button22.Location = new System.Drawing.Point(157, 186);
            this.button22.Name = "button22";
            this.button22.Size = new System.Drawing.Size(39, 23);
            this.button22.TabIndex = 21;
            this.button22.Text = "+";
            this.button22.UseVisualStyleBackColor = true;
            this.button22.Click += new System.EventHandler(this.button22_Click);
            // 
            // button21
            // 
            this.button21.Location = new System.Drawing.Point(107, 186);
            this.button21.Name = "button21";
            this.button21.Size = new System.Drawing.Size(44, 23);
            this.button21.TabIndex = 20;
            this.button21.Text = ".";
            this.button21.UseVisualStyleBackColor = true;
            this.button21.Click += new System.EventHandler(this.button21_Click);
            // 
            // button20
            // 
            this.button20.Location = new System.Drawing.Point(57, 187);
            this.button20.Name = "button20";
            this.button20.Size = new System.Drawing.Size(43, 23);
            this.button20.TabIndex = 19;
            this.button20.Text = "+/-";
            this.button20.UseVisualStyleBackColor = true;
            this.button20.Click += new System.EventHandler(this.button20_Click);
            // 
            // button19
            // 
            this.button19.Location = new System.Drawing.Point(7, 187);
            this.button19.Name = "button19";
            this.button19.Size = new System.Drawing.Size(44, 23);
            this.button19.TabIndex = 18;
            this.button19.Text = "0";
            this.button19.UseVisualStyleBackColor = true;
            this.button19.Click += new System.EventHandler(this.button19_Click);
            // 
            // button18
            // 
            this.button18.Location = new System.Drawing.Point(203, 145);
            this.button18.Name = "button18";
            this.button18.Size = new System.Drawing.Size(40, 23);
            this.button18.TabIndex = 17;
            this.button18.Text = "1/x";
            this.button18.UseVisualStyleBackColor = true;
            this.button18.Click += new System.EventHandler(this.button18_Click);
            // 
            // button17
            // 
            this.button17.Location = new System.Drawing.Point(157, 145);
            this.button17.Name = "button17";
            this.button17.Size = new System.Drawing.Size(39, 23);
            this.button17.TabIndex = 16;
            this.button17.Text = "-";
            this.button17.UseVisualStyleBackColor = true;
            this.button17.Click += new System.EventHandler(this.button17_Click);
            // 
            // button16
            // 
            this.button16.Location = new System.Drawing.Point(107, 145);
            this.button16.Name = "button16";
            this.button16.Size = new System.Drawing.Size(44, 23);
            this.button16.TabIndex = 15;
            this.button16.Text = "3";
            this.button16.UseVisualStyleBackColor = true;
            this.button16.Click += new System.EventHandler(this.button16_Click);
            // 
            // button15
            // 
            this.button15.Location = new System.Drawing.Point(58, 145);
            this.button15.Name = "button15";
            this.button15.Size = new System.Drawing.Size(42, 23);
            this.button15.TabIndex = 14;
            this.button15.Text = "2";
            this.button15.UseVisualStyleBackColor = true;
            this.button15.Click += new System.EventHandler(this.button15_Click);
            // 
            // button14
            // 
            this.button14.Location = new System.Drawing.Point(7, 145);
            this.button14.Name = "button14";
            this.button14.Size = new System.Drawing.Size(44, 23);
            this.button14.TabIndex = 13;
            this.button14.Text = "1";
            this.button14.UseVisualStyleBackColor = true;
            this.button14.Click += new System.EventHandler(this.button14_Click);
            // 
            // button13
            // 
            this.button13.Location = new System.Drawing.Point(203, 101);
            this.button13.Name = "button13";
            this.button13.Size = new System.Drawing.Size(40, 23);
            this.button13.TabIndex = 12;
            this.button13.Text = "Sqrt";
            this.button13.UseVisualStyleBackColor = true;
            this.button13.Click += new System.EventHandler(this.button13_Click);
            // 
            // button12
            // 
            this.button12.Location = new System.Drawing.Point(158, 101);
            this.button12.Name = "button12";
            this.button12.Size = new System.Drawing.Size(38, 23);
            this.button12.TabIndex = 11;
            this.button12.Text = "*";
            this.button12.UseVisualStyleBackColor = true;
            this.button12.Click += new System.EventHandler(this.button12_Click);
            // 
            // button11
            // 
            this.button11.Location = new System.Drawing.Point(106, 101);
            this.button11.Name = "button11";
            this.button11.Size = new System.Drawing.Size(45, 23);
            this.button11.TabIndex = 10;
            this.button11.Text = "6";
            this.button11.UseVisualStyleBackColor = true;
            this.button11.Click += new System.EventHandler(this.button11_Click);
            // 
            // button10
            // 
            this.button10.Location = new System.Drawing.Point(58, 102);
            this.button10.Name = "button10";
            this.button10.Size = new System.Drawing.Size(42, 23);
            this.button10.TabIndex = 9;
            this.button10.Text = "5";
            this.button10.UseVisualStyleBackColor = true;
            this.button10.Click += new System.EventHandler(this.button10_Click);
            // 
            // button9
            // 
            this.button9.Location = new System.Drawing.Point(7, 102);
            this.button9.Name = "button9";
            this.button9.Size = new System.Drawing.Size(44, 23);
            this.button9.TabIndex = 8;
            this.button9.Text = "4";
            this.button9.UseVisualStyleBackColor = true;
            this.button9.Click += new System.EventHandler(this.button9_Click);
            // 
            // button8
            // 
            this.button8.Location = new System.Drawing.Point(202, 61);
            this.button8.Name = "button8";
            this.button8.Size = new System.Drawing.Size(41, 23);
            this.button8.TabIndex = 7;
            this.button8.Text = "x^";
            this.button8.UseVisualStyleBackColor = true;
            this.button8.Click += new System.EventHandler(this.button8_Click);
            // 
            // button7
            // 
            this.button7.Location = new System.Drawing.Point(157, 60);
            this.button7.Name = "button7";
            this.button7.Size = new System.Drawing.Size(39, 23);
            this.button7.TabIndex = 6;
            this.button7.Text = "/";
            this.button7.UseVisualStyleBackColor = true;
            this.button7.Click += new System.EventHandler(this.button7_Click);
            // 
            // button6
            // 
            this.button6.Location = new System.Drawing.Point(106, 61);
            this.button6.Name = "button6";
            this.button6.Size = new System.Drawing.Size(45, 23);
            this.button6.TabIndex = 5;
            this.button6.Text = "9";
            this.button6.UseVisualStyleBackColor = true;
            this.button6.Click += new System.EventHandler(this.button6_Click);
            // 
            // button5
            // 
            this.button5.Location = new System.Drawing.Point(58, 60);
            this.button5.Name = "button5";
            this.button5.Size = new System.Drawing.Size(42, 23);
            this.button5.TabIndex = 4;
            this.button5.Text = "8";
            this.button5.UseVisualStyleBackColor = true;
            this.button5.Click += new System.EventHandler(this.button5_Click);
            // 
            // button4
            // 
            this.button4.Location = new System.Drawing.Point(7, 61);
            this.button4.Name = "button4";
            this.button4.Size = new System.Drawing.Size(44, 23);
            this.button4.TabIndex = 3;
            this.button4.Text = "7";
            this.button4.UseVisualStyleBackColor = true;
            this.button4.Click += new System.EventHandler(this.button4_Click);
            // 
            // button3
            // 
            this.button3.Location = new System.Drawing.Point(190, 19);
            this.button3.Name = "button3";
            this.button3.Size = new System.Drawing.Size(53, 23);
            this.button3.TabIndex = 2;
            this.button3.Text = "C";
            this.button3.UseVisualStyleBackColor = true;
            this.button3.Click += new System.EventHandler(this.button3_Click);
            // 
            // button2
            // 
            this.button2.Location = new System.Drawing.Point(106, 19);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(57, 23);
            this.button2.TabIndex = 1;
            this.button2.Text = "CE";
            this.button2.UseVisualStyleBackColor = true;
            this.button2.Click += new System.EventHandler(this.button2_Click);
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(7, 20);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 0;
            this.button1.Text = "BackSpace";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(0)))), ((int)(((byte)(64)))));
            this.ClientSize = new System.Drawing.Size(338, 328);
            this.Controls.Add(this.groupBox2);
            this.Controls.Add(this.groupBox1);
            this.Name = "Form1";
            this.Text = "Compute in C#";
            this.groupBox1.ResumeLayout(false);
            this.groupBox1.PerformLayout();
            this.groupBox2.ResumeLayout(false);
            this.ResumeLayout(false);

        }

   [STAThread]
   static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
private void button1_Click(object sender, EventArgs e)
        {
            string str;
            int loc;
            //Make sure the text length is > 1
            if (txtInput.Text.Length > 0)
            {
                //Get the next to last character
                str = txtInput.Text.Substring(txtInput.Text.Length - 1);
                //Check if its a decimal
                if (str == ".")
                {
                    //If it is toggle the hasDecimal flag
                    hasDecimal = false;
                }
                //Get the length of the string
                loc = txtInput.Text.Length;
                //Remove the last character, incrementing by 1
                txtInput.Text = txtInput.Text.Remove(loc - 1, 1);
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            //Empty the input box
            txtInput.Text = string.Empty;
            //Toggle the decimal flag
            hasDecimal = false;

        }

        private void button3_Click(object sender, EventArgs e)
        {
            //Empty the text in the input box
            txtInput.Text = string.Empty;
            //Clear out both temp values
            valHolder1 = 0;
            valHolder2 = 0;
            //Set the calc switch to empty
            calcFunc = string.Empty;
            //Toggle the hasDecimal flag
            hasDecimal = false;

        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (inputStatus)
            {
                txtInput.Text += button4.Text;
            }
            else
            {
                txtInput.Text = button4.Text;
                inputStatus = true;
            }

        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (inputStatus)
            {
                txtInput.Text += button5.Text;
            }
            else
            {
                txtInput.Text = button5.Text;
                inputStatus = true;
            }

        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (inputStatus)
            {
                txtInput.Text += button6.Text;
            }
            else
            {
                txtInput.Text = button6.Text;
                inputStatus = true;
            }

        }

        private void CalculateTotals()
        {
            valHolder2 = System.Double.Parse(txtInput.Text);
            //determine which calculation we're going to execute
            //by checking the value of calcFunc
            switch (calcFunc)
            {
                //addition
                case "Add":
                    valHolder1 = valHolder1 + valHolder2;
                    break;
                //subtraction
                case "Subtract":
                    valHolder1 = valHolder1 - valHolder2;
                    break;
                //division
                case "Divide":
                    valHolder1 = valHolder1 / valHolder2;
                    break;
                //multiplication
                case "Multiply":
                    valHolder1 = valHolder1 * valHolder2;
                    break;
                //exponents (power of)
                case "PowerOf":
                    valHolder1 = System.Math.Pow(valHolder1, valHolder2);
                    break;
            }
            //set our input area to the value of the calculation
            txtInput.Text = valHolder1.ToString();
            inputStatus = false;
        }

        private void button7_Click(object sender, EventArgs e)
        {
            //Make sure our input box has a value
            if (txtInput.Text.Length != 0)
            {
                //Check to see if the calc func flag is empty
                if (calcFunc == string.Empty)
                {
                    //Assign the value of our input
                    //box to our holder
                    valHolder1 = System.Double.Parse(txtInput.Text);
                    //Empty the input box
                    txtInput.Text = string.Empty;
                }
                else
                {
                    //Flag isnt empty
                    //Call the calculate totals method
                    CalculateTotals();
                }
                //Assign our calc function flag
                calcFunc = "Divide";
                //Toggle the decimal flag
                hasDecimal = false;
            }

        }

        private void button8_Click(object sender, EventArgs e)
        {
            //Make sure the input box has a value
            if (txtInput.Text.Length != 0)
            {
                //Check if the calcFunc flag is empty
                if (calcFunc == string.Empty)
                {
                    //Assign the value of the input box to our variable
                    valHolder1 = System.Double.Parse(txtInput.Text);
                    //Empty the input box
                    //So the user can enter the power of value
                    txtInput.Text = string.Empty;
                }
                else
                {
                    //Call the calculate totals method
                    CalculateTotals();
                }
                //Assign our flag the value of "PowerOf"
                calcFunc = "PowerOf";
                //Reset the decimal flag
                hasDecimal = false;
            }

        }

        private void button9_Click(object sender, EventArgs e)
        {
            //Check the inputStatus
            if (inputStatus)
            {
                //Its True
                //Append values to the value
                //in the input box
                txtInput.Text += button9.Text;
            }
            else
            {
                //Value is False
                //Set the value to the value of the button
                txtInput.Text = button9.Text;
                //Toggle inputStatus to True
                inputStatus = true;
            }

        }

        private void button10_Click(object sender, EventArgs e)
        {
            if (inputStatus)
            {
                txtInput.Text += button10.Text;
            }
            else
            {
                txtInput.Text = button10.Text;
                inputStatus = true;
            }

        }

        private void button11_Click(object sender, EventArgs e)
        {
            if (inputStatus)
            {
                txtInput.Text += button11.Text;
            }
            else
            {
                txtInput.Text = button11.Text;
                inputStatus = true;
            }

        }

        private void button12_Click(object sender, EventArgs e)
        {
            if (txtInput.Text.Length != 0)
            {
                //Make sure out calc function flag is empty
                if (calcFunc == string.Empty)
                {
                    //Assign the value of the input box
                    //to our holder variable
                    valHolder1 = System.Double.Parse(txtInput.Text);
                    //Empty the input box
                    txtInput.Text = string.Empty;
                }
                else
                {
                    //Flag isnt empty
                    //Call the calculate totals method
                    CalculateTotals();
                }
                //Assign our calc function flag
                calcFunc = "Multiply";
                //Toggle the decimal flag
                hasDecimal = false;
            }

        }

        private void button13_Click(object sender, EventArgs e)
        {
            //Make sure the input box has a value
            if (txtInput.Text.Length != 0)
            {
                //Assign our variable the value in the input box
                tmpValue = System.Double.Parse(txtInput.Text);
                //Perform the square root
                tmpValue = System.Math.Sqrt(tmpValue);
                //Display the results in the input box
                txtInput.Text = tmpValue.ToString();
                //Clear the decimal flag
                hasDecimal = false;
            }
        
        }

        private void button14_Click(object sender, EventArgs e)
        {
            //Check the inputStatus
            if (inputStatus)
            {
                //Its True
                //Append values to the value
                //in the input box
                txtInput.Text += button14.Text;
            }
            else
            {
                //Value is False
                //Set the value to the value of the button
                txtInput.Text = button14.Text;
                //Toggle inputStatus to True
                inputStatus = true;
            }

        }

        private void button15_Click(object sender, EventArgs e)
        {
            //Check the inputStatus
            if (inputStatus)
            {
                //Its True
                //Append values to the value
                //in the input box
                txtInput.Text += button15.Text;
            }
            else
            {
                //Value is False
                //Set the value to the value of the button
                txtInput.Text = button15.Text;
                //Toggle inputStatus to True
                inputStatus = true;
            }

        }

        private void button16_Click(object sender, EventArgs e)
        {
            //Check the inputStatus
            if (inputStatus)
            {
                //Its True
                //Append values to the value
                //in the input box
                txtInput.Text += button16.Text;
            }
            else
            {
                //Value is False
                //Set the value to the value of the button
                txtInput.Text = button16.Text;
                //Toggle inputStatus to True
                inputStatus = true;
            }

        }

        private void button17_Click(object sender, EventArgs e)
        {
            if (txtInput.Text.Length != 0)
            {
                //Check the valueof our calculate function flag
                if (calcFunc == string.Empty)
                {
                    //Flag is empty
                    //Assign the value of our input
                    //box to our holder
                    valHolder1 = System.Double.Parse(txtInput.Text);
                    //Empty the input box
                    txtInput.Text = string.Empty;
                }
                else
                {
                    //Flag isnt empty
                    //Call our calculate totals method
                    CalculateTotals();
                }
                //assign a value to our 
                //calculate function flag
                calcFunc = "Subtract";
                //Toggle the decimal flag
                hasDecimal = false;
            }

        }

        private void button18_Click(object sender, EventArgs e)
        {
            //Make sure the input box has a value
            if (txtInput.Text.Length != 0)
            {
                //Assign the temp holder to the value of the
                //input box converted to a Double
                tmpValue = System.Double.Parse(txtInput.Text);
                //Perform a (1 divided by value calculations)
                tmpValue = 1 / tmpValue;
                //Display the results in the input box
                txtInput.Text = tmpValue.ToString();
                //Toggle the decimal flag
                hasDecimal = false;
            }

        }

        private void button19_Click(object sender, EventArgs e)
        {
            //Check the input status
            if (inputStatus)
            {
                //If true
                //Now check to make sure our
                //input box has a value
                if (txtInput.Text.Length >= 1)
                {
                    //Add our zero
                    txtInput.Text += button19.Text;
                }
            }

        }

        private void button20_Click(object sender, EventArgs e)
        {
            //Check the input status
            if (inputStatus)
            {
                //Check to make sure theres a value in the input box
                if (txtInput.Text.Length > 0)
                {
                    //Set the temp holder
                    //Its value will be (-1 times the value in the input box)
                    valHolder1 = -1 * System.Double.Parse(txtInput.Text);
                    //Display the value to the user
                    txtInput.Text = valHolder1.ToString();
                }
            }

        }

        private void button21_Click(object sender, EventArgs e)
        {
            //Check for input status (we want true)
            if (inputStatus)
            {
                //Check if it already has a decimal (if it does then do nothing)
                if (!hasDecimal)
                {
                    //Check to make sure the length is > than 1
                    //Dont want user to add decimal as first character
                    if (txtInput.Text.Length != 0)
                    {
                        //Make sure 0 isnt the first number
                        if (txtInput.Text != "0")
                        {
                            //It met all our requirements so add the zero
                            txtInput.Text += button21.Text;
                            //Toggle the flag to true (only 1 decimal per calculation)
                            hasDecimal = true;
                        }
                    }
                    else
                    {
                        //Since the length isnt > 1
                        //make the text 0.
                        txtInput.Text = "0.";
                    }
                }
            }

        }

        private void button22_Click(object sender, EventArgs e)
        {
            //Make sure out input box has a value
            if (txtInput.Text.Length != 0)
            {
                //Check the value of our function flag
                if (calcFunc == string.Empty)
                {
                    //Flag is empty
                    //Assign the value in our input
                    //box to our holder
                    valHolder1 = System.Double.Parse(txtInput.Text);
                    //Empty the input box
                    txtInput.Text = string.Empty;
                }
                else
                {
                    //Flag isnt empty
                    //Call our calculate totals method
                    CalculateTotals();
                }
                //Assign a value to our calc function flag
                calcFunc = "Add";
                //Toggle the decimal flag
                hasDecimal = false;
            }
        
        }

        private void button23_Click(object sender, EventArgs e)
        {
            if (txtInput.Text.Length != 0 && valHolder1 != 0)
            {
                //Call the calculate totals method
                CalculateTotals();
                //Clear the calcFunction value
                calcFunc = string.Empty;
                //Toggle the decimal flag
                hasDecimal = false;
            }

        }
    
}

Notice string.Empty. Here is System.Entry disassembled:

[Serializable]
internal sealed class Empty : ISerializable
{
    // Fields
    public static readonly Empty Value = new Empty();

    // Methods
    private Empty()
    {
    }

    [SecurityCritical]
    public void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        if (info == null)
        {
            throw new ArgumentNullException("info");
        }
        UnitySerializationHolder.GetUnitySerializationInfo(info, 1, null, null);
    }

    public override string ToString()
    {
        return string.Empty;
    }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Monroe Community
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 1 Pin
Pavel Vladov15-May-10 4:17
Pavel Vladov15-May-10 4:17 
GeneralMy vote of 1 Pin
Uroš Šmon14-May-10 20:01
professionalUroš Šmon14-May-10 20:01 
General! Pin
sam.hill14-May-10 18:44
sam.hill14-May-10 18:44 
GeneralMy vote of 1 Pin
luisnike1914-May-10 9:55
luisnike1914-May-10 9:55 
QuestionWhy? Pin
Matt McKinney14-May-10 9:21
Matt McKinney14-May-10 9:21 
GeneralMy vote of 1 Pin
Ajay Vijayvargiya14-May-10 9:08
Ajay Vijayvargiya14-May-10 9:08 
GeneralSad Pin
PIEBALDconsult14-May-10 5:25
mvePIEBALDconsult14-May-10 5:25 
GeneralRe: Sad Pin
luisnike1914-May-10 9:53
luisnike1914-May-10 9:53 
GeneralRe: Sad Pin
ring_015-May-10 17:36
ring_015-May-10 17:36 
Big Grin | :-D Big Grin | :-D Big Grin | :-D Big Grin | :-D Big Grin | :-D Big Grin | :-D
Man having 1 bit brain with parity error

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.