C# Calculator working incorrectly [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
The calculator works fine if I keep using the same operator(*, /, +, -) but if for example I decide to multiply the total of two added numbers it'll give me a wrong answer. I've looked for a solution but can't seem to find one.
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
}
bool multiply = false;
bool divide = false;
bool add = false;
bool subtract = false;
private void btnOne_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + "1";
}
private void btnTwo_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + "2";
}
private void btnThree_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + "3";
}
private void btnFour_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + "4";
}
private void btnFive_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + "5";
}
private void btnSix_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + "6";
}
private void btnSeven_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + "7";
}
private void btnEight_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + "8";
}
private void btnNine_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + "9";
}
private void btnZero_Click(object sender, EventArgs e)
{
if (txtDisplay.Text.Length > 0)
{
txtDisplay.Text = txtDisplay.Text + "0";
}
}
private void btnClear_Click(object sender, EventArgs e)
{
txtDisplay.Clear();
}
private void btnDecimalPoint_Click(object sender, EventArgs e)
{
if (txtDisplay.Text.Contains("."))
{
return;
}
else
{
txtDisplay.Text = txtDisplay.Text + ".";
}
}
private void btnNegative_Click(object sender, EventArgs e)
{
if (txtDisplay.Text.Contains("-"))
{
txtDisplay.Text = txtDisplay.Text.Remove(0,1);
}
else
{
txtDisplay.Text = "-" + txtDisplay.Text;
}
}
private void btnMultiply_Click(object sender, EventArgs e)
{
if (txtDisplay.Text == "")
{
return;
}
else
{
multiply = true;
txtDisplay.Tag = txtDisplay.Text;
txtDisplay.Text = "";
}
}
private void btnAdd_Click(object sender, EventArgs e)
{
if (txtDisplay.Text == "")
{
return;
}
else
{
add = true;
txtDisplay.Tag = txtDisplay.Text;
txtDisplay.Text = "";
}
}
private void btnSubtract_Click(object sender, EventArgs e)
{
if (txtDisplay.Text == "")
{
return;
}
else
{
subtract = true;
txtDisplay.Tag = txtDisplay.Text;
txtDisplay.Text = "";
}
}
private void btnEquals_Click(object sender, EventArgs e)
{
if (multiply)
{
decimal dec = Convert.ToDecimal(txtDisplay.Tag) * Convert.ToDecimal(txtDisplay.Text);
txtDisplay.Text = dec.ToString();
}
if (divide)
{
decimal dec = Convert.ToDecimal(txtDisplay.Tag) / Convert.ToDecimal(txtDisplay.Text);
txtDisplay.Text = dec.ToString();
}
if (add)
{
decimal dec = Convert.ToDecimal(txtDisplay.Tag) + Convert.ToDecimal(txtDisplay.Text);
txtDisplay.Text = dec.ToString();
}
if (subtract)
{
decimal dec = Convert.ToDecimal(txtDisplay.Tag) - Convert.ToDecimal(txtDisplay.Text);
txtDisplay.Text = dec.ToString();
}
else
{
return;
}
}
private void btnDivide_Click(object sender, EventArgs e)
{
if (txtDisplay.Text == "")
{
return;
}
else
{
divide = true;
txtDisplay.Tag = txtDisplay.Text;
txtDisplay.Text = "";
}
}
}

It looks like the problem is that you aren't clearing your operation commands. i.e. you set it to Add, but you never clear the Add when you set it to Multiply. If you look in the btnEquals_Click method, it's possible to have several operations active at once, and it will execute all of them.

