I hear ya, I had this same problem. Still working things out on a full scale, but so far it works as required.
I fixed mine by adding a few variables. One so it knows if an operator has been pressed previously, one which holds the current operator, and one for the previously pressed operator. If i had 1+1 and then pressed =, it would check for the operator used and make the calculation.
If I pressed an operator, it would set the variable to true if it was false, so if another operator was pressed before equals, it would make a subtotal and then calculate based on the currently pressed variable.
My code in the example uses only a textBox, 1 key, + key and = key.
double subtotal = 0;
bool operatorPressed = false;
string currentOperator = "";
string previousOperator = "";
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = button1.Text;
}
private void button4_Click(object sender, EventArgs e)
{
textBox1.Text = button4.Text;
}
private void button8_Click(object sender, EventArgs e)
{
if (operatorPressed == true)
{
previousOperator = currentOperator;
currentOperator = "+";
switch (previousOperator)
{
case "+":
subtotal = subtotal + double.Parse(textBox1.Text);
textBox1.Text = subtotal.ToString();
break;
case "*":
subtotal = subtotal * double.Parse(textBox1.Text);
textBox1.Text = subtotal.ToString();
break;
}
}
else if(operatorPressed == false)
{
operatorPressed = true;
currentOperator = "+";
subtotal = double.Parse(textBox1.Text);
}
}
private void button10_Click(object sender, EventArgs e)
{
if (operatorPressed == true)
{
previousOperator = currentOperator;
currentOperator = "*";
switch (previousOperator)
{
case "+":
subtotal = subtotal + double.Parse(textBox1.Text);
textBox1.Text = subtotal.ToString();
break;
case "*":
subtotal = subtotal * double.Parse(textBox1.Text);
textBox1.Text = subtotal.ToString();
break;
}
}
else if (operatorPressed == false)
{
operatorPressed = true;
currentOperator = "*";
subtotal = double.Parse(textBox1.Text);
}
}
private void button9_Click(object sender, EventArgs e)
{
switch(currentOperator)
{
case "+":
subtotal = subtotal + double.Parse(textBox1.Text);
textBox1.Text = subtotal.ToString();
subtotal = 0;
currentOperator = "";
previousOperator = "";
operatorPressed = false;
break;
case "*":
subtotal = subtotal * double.Parse(textBox1.Text);
textBox1.Text = subtotal.ToString();
subtotal = 0;
currentOperator = "";
previousOperator = "";
operatorPressed = false;
break;
}
}
I hope this is clear enough to understand