2017-04-19 31 views
0

我不确定发生了什么事。我认为我已经设置了最后清除输出标签。每次我清除它,该程序仍然记住以前的号码并添加到它。我真的不知道发生了什么。为什么在清除输出标签后,这些值仍然相加?

另请注意,如何在此方法中设置单选按钮?

第一编码类,所以我仍然是一个初学者。

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"; 

    } 
} 

}

回答

1

当您清除控件的内容,你也应该清楚的后盾变量的值,所以它们不保留其先前的值。你应该能够在你的Clear方法中将它们设置回零。

例如,油和润滑油可能看起来像:

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

谢谢,它似乎现在工作。 –

0

它看起来像你拿着你的一些变量的状态,全球,所以你可以在其他地方访问它们。

Mixed = 10; 

你必须是重置一些默认值,以及。

+0

这些用于单选按钮。我目前使用的所有值都设置为0. –

+0

我明白了,感谢您的帮助。 –

相关问题