• (4 Pages)
  • +
  • 1
  • 2
  • 3
  • 4

Basic Calculator in C#

#16 Irish18  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 42
  • Joined: 15-June 09

Posted 24 June 2009 - 09:01 AM

Now that I've finished my Calculator, its not putting anything in the input box when i press the buttons.

I'm pressing Ctrl+F5 to run it and its coming up, with all the buttons in place but nothing happens when i press them.
Positive ive followed the code right to the letter.
But ive probably made another rookie mistake:


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

		// variables to hold operands
		private double valHolder1;
		private double valHolder2;

		// variable to hold temporary values
		private double tmpValue;

		// True if '.' is use else false
		private bool hasDecimal = false;
		private bool inputStatus = true;

		// variable to hold Operator
		private string calcFunc;

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

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

		//Due to a mix up when creating the form, button4 is actually number 2
		private void button4_Click(object sender, System.EventArgs e)
		{
			//Check the inputStatus
			if (inputStatus)
			{
				//Its True
				//Append values to the value
				//in the input box
				textBox1.Text += button4.Text;
			}
			else
			{
				//Value is False
				//Set the value to the value of the button
				textBox1.Text = button4.Text;
				//Toggle inputStatus to True
				inputStatus = true;
			}
		}

		//and button6 is nimber 3
		private void button6_Click(object sender, System.EventArgs e)
		{
			//Check the inputStatus
			if (inputStatus)
			{
				//Its True
				//Append values to the value
				//in the input box
				textBox1.Text += button6.Text;
			}
			else
			{
				//Value is False
				//Set the value to the value of the button
				textBox1.Text = button6.Text;
				//Toggle inputStatus to True
				inputStatus = true;
			}
		}

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

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

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

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

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


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

		// The number zero has slightly different code to the other 
		// 9 numbers as we don't want to be able to write a zero as
		// the first number.
		private void button10_Click(object sender, System.EventArgs e)
		{
			//Check the input Status
			if (inputStatus)
			{
				//If True
				//Now check to make sure the
				//input Box has a value
				if (textBox1.Text.Length >= 1)
				{
					//Add our zero
					textBox1.Text += button10.Text;
				}
			}
		}

		private void button11_Click(object sender, System.EventArgs e)
		{
			//Check the input status
			if (inputStatus)
			{
				//Check that it already has a decimal, if it does: do nothing.
				if (!hasDecimal)
				{
					//Check to make sure the length is > 1
					//Dont want user to add decimal as first charachter
					if (textBox1.Text.Length != 0)
					{
						//make sure zero isnt the first number
						if (textBox1.Text != "0")
						{
							//It met all requirements so add the zero
							textBox1.Text += button11.Text;
							//Toggle the flag to true (only one decimal per calculation)
							hasDecimal = true;
						}
					}
					else
					{
						//Since the length isnt > 1
						//make the text = 0.
						textBox1.Text = "0.";
					}
				}
			}
		}

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

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

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

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

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

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

		private void button15_Click(object sender, System.EventArgs e)
		{
			//declare locals needed
			string str;
			int loc;
			//Make sure the input has a value
			if (textBox1.Text.Length != 0)
			{
				//get the next to last charachter
				str = textBox1.Text.Substring(textBox1.Text.Length - 1);
				// Check if it has a decimal
				if (str == ".")
				{
					//Toggle the hasDecimal flag
					hasDecimal = false;
				}
				//Get the length of the string
				loc = textBox1.Text.Length;
				//Remove the last charachter of the string
				textBox1.Text = textBox1.Text.Remove(loc - 1, 1);
			}
		}

		private void button13_Click(object sender, System.EventArgs e)
		{
			//Empty the input box
			textBox1.Text = string.Empty;
			//Toggle the deciaml flag
			hasDecimal = false;
		}

		private void button14_Click(object sender, System.EventArgs e)
		{
			//clear the input box
			textBox1.Text = string.Empty;
			//Set the valHolders to 0
			valHolder1 = 0;
			valHolder2 = 0;
			//Set the calcFunc switch to empty
			calcFunc = string.Empty;
			//Toggle the decimal flag;
			hasDecimal = false;
		}

		private void CalculateTotals()
		{
			valHolder2 = System.Double.Parse(textBox1.Text);
			//determine which calculation to execute
			//by checking the value of calcFunc
			switch (calcFunc)
			{
				//addition
				case "Add":
					valHolder1 = valHolder1 + valHolder2;
					break;

				//subtraction
				case "Subtract":
					valHolder1 = valHolder1 - valHolder2;
					break;

				//multiply
				case "Multiply":
					valHolder1 = valHolder1 * valHolder2;
					break;

				//division
				case "Divide":
					valHolder1 = valHolder1 / valHolder2;
					break;

				//Power of 
				case "PowerOf":
					valHolder1 = System.Math.Pow(valHolder1, valHolder2);
					break;
			}
			//set our input area to the value of the calculation
			textBox1.Text = valHolder1.ToString();
			inputStatus = false;
		}
	}
}