Look at your btnEquals_Click event. Someone chooses to add, so add = true and it's the only if block that executes - everything good so far.
Then someone chooses to multiply, so now multiply = true, but add = true as well, so now you're multiplying and adding. If someone goes through all the operators, then (because you're never clearing your operator flags), every number thereafter will be multiplied, then divided, then added, and finally subtracted.
To fix it, you could create a method that clears the operators:
private void ResetOperatorFlags()
{
multiply = false;
divide = false;
add = false;
subtract = false;
}
Then call it before you perform an operation on the number:
private void btnMultiply_Click(object sender, EventArgs e)
{
if (txtDisplay.Text == "")
return;
ResetOperatorFlags();
multiply = true;
txtDisplay.Tag = txtDisplay.Text;
txtDisplay.Text = "";
}
Finally, in your btnEquals_Click event, use else if... you won't really need it after clearing the flags, but there's no sense in testing every flag if only one can be set at a time:
private void btnEquals_Click(object sender, EventArgs e)
{
if (multiply)
{
...
}
else if (divide)
{
...

Related

Trying to minus 2 values together results in them adding instead, also all operators exept addition do not function

For some reason when I test the code the minus if statement adds the two values togehther instead of minusing them, along with that the other if statements for times and subtraction return the error message
System.FormatException: 'Input string was not in a correct format.'
The first if statement for addition works fine despite this
public Form1()
{
InitializeComponent();
}
private void Calculation()
{
decimal answer;
if (savedOperator == "+")
{
answer = savedNumber + Convert.ToDecimal(txtBox.Text);
txtBox.Text = answer.ToString();
savedOperator = "";
}
if (savedOperator == "-")
{
answer = savedNumber - Convert.ToDecimal(txtBox.Text);
txtBox.Text = answer.ToString();
savedOperator = "";
}
if (savedOperator == "*")
{
answer = savedNumber * Convert.ToDecimal(txtBox.Text);
txtBox.Text = answer.ToString();
savedOperator = "";
}
if (savedOperator == "/")
{
answer = savedNumber / Convert.ToDecimal(txtBox.Text);
txtBox.Text = answer.ToString();
savedOperator = "";
}
}
private void SaveNumber()
{
savedNumber = System.Convert.ToDecimal(txtBox.Text);
txtUpperBox.Text = savedNumber.ToString();
}
// numbers
private void btn1_Click(object sender, EventArgs e)
{
txtBox.Text = txtBox.Text + "1";
}
private void btn2_Click(object sender, EventArgs e)
{
txtBox.Text = txtBox.Text + "2";
}
private void btn3_Click(object sender, EventArgs e)
{
txtBox.Text = txtBox.Text + "3";
}
private void btn4_Click(object sender, EventArgs e)
{
txtBox.Text = txtBox.Text + "4";
}
private void btn5_Click(object sender, EventArgs e)
{
txtBox.Text = txtBox.Text + "5";
}
private void btn6_Click(object sender, EventArgs e)
{
txtBox.Text = txtBox.Text + "6";
}
private void btn7_Click(object sender, EventArgs e)
{
txtBox.Text = txtBox.Text + "7";
}
private void btn8_Click(object sender, EventArgs e)
{
txtBox.Text = txtBox.Text + "8";
}
private void btn9_Click(object sender, EventArgs e)
{
txtBox.Text = txtBox.Text + "9";
}
private void btn0_Click(object sender, EventArgs e)
{
txtBox.Text = txtBox.Text + "0";
}
// operators
private void btnPlus_Click(object sender, EventArgs e)
{
SaveNumber();
txtBox.Clear();
savedOperator = "+";
txtBox.Text = "+";
}
private void btnMinus_Click(object sender, EventArgs e)
{
SaveNumber();
txtBox.Clear();
savedOperator = "-";
txtBox.Text = "-";
}
private void btnTimes_Click(object sender, EventArgs e)
{
SaveNumber();
txtBox.Clear();
savedOperator = "*";
txtBox.Text = "*";
}
private void btnDivide_Click(object sender, EventArgs e)
{
SaveNumber();
txtBox.Clear();
savedOperator = "/";
txtBox.Text = "/";
}
private void btnEquals_Click(object sender, EventArgs e)
{
Calculation();
}
}
}
I don't see any obvious problems in your math logic.
I tested it in https://dotnetfiddle.net/Uo2EHM and it works fine. The issue could be in the inputs you are getting or in conversion.
You can either set breakpoints at various points in your code to see what the values are or print out the values to another "debug" text box.
Some potential issues I see are in conversion between number and string. Sometimes, it may not give you the results you expect.
Also, you might want to use a switch statement instead of multiple if statements but that is not a bug you are seeing.
Could the issue be with the value of the TextBox before the calculations are performed? If you have "8--3" it would be the same as 8 - (-3). Try your code with different text boxes for the numbers and the operators to see if the problem occurs.

