0
 private void radioButton1_CheckedChanged(object sender, EventArgs e)
    {
        if (smallPizza.Checked)
        {
            int smallPizzaPrice = 9;
            pizzaTotal += smallPizzaPrice;
        }
    }

when I send the value of this to a text box it gives the correct value '9'

private void mediumPizza_CheckedChanged_1(object sender, EventArgs e)
    {
        if (mediumPizza.Checked)
        {
            int mediumPizzaPrice = 12;
            pizzaTotal += mediumPizzaPrice;
        }
    }

when I send this value to the same text box I get '21'

I have code for a large pizza in which I initialise the price at 14 but the value 23 is sent to the textbox.

The rest of the code:

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        double pizzaTotal = 0;
        double finalPizzaTotal = 0;
        double finalPrice =0;
        string totalPrice = "";

        private void button3_Click(object sender, EventArgs e)
        {
            orderTotal.Text += finalPrice;
            double totalVat = finalPrice * .21;
            totalAfterVat.Text += finalPrice + totalVat;
        }

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

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (smallPizza.Checked)
            {
                int smallPizzaPrice = 9;
                pizzaTotal += smallPizzaPrice;
            }
        }

        private void mediumPizza_CheckedChanged_1(object sender, EventArgs e)
        {
            if (mediumPizza.Checked)
            {
                int mediumPizzaPrice = 12;
                pizzaTotal += mediumPizzaPrice;
            }
        }

        private void largePizza_CheckedChanged(object sender, EventArgs e)
        {

            if (largePizza.Checked)
            {
                pizzaTotal += 14;
            }
        }

        private void checkBox4_CheckedChanged(object sender, EventArgs e)
        {
            if (pineappple.Checked) {
                pizzaTotal += 0.5;
            }
        }

        private void calculateTotal_Click(object sender, EventArgs e)
        {
            costOfPizza.Text = String.Empty;
            finalPizzaTotal += pizzaTotal;
            pizzaTotal = 0;
            finalPrice += finalPizzaTotal;
            costOfPizza.Text = costOfPizza.Text + finalPizzaTotal;
        }



        private void margeritta_CheckedChanged(object sender, EventArgs e)
        {
            if (margeritta.Checked)
            {
                pizzaTotal += 1;
            }
        }

        private void Pepperoni_CheckedChanged(object sender, EventArgs e)
        {
            if (pepperoni.Checked) {
                pizzaTotal += 2;
            }
        }

        private void hawaiian_CheckedChanged(object sender, EventArgs e)
        {
            if (hawaiian.Checked)
            {
                pizzaTotal += 3;
            }
        }

        private void salami_CheckedChanged(object sender, EventArgs e)
        {
            if (salami.Checked)
            {
                pizzaTotal += 0.5;
            }
        }

        private void goatsCheese_CheckedChanged(object sender, EventArgs e)
        {
            if (goatsCheese.Checked)
            {
                pizzaTotal += 0.5;
            }
        }

        private void chirizo_CheckedChanged(object sender, EventArgs e)
        {
            if (chirizo.Checked)
            {
                pizzaTotal += 0.5;
            }
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                finalPizzaTotal = pizzaTotal;
                pizzaTotal = 0;

                pineappple.Checked = false;
                chirizo.Checked = false;
                salami.Checked = false;
                pepperoni.Checked = false;
                goatsCheese.Checked = false;
                margeritta.Checked = false;
                pepperoni.Checked = false;
                hawaiian.Checked = false;
                smallPizza.Checked = false;
                mediumPizza.Checked = false;
                largePizza.Checked = false;

                costOfPizza.Text = string.Empty;
            }
            else if (!checkBox1.Checked)
            {
                MessageBox.Show(
                    "If you don't want to order another pizza press " +
                    " confirm order");
            }
        }
    }
}
Jodrell
  • 34,946
  • 5
  • 87
  • 124
Jerry Murphy
  • 348
  • 1
  • 5
  • 21

2 Answers2

2