Gyah.
Was This Post Helpful? 0
  • +
  • -

#17 pareidolia  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 38
  • Joined: 17-September 09

Posted 25 September 2009 - 11:46 PM

Just wanted to let you know that the code for the square root button is still in Visual Basic. I'm sure people can figure it out but thought maybe you would want to fix it.

Edit: Kept reading and saw it below it. Shouldn't have jumped to conclusions. :P

This post has been edited by pareidolia: 25 September 2009 - 11:53 PM

Was This Post Helpful? 0
  • +
  • -

#18 Kratos61  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 31-October 09

Posted 31 October 2009 - 08:05 PM

whenever i put in the parts of the code that include the CalculateTotals method I keep getting an error message saying " The name "CalculateTotals" does not exist in the current context" whats happening?
my code so far is :
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 Calculator
{
    public partial class frmCalculator : Form
    {
        //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;

        public frmCalculator()
        {
            InitializeComponent();
        }

   
        private void frmCalculator_Load(object sender, EventArgs e)
        {

        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show(" This is a Calculator. This calculator can do all the functions of a basic calculator and then some.", "about");
        }

        private void howToUseThisCalculatorToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Click the numbers you need and they will appear on the number bar. Zero cannot be selected as the first number. n! is a factorial. √ is a square root sign. ^ is for exponents. GFC stands for Greatest common factor. MS (Memory save) is to add a number to the memory, MR (Memory recall) is used to recall that number, M+ (Memory Addition) is used to add a number to your saved number, and MC (Memory Clear) clears the number saved in the memory.", "How to use the calculator");
        }

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

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

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

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

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

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

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

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

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

        private void btnZero_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 (txtNumberBar.Text.Length >= 1)
                {
                    //Add our zero
                    txtNumberBar.Text += btnZero.Text;

                }

            }
        }

        private void btnDecimal_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 (txtNumberBar.Text.Length != 0)
                    {
                        //Make sure 0 isnt the first number
                        if (txtNumberBar.Text != "0")
                        {
                            //It met all our requirements so add the zero
                            txtNumberBar.Text += btnDecimal.Text;
                            //Toggle the flag to true (only 1 decimal per calculation)
                            hasDecimal = true;
                        }
                    }
                    else
                    {
                        //Since the length isnt > 1
                        //make the text 0.
                        txtNumberBar.Text = "0.";
                    }

                }

            }
        }

        private void btnPlus_Click(object sender, EventArgs e)
        {
            //Make sure out input box has a value
            if (txtNumberBar.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(txtNumberBar.Text);
                    //Empty the input box
                    txtNumberBar.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;
            }

        }
        }
    }



