简体   繁体   中英

Why are the values still adding up after i clear the output label?

I'm not sure what is going on. I thought I had it set up to clear the output label at the end. Everytime I clear it, the program still remembers the previous number and adds to it. I honestly have no idea what is going on.

Also on a side note, how do I put set up radiobuttons to be used in the this method?

First coding class so i'm still kind of a beginner.

    private double oilandlube()
    {
        if (checkBox1.Checked == true)
        {
            Oil_change = 26;
        }
        if (checkBox2.Checked == true)
        {
            Lube_job = 18;
        }
        return Oil_change + Lube_job;
    }
    private void Oiltype ()
    {
        if (radioButton1.Checked)
        {
            Regular = 0;
        }
        if (radioButton2.Checked)
        {
            Mixed = 10;
        }
        else
        {
            Mixed = 0;
        }
        if (radioButton3.Checked)
        {
            Full_Synthetic = 18;
        }
        else
        {
            Full_Synthetic = 0;
        }
    }
    private void carwash()
    {
        if (radioButton4.Checked)
        {
            none = 0;
        }
        if (radioButton5.Checked)
        {
            complimentary = 0;
        }
        if (radioButton6.Checked)
        {
            Full_service = 6;
        }
        else
        {
            Full_service = 0;
        }
        if (radioButton7.Checked)
        {
            Premium = 9;
        }
        else
        {
            Premium = 0;
        }

    }
    private double flushes()
    {
        if (checkBox3.Checked == true)
        {
            Radiator_flush = 30;
        }

        if (checkBox4.Checked == true)
        {
            Transmission_flush = 80;
        }

        return Radiator_flush + Transmission_flush;
    }
    private double misc()
    {
        if (checkBox5.Checked == true)
        {
            inspection = 15;
        }

        if (checkBox6.Checked == true)
        {
            replace_muffler = 100;
        }

        if (checkBox7.Checked == true)
        {
            tire_rotation = 20;
        }
        return inspection + replace_muffler;
    }
    private double partsandlabor()
    {

       double.TryParse(textBox1.Text, out parts);
       double.TryParse(textBox2.Text, out labor);

       return parts + labor; 

    }

    private double tax()
    {
        return partsandlabor() * taxes;
    }



    private void summary()
    {
        service = oilandlube() + flushes() + misc();
        total_parts = partsandlabor();
        double total_tax = tax();
        double grand_total = service + total_parts + total_tax;

        label7.Text = service.ToString("c");
        label8.Text = total_parts.ToString("c");
        label9.Text = total_tax.ToString("c");
        label10.Text = grand_total.ToString("c");

    }


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

    private void button1_Click(object sender, EventArgs e)
    {
        oilandlube();
        carwash();
        flushes();
        misc();
        partsandlabor();
        summary();
    }
    private void ClearOilandlube()
    {
        checkBox1.Checked = false;
        checkBox2.Checked = false;
    }
    private void ClearMisc()
    {
        checkBox5.Checked = false;
        checkBox6.Checked = false;
        checkBox7.Checked = false;
    }
    private void ClearFlushes()
    {
        checkBox3.Checked = false;
        checkBox4.Checked = false;
    }

    private void ClearSummary()
    {
        label7.Text = "";
        label8.Text = "";
        label9.Text = "";
        label10.Text = "";
    }
    private void button2_Click(object sender, EventArgs e)
    {

        ClearOilandlube();
            ClearMisc();
            ClearFlushes();
            ClearSummary();



        radioButton1.Checked = false;
        radioButton2.Checked = false;
        radioButton3.Checked = false;
        radioButton4.Checked = false;
        radioButton5.Checked = false;
        radioButton6.Checked = false;
        radioButton7.Checked = false;
        textBox1.Text = "0";
        textBox2.Text = "0";

    }
}

}

When you clear the contents of your controls, you should also clear the values of the backing variables, so they don't retain their previous values. You should be able to just set them back to zero in your Clear methods.

For example, oil and lube might look like:

private void ClearOilandlube()
{
    checkBox1.Checked = false;
    checkBox2.Checked = false;
    Oil_change = 0;
    Lube_job = 0;
    Mixed = 0;
    Regular = 0;
    Full_Synthetic = 0;
}

It looks like you're holding state of some your variables globally so you can access them elsewhere.

Mixed = 10;

You'll have to reset that to some default value as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM