C#在组合框下面的if仅适用于第一个条件,而不适用于第二个条件。任何想法为什么?

问题描述:

组合框当我选择独立时它确实工作,但在此之后不适用第二个条件(零售)。我可以选择零售,但没有任何变化的形式。看起来只有第一个IF被识别,而不是第二个。任何想法如何解决代码?谢谢C#在组合框下面的if仅适用于第一个条件,而不适用于第二个条件。任何想法为什么?

 if (comboBox1.Text=="Standalone") 
     { 

      this.textBox4.BackColor = Color.Gray; 
      this.textBox4.Enabled = false; 


      this.checkBox1.BackColor = Color.Gray; 
      this.checkBox1.Enabled = false; 

      this.checkBox2.BackColor = Color.Gray; 
      this.checkBox2.Enabled = false; 

      this.checkBox3.BackColor = Color.Gray; 
      this.checkBox3.Enabled = false; 


      this.textBox6.BackColor = Color.Gray; 
      this.textBox6.Enabled = false; 

      this.textBox7.BackColor = Color.Gray; 
      this.textBox7.Enabled = false; 

      this.textBox8.BackColor = Color.Gray; 
      this.textBox8.Enabled = false; 



      this.checkBox7.BackColor = Color.Gray; 
      this.checkBox7.Enabled = false; 

      this.checkBox6.BackColor = Color.Gray; 
      this.checkBox6.Enabled = false; 

      this.checkBox4.BackColor = Color.Gray; 
      this.checkBox4.Enabled = false; 


      this.textBox12.BackColor = Color.Gray; 
      this.textBox12.Enabled = false; 

      this.textBox11.BackColor = Color.Gray; 
      this.textBox11.Enabled = false; 

      this.textBox9.BackColor = Color.Gray; 
      this.textBox9.Enabled = false; 


      if (comboBox1.Text =="Retail") 
      {     


      this.checkBox1.BackColor = Color.Gray; 
      this.checkBox1.Enabled = false; 

      this.checkBox2.BackColor = Color.Gray; 
      this.checkBox2.Enabled = false; 

      this.checkBox3.BackColor = Color.Gray; 
      this.checkBox3.Enabled = false; 


      this.checkBox9.BackColor = Color.Gray; 
      this.checkBox9.Enabled = false; 

      this.textBox6.BackColor = Color.Gray; 
      this.textBox6.Enabled = false; 

      this.textBox7.BackColor = Color.Gray; 
      this.textBox7.Enabled = false; 

      this.textBox8.BackColor = Color.Gray; 
      this.textBox8.Enabled = false; 

      this.textBox9.BackColor = Color.Gray; 
      this.textBox9.Enabled = false; 



      this.checkBox7.BackColor = Color.Gray; 
      this.checkBox7.Enabled = false; 

      this.checkBox6.BackColor = Color.Gray; 
      this.checkBox6.Enabled = false; 

      this.checkBox4.BackColor = Color.Gray; 
      this.checkBox4.Enabled = false; 

      this.checkBox10.BackColor = Color.Gray; 
      this.checkBox10.Enabled = false; 




      this.textBox12.BackColor = Color.Gray; 
      this.textBox12.Enabled = false; 

      this.textBox11.BackColor = Color.Gray; 
      this.textBox11.Enabled = false; 

      this.textBox9.BackColor = Color.Gray; 
      this.textBox9.Enabled = false; 

      this.textBox15.BackColor = Color.Gray; 
      this.textBox15.Enabled = false; 


      this.textBox14.BackColor = Color.Gray; 
      this.textBox14.Enabled = false; 

     } 

    } 
+0

你嵌套第二个'if'。你究竟在做什么?如果你解释你的情况,你可能会得到更好的帮助。 –

+0

就是如果combobox1值是独立做某事,如果combobox1值零售做某事,如果combobox1值是整体做某事等等.. – Diegoctn

+0