whenever i put in the parts of the code that include the CalculateTotals method I keep getting an error message saying " The name "CalculateTotals" does not exist in the current context" whats happening?
my code so far is :
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 Calculator
{
    public partial class frmCalculator : Form
    {
        //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;

        public frmCalculator()
        {
            InitializeComponent();
        }

   
        private void frmCalculator_Load(object sender, EventArgs e)
        {

        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show(" This is a Calculator. This calculator can do all the functions of a basic calculator and then some.", "about");
        }

        private void howToUseThisCalculatorToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Click the numbers you need and they will appear on the number bar. Zero cannot be selected as the first number. n! is a factorial. √ is a square root sign. ^ is for exponents. GFC stands for Greatest common factor. MS (Memory save) is to add a number to the memory, MR (Memory recall) is used to recall that number, M+ (Memory Addition) is used to add a number to your saved number, and MC (Memory Clear) clears the number saved in the memory.", "How to use the calculator");
        }

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

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

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

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

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

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

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

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

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

        private void btnZero_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 (txtNumberBar.Text.Length >= 1)
                {
                    //Add our zero
                    txtNumberBar.Text += btnZero.Text;

                }

            }
        }

        private void btnDecimal_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 (txtNumberBar.Text.Length != 0)
                    {
                        //Make sure 0 isnt the first number
                        if (txtNumberBar.Text != "0")
                        {
                            //It met all our requirements so add the zero
                            txtNumberBar.Text += btnDecimal.Text;
                            //Toggle the flag to true (only 1 decimal per calculation)
                            hasDecimal = true;
                        }
                    }
                    else
                    {
                        //Since the length isnt > 1
                        //make the text 0.
                        txtNumberBar.Text = "0.";
                    }

                }

            }
        }

        private void btnPlus_Click(object sender, EventArgs e)
        {
            //Make sure out input box has a value
            if (txtNumberBar.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(txtNumberBar.Text);
                    //Empty the input box
                    txtNumberBar.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;
            }

        }
        }
    }

This post has been edited by JackOfAllTrades: 27 February 2010 - 08:17 AM
Reason for edit:: Added code tags

Was This Post Helpful? 0
  • +
  • -

#19 boeseskeksi  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 1
  • Joined: 03-November 09

Posted 03 November 2009 - 03:51 AM

Hi there,

i Found a little mistake :blink:

If I try to calculate -3 + -2 the result is -6 :crazy:

Can somebody help me ^^ I donīt know how to fix this