Calculator Trouble: Only getting second value I enter

I created a basic calculator, but each time I enter the second value and press the equal button, I get no result back. Only the value I entered 2nd.
My methods for +-*/ are in a separate class. What will the best way be to resolve this problem? It is probably a simple error, but I can't find it. Can you also please give me an explanation on what I did wrong. Thanks in Advance.
public sealed partial class Calculator : Page
{
public double num01, num02;
int operater;
public Calculator()
{
this.InitializeComponent();
}
private void btn1_Click(object sender, RoutedEventArgs e)
{
txtcalcdisplay.Text =txtcalcdisplay.Text+ "1";
}
private void btn2_Click(object sender, RoutedEventArgs e)
{
txtcalcdisplay.Text = txtcalcdisplay.Text + "2";
}
private void btn3_Click(object sender, RoutedEventArgs e)
{
txtcalcdisplay.Text = txtcalcdisplay.Text + "3";
}
private void btn4_Click(object sender, RoutedEventArgs e)
{
txtcalcdisplay.Text = txtcalcdisplay.Text + "4";
}
private void btn5_Click(object sender, RoutedEventArgs e)
{
txtcalcdisplay.Text = txtcalcdisplay.Text + "5";
}
private void btn6_Click(object sender, RoutedEventArgs e)
{
txtcalcdisplay.Text = txtcalcdisplay.Text + "6";
}
private void btn7_Click(object sender, RoutedEventArgs e)
{
txtcalcdisplay.Text = txtcalcdisplay.Text + "7";
}
private void btn8_Click(object sender, RoutedEventArgs e)
{
txtcalcdisplay.Text = txtcalcdisplay.Text + "8";
}
private void btn9_Click(object sender, RoutedEventArgs e)
{
txtcalcdisplay.Text = txtcalcdisplay.Text + "9";
}
private void Clear_Click(object sender, RoutedEventArgs e)
{
txtcalcdisplay.Text = string.Empty;
}
private void btnsubtract_Click(object sender, RoutedEventArgs e)
{
num01 = Convert.ToDouble(txtcalcdisplay.Text);
txtcalcdisplay.Text = "";
operater = '1';
}
private void btnadd_Click(object sender, RoutedEventArgs e)
{
num01 = Convert.ToDouble(txtcalcdisplay.Text);
txtcalcdisplay.Text = "";
operater = '2';
}
private void btnmultiply_Click(object sender, RoutedEventArgs e)
{
num01 = Convert.ToDouble(txtcalcdisplay.Text);
txtcalcdisplay.Text = "";
operater = '3';
}
private void btndivide_Click(object sender, RoutedEventArgs e)
{
num01 = Convert.ToDouble(txtcalcdisplay.Text);
txtcalcdisplay.Text = "";
operater = '4';
}
private void btnequals_Click(object sender, RoutedEventArgs e)
{
switch (operater)
{
case 1:
num02 = Convert.ToDouble(txtcalcdisplay.Text);
CalculationClass sub = new CalculationClass();
double answer= sub.Subtract(num01, num02);
txtcalcdisplay.Text = answer.ToString();
break;
case 2:
num02 = Convert.ToDouble(txtcalcdisplay.Text);
CalculationClass add = new CalculationClass();
answer= add.Addition(num01, num02);
txtcalcdisplay.Text = answer.ToString();
break;
case 3:
num02 = Convert.ToDouble(txtcalcdisplay.Text);
CalculationClass mult = new CalculationClass();
answer = mult.Multiply(num01, num02);
txtcalcdisplay.Text = answer.ToString();
break;
case 4:
num02 = Convert.ToDouble(txtcalcdisplay.Text);
CalculationClass div = new CalculationClass();
answer = div.Div(num01, num02);
txtcalcdisplay.Text = Convert.ToString(answer);
break;
}
}
private void btnback_Click(object sender, RoutedEventArgs e)
{
}
private void btnplusdivideminus_Click(object sender, RoutedEventArgs e)
{
}
private void btncomma_Click(object sender, RoutedEventArgs e)
{
}
private void btngallery_Click(object sender, RoutedEventArgs e)
{
}
private void btncontact_Click(object sender, RoutedEventArgs e)
{
}
private void num0_Click(object sender, RoutedEventArgs e)
{
txtcalcdisplay.Text += "0";
num01 = Convert.ToDouble(txtcalcdisplay.Text);
}
}
class CalculationClass
{
double answer;
public double Addition(double x, double y)
{
answer = x + y;
return answer;
}
public double Subtract(double x, double y)
{
answer = x - y;
return answer;
}
public double Multiply(double x, double y)
{
answer = x * y;
return answer;
}
public double Div(double x, double y)
{
answer = x / y;
return answer;
}
}
I'm not 100% certain, but I'm fairly sure this has to do with the fact that your variable operater is an int but you're assigning a character to it (this works - characters can be assigned to ints) and then comparing it back to the integer (eg doing 1 == '1')
int x = '1';
Console.WriteLine(x); // outputs 49
Console.WriteLine(x == 1); // outputs false
So to fix it either use the characters in your switch:
switch(operater){
case '1': ...
}
Or assign the integer 1,2,3,4 rather than the character '1','2,'3','4'
private void btnadd_Click(object sender, RoutedEventArgs e)
{
num01 = Convert.ToDouble(txtcalcdisplay.Text);
txtcalcdisplay.Text = "";
operater = 2; // here
}
So in your operator click handlers (the ones for +,-,*,/ etc.) you are setting your operater variable to a character '1','2', etc. Well, your operater variable is an int. But wait, you shouldn't be able to assign a char to an int?! Well, the compiler does an implicit conversion here (language spec says it should).
So what ends up happening when you do operater = '1' is that operater gets assigned 49 ('1''s ASCII value). Then when you get to your equals button click handler it hits that switch statement. And guess what? You don't have a case for 49. So nothing happens and you keep seeing your second number as the text on screen.
So to fix it, remove the single quotes around your numbers you assign to operater in your operator handlers. IE:
private void btnsubtract_Click(object sender, RoutedEventArgs e)
{
num01 = Convert.ToDouble(txtcalcdisplay.Text);
txtcalcdisplay.Text = "";
operater = 1; // <-- change this line to be like this, removed the single quotes
}

Double result when using keypress

I made this calculator in C# and I have one problem: When I press keyboard something like 1 it gives me incorrect result double number. I don't get the correct result.
Do you know what I am doing wrong,
Thank you very much!
namespace Calculator
{
public partial class Calculator : Form
{
Double value = 0;
String operation = "";
int i = 0;
bool operation_pressed = false;
bool button_dot = false;
OperationClass class1 = new OperationClass();
public Calculator()
{
InitializeComponent();
this.KeyPreview = true;
this.KeyPress += new KeyPressEventHandler(Calculator_KeyPress);
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void Calculator_Load(object sender, EventArgs e)
{
}
private void Calculator_KeyPress(object sender, KeyPressEventArgs e)
{
if ((result.Text == "0") || (operation_pressed))
result.Clear();
switch (e.KeyChar)
{
// key press from 0-9
case (char)48:
case (char)49:
case (char)50:
case (char)51:
case (char)52:
case (char)53:
case (char)54:
case (char)55:
case (char)56:
case (char)57:
e.Handled = true;
result.Text += e.KeyChar.ToString();
break;
}
}
private void button_Click(object sender, EventArgs e)
{
if ((result.Text == "0") || (operation_pressed))
result.Clear();
operation_pressed = false;
Button b = (Button)sender;
result.Text = result.Text + b.Text;
}
private void operator_click(object sender, EventArgs e)
{
Button b = (Button)sender;
operation = b.Text;
value = Double.Parse(result.Text);
operation_pressed = true;
equation.Text = value + " " + operation;
}
private void buttonCE_Click(object sender, EventArgs e)
{
result.Text = "0";
equation.Text = "";
button_dot = false;
}
private void buttonC_Click(object sender, EventArgs e)
{
result.Clear();
value = 0;
}
private void buttonEqual_Click(object sender, EventArgs e)
{
equation.Text = "";
button_dot = false;
operation_pressed = true;
if (operation != "")
result.Text = class1.GetResult(operation, value, result.Text);
else
result.Text = result.Text + "";
}
private void buttonDot_Click(object sender, EventArgs e)
{
Button b = (Button)sender;
if ((!button_dot && !operation_pressed) || result.Text == "0")
result.Text = result.Text + b.Text;
button_dot = true;
}
}
}
You don't need to append keychar in your result.Text (Assuming result is your TextBox item) as you have written in line result.Text += e.KeyChar.ToString();. That's the line which is doubling your input.
Remove it and it'll work as expected.
I'd change it from keypress to KeyUp instead. That way it only fires as soon as the key is released.
Edit: Check out this info: Difference between the KeyDown Event, KeyPress Event and KeyUp Event in Visual Studio

When I click the button it adds £3.00 to the total on top of what is correct [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
The program is for a pizza menu, the button I click to add the pizza adds £3.00 on top of the actual total, I have looked for errors but can not find the problem. The program is not fully finished, only the adding of the pizza total is complete but something is wrong with the code which I can not find.
I also would like any suggestion to make the program more efficient.
string stuffedcrust;
string Deeppan;
string thincrispy;
private void radioButton3_CheckedChanged(object sender, EventArgs e)
{ //Add type of crust to summary box
stuffedcrust = rbStuffedcrust.Text;
SummaryBox.Text = stuffedcrust;
}
private void button5_Click(object sender, EventArgs e)
{
Application.Exit();
}
private int clickCounter = 0;
private void button4_Click(object sender, EventArgs e) // Button to add the value pf the pizza to the text box
{
this.clickCounter++;
if (this.clickCounter < 10) // number of time the button can be pressed to add Pizzas
{
radioButton1.Checked = false;
radioButton2.Checked = false;
radioButton3.Checked = false;
radioButton4.Checked = false;
radioButton6.Checked = false;
radioButton7.Checked = false;
rbThinandcrispy.Checked = false;
rbStuffedcrust.Checked = false;
rbDeeppan.Checked = false;
checkBoxCrispyOnions.Checked = false;
checkBoxExtraCheese.Checked = false;
checkBoxPeppers.Checked = false;
checkBoxPepperoni.Checked = false;
checkBoxGarlicSauce.Checked = false;
checkBox12.Checked = false;
/*StreamWriter sw = new StreamWriter(SummaryBox.Text, true);
sw.WriteLine();
sw.WriteLine();
sw.WriteLine();
sw.WriteLine();
sw.Close(); */
MessageBox.Show("Pizza Added");
}
else
{
MessageBox.Show("No more Pizza's can be added, the maximum order is 10");
}
}
private void button7_Click(object sender, EventArgs e)
{
File.Create(textBox1.Text).Close();
}
private void button6_Click(object sender, EventArgs e)
{
OpenFileDialog of = new OpenFileDialog();
of.ShowDialog();
textBox1.Text = of.FileName;
}
public double PizzaPrice { get; set; } //Global Public
double ExtraTopping; //Global
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
string CT;
if (radioButton2.Enabled == true) //PIZZA CHEESE TOMATO
{
double ctp = 3.50;
PizzaPrice += ctp;
txtPizzaPrice.Text = "£ " + PizzaPrice.ToString();
CT = radioButton2.Text;
SummaryBox.Text = CT;
}
else
{
SummaryBox.Clear();
}
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
string VS;
if (radioButton1.Enabled == true) //PIZZA Veg SUpreme
{
double vsp = 5.20;
PizzaPrice += vsp;
txtPizzaPrice.Text = "£ " + PizzaPrice.ToString();
VS = radioButton1.Text;
SummaryBox.Text = VS;
}
else
{
SummaryBox.Clear();
}
}
private void radioButton3_CheckedChanged_1(object sender, EventArgs e)
{
string SV;
if (radioButton3.Enabled == true) //PIZZA SPicy Veg
{
double svp = 5.20;
PizzaPrice += svp;
txtPizzaPrice.Text = "£ " + PizzaPrice.ToString();
SV = radioButton3.Text;
SummaryBox.Text = SV;
}
else
{
SummaryBox.Clear();
}
}
private void radioButton6_CheckedChanged(object sender, EventArgs e)
{
string MF;
if (radioButton6.Enabled == true) //PIZZA MEAT FEAST
{
double mfp = 5.80;
PizzaPrice += mfp;
txtPizzaPrice.Text = "£ " + PizzaPrice.ToString();
MF = radioButton6.Text;
SummaryBox.Text = MF;
}
else
{
SummaryBox.Clear();
}
}
private void radioButton7_CheckedChanged(object sender, EventArgs e)
{
string HP;
if (radioButton7.Enabled == true) //PIZZA Ham pineapple
{
double hpp = 4.20;
PizzaPrice += hpp;
txtPizzaPrice.Text = "£ " + PizzaPrice.ToString();
HP = radioButton7.Text;
SummaryBox.Text = HP;
}
else
{
SummaryBox.Clear();
}
}
private void radioButton4_CheckedChanged(object sender, EventArgs e)
{
string SF;
if (radioButton4.Enabled == true) // PIZZA SEAFOOD
{
double sfp = 5.60;
PizzaPrice += sfp;
txtPizzaPrice.Text = "£ " + PizzaPrice.ToString();
SF = radioButton4.Text;
SummaryBox.Text = SF;
}
else
{
SummaryBox.Clear();
}
}
private void button1_Click(object sender, EventArgs e)
{
Bill sf = new Bill();
sf.Show(); // Open Bill
}
private void checkBox15_CheckedChanged(object sender, EventArgs e)
{ //EXTRA CHEESE
string EC;
if (checkBoxExtraCheese.Checked)
{
double ecp = .50;
ExtraTopping += ecp;
txtPizzaPrice.Text = "£ " + ExtraTopping.ToString();
EC = checkBoxExtraCheese.Text;
}
else
{
ExtraTopping = 0 + PizzaPrice;
txtPizzaPrice.Text = "£ " + ExtraTopping.ToString();
}
}
private void checkBox10_CheckedChanged(object sender, EventArgs e)
{ //PEPPERS
string PEP;
if (checkBoxPeppers.Checked)
{
double pepp = .50;
ExtraTopping += pepp;
txtPizzaPrice.Text = "£ " + ExtraTopping.ToString();
PEP = checkBoxPeppers.Text;
}
else
{
ExtraTopping = 0 + PizzaPrice;
txtPizzaPrice.Text = "£ " + ExtraTopping.ToString();
}
}
private void checkBoxCrispyOnions_CheckedChanged(object sender, EventArgs e)
{ //CRISPY ONIONS
string CO;
if (checkBoxCrispyOnions.Checked)
{
double cop = .50;
ExtraTopping += cop;
txtPizzaPrice.Text = "£ " + ExtraTopping.ToString();
CO = checkBoxCrispyOnions.Text;
}
else
{
ExtraTopping = 0 + PizzaPrice;
txtPizzaPrice.Text = "£ " + ExtraTopping.ToString();
}
}
private void checkBoxGarlicSauce_CheckedChanged(object sender, EventArgs e)
{ //Garlic Sauce
string GS;
if (checkBoxGarlicSauce.Checked)
{
double gsp = .50;
ExtraTopping += gsp;
txtPizzaPrice.Text = "£ " + ExtraTopping.ToString();
GS = checkBoxGarlicSauce.Text;
}
else
{
ExtraTopping = 0.0 + PizzaPrice;
txtPizzaPrice.Text = "£ " + ExtraTopping.ToString();
}
}
private void checkBoxPepperoni_CheckedChanged(object sender, EventArgs e)
{ //PEPPERONI
string Proni;
if (checkBoxPepperoni.Checked)
{
double pepperoni = .50;
ExtraTopping += pepperoni;
txtPizzaPrice.Text = "£ " + ExtraTopping.ToString();
Proni = checkBoxPepperoni.Text;
}
else
{
ExtraTopping = 0 + PizzaPrice;
txtPizzaPrice.Text = "£ " + ExtraTopping.ToString();
}
}
private void rbThinandcrispy_CheckedChanged(object sender, EventArgs e)
{ //Add type of crust to summary box
thincrispy = rbThinandcrispy.Text;
SummaryBox.Text = thincrispy;
}
private void rbDeeppan_CheckedChanged(object sender, EventArgs e)
{ //Add type of crust to summary box
Deeppan = rbDeeppan.Text;
SummaryBox.Text = Deeppan;
}
Menu(object sender, System.ComponentModel.CancelEventArgs e)
{
clickCounter--;
}
}
}
Your code is fairly confusing, particularly because of controls with names like radioButton1, radioButton2, etc...
It looks like your issue could be stemming from the fact that when your pizza type radio buttons change state, you add the price of the newly selected pizza to the pizza price instead of replacing it.
I would strongly encourage you to adopt an object-oriented approach in your application. If you create a Pizza class with fields for extra toppings, each Pizza that you add to an order will know everything that should be on it and how much it costs in total, and the Pizza could have a .ToString() overload that would build the summary text you're looking for: i.e., "Supreme pizza (4.50) : cheese (.50), crispy onions (.50) = Total: 5.50"
Separating your business objects from the UI elements (checkboxes, radio buttons, text fields) will vastly simplify what you're trying to do.
This is a guess but your add button only sets all your check boxes.Checked = false and then displays a popup. It never actually adds the price of the current pizza selected to any totals.