It seems that pizzaTotal is a variable of the Form, the Controls are placed on. So the first time the value is 0, adding 9 gives 9. But when pressing the second button 12 is added to the previous value 9. That gives 21.

Try to press the buttons the other way round and see the result.

elite
  • 191
  • 2
  • 13
  • If I start the program and only press medium without touching small the result is still 21. – Jerry Murphy Dec 05 '14 at 15:11
  • Can you please add how the variable `pizzaTotal`is configured and initialized. – elite Dec 05 '14 at 15:12
  • Maybe you set the `Checked` property of `radioButton1` in the `Shown` event of the form. This triggers the event `CheckedChanged` to be fired. – elite Dec 05 '14 at 15:16
  • What is `smallPizza.Checked`. This is in the event of `radioButton1`. – elite Dec 05 '14 at 15:23
  • elite, Sorry I don't understand what you mean by they Checked property in the shown event of the form. Is it in the properties window there is a checked option that I can change to True or False? If so They are all set to False – Jerry Murphy Dec 05 '14 at 15:24
  • @jerry. No, I wrote the comment before I have seen the whole code. It was just a assumtion. – elite Dec 05 '14 at 15:26
  • I changed the name of the radiobutton in the properties window to smalPizza but it never got updated in the code, seesm to sometimes happen in Visual Studio..but It can be referenced by either. – Jerry Murphy Dec 05 '14 at 15:27
1

You'd be much better off if you had a method like,

private double CalculateTotal()
{
    var price = 0.0;
    if (smallPizza.Checked)
        price += 9.0;
    if (mediumPizza.Checked)
        price += 12.0;
    if (mediumPizza.Checked)
        price += 14.0;
    if (pineapple.Checked)
        price += 0.5;
    if (margeritta.Checked)
        price += 1.0;
    if (pepperoni.Checked)
        price += 12.0;
    if (hawaiian.Checked)
        price += 3.0;
    if (salami.Checked)
        price += 0.5;
    if (goatsCheese.Checked)
        price += 0.5;
    if (chorizo.Checked)
        price += 0.5;

    return price;
}

which you called like this.

private void calculateTotal_Click(object sender, EventArgs e)
{
    this.priceBeforeVat = this.CalculateTotal()
    costOfPizza.Text = string.Format("{0:C2}", this.priceBeforeVat)
}

If you need to persist with updating a running total in each CheckBox.Checked handler you should consider that "un-checking" should substract from the total i.e.

private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
    const double SmallPizzaPrice = 9.0;

    if (smallPizza.Checked)
    {            
        this.pizzaTotal += SmallPizzaPrice;
    }
    else
    {
        this.pizzaTotal -= SmallPizzaPrice;
    }
}
Jodrell
  • 34,946
  • 5
  • 87
  • 124
  • its actually 23% here. This is for a college project and as we haven't started using OOP I'm supposed to do it the way I did (except it's supposed to work). I still don't understand where the strange numbers are coming from all the other prices add perfectly – Jerry Murphy Dec 05 '14 at 15:56
  • 1
    @JerryMurphy well, if you use the check handlers perhaps you need to subtract from the total when check boxes are unchecked. – Jodrell Dec 05 '14 at 16:01
  • @ Jodrell I think you might be on to something there. When the program starts small is selected. Is it possible to start the program with no radiobutton selected? – Jerry Murphy Dec 05 '14 at 16:09
  • subtracting 9 from both medium and large pizzas solves the problem thanks. – Jerry Murphy Dec 05 '14 at 16:12
  • If you could right you solution as an answer instead of a comment I can select it as the correct answer,thanks – Jerry Murphy Dec 05 '14 at 16:12
  • @JerryMurphy, like that? – Jodrell Dec 05 '14 at 16:48
  • That's perfect, even though I coded it slightly differently it used the same concept...The first radio button is automatically checked so it adds 9 to pizzaTotal as soon as the program starts, Thanks for your help. – Jerry Murphy Dec 05 '14 at 16:56