here is my code for example

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

		private double valHolder1;
		private double valHolder2;

		private double tmpValue;

		private bool hasDecimal = false;
		private bool inputStatus = true;

		private string calcFunc;



		private void btn_1_Click(object sender, EventArgs e)
		{
			if (inputStatus)
			{
				textBox1.Text += btn_1.Text;
			}
			else
			{
				textBox1.Text = btn_1.Text;
				inputStatus = true;
			}


		}

		private void btn_2_Click(object sender, EventArgs e)
		{
			if (inputStatus)
			{
				textBox1.Text += btn_2.Text;
			}
			else
			{
				textBox1.Text = btn_2.Text;
				inputStatus = true;
			}
		}

		private void btn_3_Click(object sender, EventArgs e)
		{
			if (inputStatus)
			{
				textBox1.Text += btn_3.Text;
			}
			else
			{
				textBox1.Text = btn_3.Text;
				inputStatus = true;
			}
		}

		private void btn_4_Click(object sender, EventArgs e)
		{
			if (inputStatus)
			{
				textBox1.Text += btn_4.Text;
			}
			else
			{
				textBox1.Text = btn_4.Text;
				inputStatus = true;
			}
		}

		private void btn_5_Click(object sender, EventArgs e)
		{
			if (inputStatus)
			{
				textBox1.Text += btn_5.Text;
			}
			else
			{
				textBox1.Text = btn_5.Text;
				inputStatus = true;
			}
		}

		private void btn_6_Click(object sender, EventArgs e)
		{
			if (inputStatus)
			{
				textBox1.Text += btn_6.Text;
			}
			else
			{
				textBox1.Text = btn_6.Text;
				inputStatus = true;
			}
		}

		private void btn_7_Click(object sender, EventArgs e)
		{
			if (inputStatus)
			{
				textBox1.Text += btn_7.Text;
			}
			else
			{
				textBox1.Text = btn_7.Text;
				inputStatus = true;
			}

		}

		private void btn_8_Click(object sender, EventArgs e)
		{
			if (inputStatus)
			{
				textBox1.Text += btn_8.Text;
			}
			else
			{
				textBox1.Text = btn_8.Text;
				inputStatus = true;
			}
		}

		private void btn_9_Click(object sender, EventArgs e)
		{
			if (inputStatus)
			{
				textBox1.Text += btn_9.Text;
			}
			else
			{
				textBox1.Text = btn_9.Text;
				inputStatus = true;
			}
		}

		private void btn_0_Click(object sender, EventArgs e)
		{
			if (inputStatus)
			{
				if (textBox1.Text.Length <= 1)
				{
					textBox1.Text += btn_0.Text;
				}
			}
		}

		private void btn_plus_Click(object sender, EventArgs e)
		{
			if (textBox1.Text.Length != 0)
			{
				if (calcFunc == string.Empty)
				{
					valHolder1 = System.Double.Parse(textBox1.Text);
					textBox1.Text = String.Empty;
				}
				else
				{

					CalculateTotals();
				}
				calcFunc = "Add";
				hasDecimal = false;
			}

		}

		private void btn_minus_Click(object sender, EventArgs e)
		{
			if (textBox1.Text.Length != 0)
			{
				if (calcFunc == String.Empty)
				{
					valHolder1 = System.Double.Parse(textBox1.Text);
					textBox1.Text = string.Empty;
				}
				else
				{
					CalculateTotals();
				}
				calcFunc = "Subtract";
				hasDecimal = false;
			}

		}

		private void btn_ex_Click(object sender, EventArgs e)
		{
			if (textBox1.Text.Length != 0)
			{
				if (calcFunc == string.Empty)
				{
					valHolder1 = System.Double.Parse(textBox1.Text);
					textBox1.Text = String.Empty;
				}
				else
				{
					CalculateTotals();
				}
				calcFunc = "PowerOf";
				hasDecimal = false;
			}
		}

		private void btn_sqrt_Click(object sender, EventArgs e)
		{
			if (textBox1.Text.Length != 0)
			{
				tmpValue = System.Double.Parse(textBox1.Text);

				tmpValue = System.Math.Sqrt(tmpValue);
				textBox1.Text = tmpValue.ToString();
				hasDecimal = false;
			}
		}

		private void back_Click(object sender, EventArgs e)
		{
			string str;
			int loc;

			if (textBox1.Text.Length > 0)
			{
				str = textBox1.Text.Substring(textBox1.Text.Length - 1);
				if (str == ".")
				{
					hasDecimal = false;
				}
				loc = textBox1.Text.Length;
				textBox1.Text = textBox1.Text.Remove(loc - 1, 1);

			}
		}

		private void btn_ce_Click(object sender, EventArgs e)
		{
			textBox1.Text = string.Empty;
			hasDecimal = false;


		}

		private void btn_c_Click(object sender, EventArgs e)
		{
			textBox1.Text = string.Empty;

			valHolder1 = 0;
			valHolder2 = 0;
			calcFunc = string.Empty;
			hasDecimal = false;

		}

		private void btn_divi_Click(object sender, EventArgs e)
		{
			if (textBox1.Text.Length != 0)
			{
				if (calcFunc == string.Empty)
					valHolder1 = System.Double.Parse(textBox1.Text);
				textBox1.Text = string.Empty;
			}
			else
			{
				CalculateTotals();
			}
			calcFunc = "Divide";
			hasDecimal = false;


		}

		private void btn_multi_Click(object sender, EventArgs e)
		{
			if (textBox1.Text.Length != 0)
			{
				if (calcFunc == string.Empty)
				{
					valHolder1 = System.Double.Parse(textBox1.Text);
					textBox1.Text = string.Empty;
				}
				else
				{
					CalculateTotals();
				}
				calcFunc = "Multiply";
				hasDecimal = false;
			}
		}

		private void btn_kehr_Click(object sender, EventArgs e)
		{
			if (textBox1.Text.Length != 0)
			{
				tmpValue = System.Double.Parse(textBox1.Text);
				tmpValue = 1 / tmpValue;
				textBox1.Text = tmpValue.ToString();
				hasDecimal = false;
			}
		}

		private void btn_plusminus_Click(object sender, EventArgs e)
		{
			if (inputStatus)
			{
				if (textBox1.Text.Length > 0)
				{
					valHolder1 = -1 * System.Double.Parse(textBox1.Text);
					textBox1.Text = valHolder1.ToString();
				}

			}





		}

		private void btn_equals_Click(object sender, EventArgs e)
		{
			if (textBox1.Text.Length != 0 && valHolder1 != 0)
			{
				CalculateTotals();
				calcFunc = String.Empty;
				hasDecimal = false;

			}



		}

		private void btn_komma_Click(object sender, EventArgs e)
		{
			if (inputStatus)
			{
				if (!hasDecimal)
				{
					if (textBox1.Text.Length != 0)
					{
						if (textBox1.Text != "0")
						{
							textBox1.Text +=  btn_komma.Text;
							hasDecimal = true;
						}
				   
					}
					else
					{
						textBox1.Text = "0,";
					}
				}
			}
	   
	   }
		private void CalculateTotals()
		{
			valHolder2 = System.Double.Parse(textBox1.Text);
			switch (calcFunc)
			{
				case "Add":
					valHolder1 = valHolder1 + valHolder2;

					break;

				case "Subtract":

					valHolder1 = valHolder1 - valHolder2;

					break;
				case "Divide":
					valHolder1 = valHolder1 / valHolder2;

					break;

				case "Multiply":

					valHolder1 = valHolder1 * valHolder2;

					break;

				case "PowerOf":

					valHolder1 = System.Math.Pow(valHolder1, valHolder2);
					break;
			}
			textBox1.Text = valHolder1.ToString();
			inputStatus = false;
		}
	}
}
	

Was This Post Helpful? 1
  • +
  • -

#20 Guest_Steve*


Reputation:

Posted 14 February 2010 - 11:11 PM

boeseskeksi,

Mine did the same thing. I fixed it by replacing the code for cmdSign_Click with the code below:

private void cmdSign_Click(object sender, System.EventArgs e)
        {
            //Check the input status
            if (inputStatus)
            {
                //Check to make sure theres a value in the input box
                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();
                }
            }
        }


Good luck.
Was This Post Helpful? 0

#21 bobbfwed  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 22-February 10

Posted 22 February 2010 - 11:12 AM

Is the second part of the tutorial posted somewhere? I am new to C# and created my own calculator much like this one. I am looking for "Entering numbers by typing". Maybe someone can point me a direction to implement that feature.

For input on this calculator, I did a lot of things different in my calculator (most negligible changes), but one good thing to get in the habit of doing is using constants when possible. Instead of exhausting more memory declaring strings for the "commands" create constants with integer numbers. This is also much faster to compare an integer than a string.

        const int Add = 1;
        const int Subtract = 2;
        const int Multiply = 3;
        const int Divide = 4;
        const int Power = 5;

Instead of 'calcFunc = "Add";' use 'calcFunc = Add;'
and at the actual action part:
                switch (calcFunc)
                {
                    case Add:
                        ...
                    case Subtract:
                        ...
                    case Multiply:
                        ...
                    case Divide:
                        ...
                    case Power:
                        ...
                }

Was This Post Helpful? 0
  • +
  • -

#22 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6036
  • View blog
  • Posts: 23,421
  • Joined: 23-August 08

Posted 22 February 2010 - 05:35 PM

View Postbobbfwed, on 22 February 2010 - 12:12 PM, said:

Is the second part of the tutorial posted somewhere? I am new to C# and created my own calculator much like this one. I am looking for "Entering numbers by typing". Maybe someone can point me a direction to implement that feature.


Try checking out this at MSDN: http://msdn.microsof...y/ms171538.aspx
Was This Post Helpful? 0
  • +
  • -

#23 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1638
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Posted 22 February 2010 - 05:37 PM

bobbfwed There is no 2nd part. This is a tutorial covering making a basic calculator, it doesn't cover any more advanced features as what you're looking for. Looks like I may need to do a writeup on a more advanced one now :)
Was This Post Helpful? 0
  • +
  • -

#24 Fungle  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 256
  • View blog
  • Posts: 337
  • Joined: 06-March 10

Posted 08 March 2010 - 10:31 AM

