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();
}
bool plus;
bool minus;
bool multiply;
bool divide;
private void button0_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "0";
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "1";
}
private void button2_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "2";
}
private void button3_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "3";
}
private void button4_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "4";
}
private void button5_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "5";
}
private void button6_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "6";
}
private void button7_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "7";
}
private void button8_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "8";
}
private void button9_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "9";
}
private void buttonPlus_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
return;
else
{
plus = true;
textBox1.Text = textBox1.Text + " + ";
}
}
private void buttonDivide_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
return;
else
{
divide = true;
textBox1.Text = textBox1.Text + " / ";
}
}
private void buttonMinus_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
return;
else
{
minus = true;
textBox1.Text = textBox1.Text + " - ";
}
}
private void buttonMultiply_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
return;
else
{
multiply = true;
textBox1.Text = textBox1.Text + " * ";
}
}
private void buttonClear_Click(object sender, EventArgs e)
{
textBox1.Text = string.Empty;
}
private void buttonEquals_Click(object sender, EventArgs e)
{
if (plus)
{
}
if (minus)
{
}
if (multiply)
{
}
if (divide)
{
}
}
}
}
The part I am stuck on, is after I press a math button (+,-,/,*). I only want one character to show in the textbox, unless I press another number.
Example: Right now, this is what happens:
87 + + + + + + + +
I want 87 +
and I want it where I can add another if the user adds another number, so like 87 + 87 + etc.
Also, I need a little direction when it comes to adding the math logic.
I was thinking of somehow storing whatever was in the string prior to a math sign in a variable, then doing the same for the numbers proceeding the math sign. Is that fine or is there an easier/less complicated way to accomplish this?
For the +-*\ situation.. I would suggest a Boolean property like lastCharIsSymbol. Whenever you click a number set it equal to false, and when you click a symbol set it equal to true. Then you can add an if statement in your symbol click events to only add the symbol IF lastCharIsSymbol = false.
If you would rather not do this, you can also change up your if statement to just take a substring of whatever is in the textbox and find out what the last character is.