也许我应该使用开关,但在这种情况下,我应该把开关?内部私人无效comboBox1_SelectedIndexChanged(对象发件人,EventArgs e)? – Diegoctn

您需要知道if循环的基本流程。 如果

if(condition==true){ 
    if(condition_2==true){ 
     //Reaches this if both are true 
    } 
} 

这是如果序列

if(Condition==true){ 
    //reaches if condition is true 
} 
if(Condition_2==true){ 
    //reaches if condition 2 is true 
} 

这是否则,如果

if(Condition==true){ 
    //reaches if condition is true 
} 
else if(Condition_2==true){ 
    //reaches if condition 2 is true and condition is false 
} 

我相信你需要使用else if为你的情况这是嵌套。您也可以使用switch

你把错误的条件,如果(comboBox1.Text == “零售”){}如果在(comboBox1.Text == “独立”){}

下面的代码是正确的答案。

if (comboBox1.Text=="Standalone") 
    { 

     this.textBox4.BackColor = Color.Gray; 
     this.textBox4.Enabled = false; 


     this.checkBox1.BackColor = Color.Gray; 
     this.checkBox1.Enabled = false; 

     this.checkBox2.BackColor = Color.Gray; 
     this.checkBox2.Enabled = false; 

     this.checkBox3.BackColor = Color.Gray; 
     this.checkBox3.Enabled = false; 


     this.textBox6.BackColor = Color.Gray; 
     this.textBox6.Enabled = false; 

     this.textBox7.BackColor = Color.Gray; 
     this.textBox7.Enabled = false; 

     this.textBox8.BackColor = Color.Gray; 
     this.textBox8.Enabled = false; 



     this.checkBox7.BackColor = Color.Gray; 
     this.checkBox7.Enabled = false; 

     this.checkBox6.BackColor = Color.Gray; 
     this.checkBox6.Enabled = false; 

     this.checkBox4.BackColor = Color.Gray; 
     this.checkBox4.Enabled = false; 


     this.textBox12.BackColor = Color.Gray; 
     this.textBox12.Enabled = false; 

     this.textBox11.BackColor = Color.Gray; 
     this.textBox11.Enabled = false; 

     this.textBox9.BackColor = Color.Gray; 
     this.textBox9.Enabled = false; 

} 
     else if (comboBox1.Text =="Retail") 
     {     


     this.checkBox1.BackColor = Color.Gray; 
     this.checkBox1.Enabled = false; 

     this.checkBox2.BackColor = Color.Gray; 
     this.checkBox2.Enabled = false; 

     this.checkBox3.BackColor = Color.Gray; 
     this.checkBox3.Enabled = false; 


     this.checkBox9.BackColor = Color.Gray; 
     this.checkBox9.Enabled = false; 

     this.textBox6.BackColor = Color.Gray; 
     this.textBox6.Enabled = false; 

     this.textBox7.BackColor = Color.Gray; 
     this.textBox7.Enabled = false; 

     this.textBox8.BackColor = Color.Gray; 
     this.textBox8.Enabled = false; 

     this.textBox9.BackColor = Color.Gray; 
     this.textBox9.Enabled = false; 



     this.checkBox7.BackColor = Color.Gray; 
     this.checkBox7.Enabled = false; 

     this.checkBox6.BackColor = Color.Gray; 
     this.checkBox6.Enabled = false; 

     this.checkBox4.BackColor = Color.Gray; 
     this.checkBox4.Enabled = false; 

     this.checkBox10.BackColor = Color.Gray; 
     this.checkBox10.Enabled = false; 




     this.textBox12.BackColor = Color.Gray; 
     this.textBox12.Enabled = false; 

     this.textBox11.BackColor = Color.Gray; 
     this.textBox11.Enabled = false; 

     this.textBox9.BackColor = Color.Gray; 
     this.textBox9.Enabled = false; 

     this.textBox15.BackColor = Color.Gray; 
     this.textBox15.Enabled = false; 


     this.textBox14.BackColor = Color.Gray; 
     this.textBox14.Enabled = false; 

    }