C# Calculator Help

Need help making the 0 display.

Page 1 of 1

3 Replies - 2996 Views - Last Post: 30 October 2009 - 09:59 PM Rate Topic: -----

#1 C++ Programmer  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 20
  • View blog
  • Posts: 548
  • Joined: 12-June 08

C# Calculator Help

Post icon  Posted 30 October 2009 - 08:23 PM

Allright, so with the help of a tut I have created this C# Calculator, it is very simple and I used Visual C# to build to GUI so i can't post that, but trust me, I AM ALMOST 100% POSITIVE THAT I DO NOT HAVE THE BUTTON NAMES MIXED UP! DO NOT TELL ME THIS IS THE PROBLEM!

Allright, here is my code, for some reason it doesn't show the 0's when i press the 0 button.

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
	{
		bool plus = false;
		bool minus = false;
		bool multiply = false;
		bool divide = false;
		bool equal = false;

		public Form1()
		{
			InitializeComponent();
		}

		private void CheckifEqual()
		{
			if (equal)
			{
				textBox1.Text = "";
				equal = false;
			}
		}

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

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

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

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

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

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

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

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

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

		private void button0_Click(object sender, EventArgs e)
		{
			CheckifEqual();
			textBox1.Text = textBox1.Text + "0";
		}

		private void buttonDecimal_Click(object sender, EventArgs e)
		{
			CheckifEqual();
			if (textBox1.Text.Contains("."))
			{
				return;
			}
			else
			{
				textBox1.Text = textBox1.Text + ".";
			}
		}

		private void buttonplusminus_Click(object sender, EventArgs e)
		{
			if (textBox1.Text.Contains("-"))
			{
				textBox1.Text = textBox1.Text.Remove(0, 1);
			}
			else
			{
				textBox1.Text = "-" + textBox1.Text;
			}
		}

		private void buttonPlus_Click(object sender, EventArgs e)
		{
			if (textBox1.Text == "")
			{
				return;
			}
			else
			{
				plus = true;
				textBox1.Tag = textBox1.Text;
				textBox1.Text = "";
			}
		}

		private void buttonEquals_Click(object sender, EventArgs e)
		{
			equal = true;
			if (plus)
			{
				decimal dec = Convert.ToDecimal(textBox1.Tag) + Convert.ToDecimal(textBox1.Text);
				textBox1.Text = dec.ToString();
			}

			if (multiply)
			{
				decimal dec = Convert.ToDecimal(textBox1.Tag) * Convert.ToDecimal(textBox1.Text);
				textBox1.Text = dec.ToString();
			}

			if (minus)
			{
				decimal dec = Convert.ToDecimal(textBox1.Tag) - Convert.ToDecimal(textBox1.Text);
				textBox1.Text = dec.ToString();
			}

			if (divide)
			{
				decimal dec = Convert.ToDecimal(textBox1.Tag) / Convert.ToDecimal(textBox1.Text);
				textBox1.Text = dec.ToString();
			}
		}

		private void buttonMinus_Click(object sender, EventArgs e)
		{
			if (textBox1.Text == "")
			{
				return;
			}
			else
			{
				minus = true;
				textBox1.Tag = textBox1.Text;
				textBox1.Text = "";
			}
		}

		private void buttonMultiply_Click(object sender, EventArgs e)
		{
			if (textBox1.Text == "")
			{
				return;
			}
			else
			{
				multiply = true;
				textBox1.Tag = textBox1.Text;
				textBox1.Text = "";
			}
		}

		private void buttonDivide_Click(object sender, EventArgs e)
		{
			if (textBox1.Text == "")
			{
				return;
			}
			else
			{
				divide = true;
				textBox1.Tag = textBox1.Text;
				textBox1.Text = "";
			}
		}

		private void buttonClear_Click(object sender, EventArgs e)
		{
			plus = minus = multiply = divide = equal = false;
			textBox1.Text = "";
			textBox1.Tag = "";
		}
	}
}


Thanks in advance.

- C++ Programmer

Is This A Good Question/Topic? 0
  • +

Replies To: C# Calculator Help

#2 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: C# Calculator Help

Posted 30 October 2009 - 08:37 PM

ok, since you don't want to hear it, I am not going to tell you. But one thing you could do is put a breakpoint in the button click for the "0" button, then see what happens when the 0 button is clicked.
Was This Post Helpful? 0
  • +
  • -

#3 MentalFloss  Icon User is offline

  • "ADDICTED"[2:5]
  • member icon

Reputation: 526
  • View blog
  • Posts: 1,397
  • Joined: 02-September 09

Re: C# Calculator Help

Posted 30 October 2009 - 09:24 PM

Quote

ok, since you don't want to hear it, I am not going to tell you. But one thing you could do is put a breakpoint in the button click for the "0" button, then see what happens when the 0 button is clicked.


Seriously, the best advice you can hope to receive. Yes, someone CAN give you the solution to the problem, but when you figure out breakpoints and just how powerful the concept is, you will realize that everything is open to you. It's no longer a question of "why is my code not working?" - questions become "What's the best way to implement this? - I haven't written it yet so I can't debug it". It becomes a sense of principle and design as opposed to voodoo code.

When you break on a statement, you can mouse over all your objects in the call stack and see what the values are. You can highlight specific calculations and right-click->select "quick watch" to see what that calculated value will be. You can CHANGE values as they are in their current state to force certain forks in logic.

Really, I am of the opinion that every programming course should take a piece of code and for the week train on breakpoint debugging... but I never went to school for this so maybe they do?

Regardless, the feature is powerful and when you learn to use it effectively, code's no longer as much of a mystery. If it doesn't compile, find the problem. If it compiles, well, the world is your oyster - suck it all down.
Was This Post Helpful? 0
  • +
  • -

#4 C++ Programmer  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 20
  • View blog
  • Posts: 548
  • Joined: 12-June 08

Re: C# Calculator Help

Posted 30 October 2009 - 09:59 PM

Well, neither of those really answered my question, but thanks for replying.

Does anyone have a real explanation for why my code doesn't allow me to use the 0 button?

I don't really care what you say, if it could be the problem, just let me know.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1