C# Calculator - textBox to listBox

This calculator is extremely basic and so I'm only working with two values to calculate at a time.
I am calculating two values only val1 and val2. These values can be of any size including a decimal point.
I have buttons for numbers 0-9, a decimal point, plus, minus, divide, multiply and equals.
I want val1, val2 and the operator(ex. +,-,/,*) I choose to calculate these values to appear in my listBox after I hit the = button. Every calculation I make I want it added to the listBox underneath the previous calculation. When the listBox grows to a certain amount of calculations (10-15ish) I want the oldest calculation removed from the bottom and the newest added to the top of the list.
public partial class Form1 : Form
{
// global variables
bool plus = false;
bool minus = false;
bool multiply = false;
bool divide = false;
bool equal = false;
public Form1()
{
InitializeComponent();
} // **MUST HAVE**
private void button0_Click(object sender, EventArgs e)
{
CheckIfEqual();
textBox1.Text = textBox1.Text + "0";
} // makes number click usable
private void CheckIfEqual()
{
if (equal) // checks if equal is true
{
textBox1.Text = ""; // clear textBox
equal = false; // tells program there is new calculation
}
}
private void button1_Click(object sender, EventArgs e)
{
CheckIfEqual();
textBox1.Text = textBox1.Text + "1";
} // makes number click usable
private void button2_Click(object sender, EventArgs e)
{
CheckIfEqual();
textBox1.Text = textBox1.Text + "2";
} // makes number click usable
private void button3_Click(object sender, EventArgs e)
{
CheckIfEqual();
textBox1.Text = textBox1.Text + "3";
} // makes number click usable
private void button4_Click(object sender, EventArgs e)
{
CheckIfEqual();
textBox1.Text = textBox1.Text + "4";
} // makes number click usable
private void button5_Click(object sender, EventArgs e)
{
CheckIfEqual();
textBox1.Text = textBox1.Text + "5";
} // makes number click usable
private void button6_Click(object sender, EventArgs e)
{
CheckIfEqual();
textBox1.Text = textBox1.Text + "6";
} // makes number click usable
private void button7_Click(object sender, EventArgs e)
{
CheckIfEqual();
textBox1.Text = textBox1.Text + "7";
} // makes number click usable
private void button8_Click(object sender, EventArgs e)
{
CheckIfEqual();
textBox1.Text = textBox1.Text + "8";
} // makes number click usable
private void button9_Click(object sender, EventArgs e)
{
CheckIfEqual();
textBox1.Text = textBox1.Text + "9";
} // makes number click usable
private void buttonDecimal_Click(object sender, EventArgs e)
{
CheckIfEqual();
if (textBox1.Text.Contains("."))
{
return;
} // if it contains a decimal execute
else
{
textBox1.Text = textBox1.Text + ".";
} // adds decimal
} // end decimal
private void buttonPlusMinus_Click(object sender, EventArgs e)
{
if (textBox1.Text.Contains("-"))
{
textBox1.Text = textBox1.Text.Remove(0, 1);
} // if it contains the - character remove it
else
{
textBox1.Text = "-" + textBox1.Text;
} //
} // adds/removes -
private void buttonPlus_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
{
return;
} // if textbox is empty return
else
{
plus = true;
textBox1.Tag = textBox1.Text; // stores number
textBox1.Text = ""; // clears textbox
} // tells program we are adding
} // end plus
private void buttonMinus_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
{
return;
} // if textbox is empty return
else
{
minus = true;
textBox1.Tag = textBox1.Text; // stores number
textBox1.Text = ""; // clears textbox
} // tells program we are subtracting
}
private void buttonMultiply_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
{
return;
} // if textbox is empty return
else
{
multiply = true;
textBox1.Tag = textBox1.Text; // stores number
textBox1.Text = ""; // clears textbox
} // tells program we are multiplying
}
private void buttonDivide_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
{
return;
} // if textbox is empty return
else
{
divide = true;
textBox1.Tag = textBox1.Text; // stores number
textBox1.Text = ""; // clears textbox
} // tells program we are dividing
}
private void buttonEquals_Click(object sender, EventArgs e)
{
equal = true; // tells program it made a calculation
if (plus) // if true execute
{
decimal dec = Convert.ToDecimal(textBox1.Tag) + Convert.ToDecimal(textBox1.Text);
textBox1.Text = dec.ToString(); // display value once add is finished
plus = false;
} // end if plus
if (minus) // if true execute
{
decimal dec = Convert.ToDecimal(textBox1.Tag) - Convert.ToDecimal(textBox1.Text);
textBox1.Text = dec.ToString(); // display value once add is finished
minus = false;
} // end if minus
if (multiply) // if true execute
{
decimal dec = Convert.ToDecimal(textBox1.Tag) * Convert.ToDecimal(textBox1.Text);
textBox1.Text = dec.ToString(); // display value once add is finished
multiply = false;
} // end if multiply
try
{
if (divide) // if true execute
{
decimal dec = Convert.ToDecimal(textBox1.Tag) / Convert.ToDecimal(textBox1.Text);
textBox1.Text = dec.ToString(); // display value once add is finished
divide = false;
} // end if divide
} // try
catch (DivideByZeroException ex)
{
MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK);
} // catch
} // end buttonEquals
private void buttonClear_Click(object sender, EventArgs e)
{
plus = minus = multiply = divide = false; // makes all false regardless
textBox1.Text = ""; // clear
textBox1.Tag = ""; // clears
}
} // end partial class Form1
}// end namespace
Refactoring would be nice...
For example, you can use the same method for all your "numeric button click" event.
instead of
private void button1_Click(object sender, EventArgs e)
{
CheckIfEqual();
textBox1.Text = textBox1.Text + "1";
} // makes number click usable
...
private void button9_Click(object sender, EventArgs e)
{
CheckIfEqual();
textBox1.Text = textBox1.Text + "9";
} // makes number click usable
just use one method.
button1.Click += NumericButtonClick;
...
button9.Click +=NumericButtonClick;
private void NumericButtonClick(object sender, EventArgs e) {
CheckIfEqual();
textBox1.Text = textBox1.Text + ((Button)sender).Text;
}
Anyway.
Say you have a ListBox historyListBox;
Then just add a method
private void PopulateListBox(string left, string right, string operator) {
var newContent = string.Format("{0} {1} {2}", left, operator, right);
if (historyListBox.Items.Count == 15)
historyListBox.Items.RemoveAt(14);//
historyListBox.Items.Insert(0, newContent);
}
and then you could change the "equal_click event" (which sould be also refactored, but that's another problem)
private void buttonEquals_Click(object sender, EventArgs e)
{
var left = textBox1.Tag;
var right = textBox1.Text;
var operator = string.Empty;
equal = true; // tells program it made a calculation
if (plus) // if true execute
{
operator = "+";
decimal dec = Convert.ToDecimal(left) + Convert.ToDecimal(right);
textBox1.Text = dec.ToString(); // display value once add is finished
plus = false;
} // end if plus
if (minus) // if true execute
{
operator = "-";
decimal dec = Convert.ToDecimal(left) - Convert.ToDecimal(right);
textBox1.Text = dec.ToString(); // display value once add is finished
minus = false;
} // end if minus
//...etc
PopulateListBox(left, right, operator);
}

Categories