Hey,

I have to say this is a monumental help to me, I have very little C# Experience and this one tutorial has taught me so much about how the language works.

Also, I'm not too sure why but I don't think I have the System.Math Library as it doesn't seem to pick up the System.Math Square root or any other libraries.

I'm using Visual Studio 2008 with the MSDN Library.

Once again thank you so much for this tutorial, it helped me so much.

- Harry
Was This Post Helpful? 0
  • +
  • -

#25 Shane Hudson  Icon User is offline

  • D.I.C Technophile
  • member icon

Reputation: 343
  • View blog
  • Posts: 1,286
  • Joined: 06-December 09

Posted 08 March 2010 - 11:24 AM

View PostFungle, on 08 March 2010 - 04:31 PM, said:

Hey,

I have to say this is a monumental help to me, I have very little C# Experience and this one tutorial has taught me so much about how the language works.

Also, I'm not too sure why but I don't think I have the System.Math Library as it doesn't seem to pick up the System.Math Square root or any other libraries.

I'm using Visual Studio 2008 with the MSDN Library.

Once again thank you so much for this tutorial, it helped me so much.

- Harry


Hey Harry,

If you look to the top of your code you should see some imports. In fact, in c# these are using.

For this you would just type:

using System.Math;



If it does not work... then we shall have to have a closer look!
Was This Post Helpful? 0
  • +
  • -

#26 Fungle  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 256
  • View blog
  • Posts: 337
  • Joined: 06-March 10

Posted 09 March 2010 - 03:01 AM

Quote

Hey Harry,

If you look to the top of your code you should see some imports. In fact, in c# these are using.

For this you would just type:

using System.Math;



If it does not work... then we shall have to have a closer look!


I am aware of that and I did type in using System.Math but it doesn't have any sub-libaries or detect System.Math.

I will try again later on.

This post has been edited by Fungle: 09 March 2010 - 06:44 AM

Was This Post Helpful? 0
  • +
  • -

#27 Core  Icon User is offline

  • using System.Linq;
  • member icon

Reputation: 774
  • View blog
  • Posts: 5,097
  • Joined: 08-December 08

Posted 09 March 2010 - 06:54 AM

Math is not a namespace, it is a member of the System namespace.

Therefore, whenever you need to perform a mathematical operation, you would use Math.<function name> in the code.
Was This Post Helpful? 1
  • +
  • -

#28 Fungle  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 256
  • View blog
  • Posts: 337
  • Joined: 06-March 10

Posted 09 March 2010 - 11:36 AM

View PostCore, on 09 March 2010 - 05:54 AM, said:

Math is not a namespace, it is a member of the System namespace.

Therefore, whenever you need to perform a mathematical operation, you would use Math.<function name> in the code.


Thank you for that, I understand now.
Was This Post Helpful? 0
  • +
  • -

#29 Guest_ZubZer0*


Reputation:

Posted 27 March 2010 - 01:46 PM

View PostPsychoCoder, on 09 September 2007 - 07:36 PM, said:

...[LOTS AND LOTS OF TEXT]...
Thats it, thats how you create a basic calculator in VB.Net. I hope you find this tutorial helpful.
...[NOT QUITE AS MUCH OF TEXT]...


You don't feel like using C# in the end of the tutorial? :P
Was This Post Helpful? 0

#30 bobbfwed  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 22-February 10

Posted 31 March 2010 - 12:36 PM

View PostJackOfAllTrades, on 22 February 2010 - 04:35 PM, said:

View Postbobbfwed, on 22 February 2010 - 12:12 PM, said:

Is the second part of the tutorial posted somewhere? I am new to C# and created my own calculator much like this one. I am looking for "Entering numbers by typing". Maybe someone can point me a direction to implement that feature.


Try checking out this at MSDN: http://msdn.microsof...y/ms171538.aspx


I know its been a while...but here is a reply (I didn't check back at this forum either...which would have made my life easier):

I ended up figuring this out. I had everything (the event handlers, etc.) right, I just didn't know I had to do the keypreview thing. I eventually saw that in the properties pane and all was well.
Was This Post Helpful? 0
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • 4