2016-05-02 68 views
-2

我只是一個正在學習如何編寫代碼的普通人。我不喜歡代碼看起來馬虎,可以鞏固。有人說,我需要鞏固一些代碼,但我沒有找到如何做到這一點的信息。C#根據組合框選擇啓用/禁用多個文本框

我試圖啓用/禁用基於組合框選擇的多個文本框。

這裏是長和醜陋的代碼我試圖鞏固

private void comboBox2_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     int Combobox_Process_Selected_Index = comboBox2.SelectedIndex; 
     if (Combobox_Process_Selected_Index == 0) 
     { 
      textBox2.Enabled = false; 
      textBox3.Enabled = false; 
      textBox4.Enabled = false; 
      textBox5.Enabled = false; 
      textBox6.Enabled = false; 
      textBox7.Enabled = false; 
      textBox8.Enabled = false; 
      textBox9.Enabled = false; 
      textBox10.Enabled = false; 
      textBox11.Enabled = false; 
      textBox12.Enabled = false; 
      textBox13.Enabled = false; 
     } 
     if (Combobox_Process_Selected_Index == 1) 
     { 
      textBox2.Enabled = true; 
      textBox3.Enabled = false; 
      textBox4.Enabled = false; 
      textBox5.Enabled = false; 
      textBox6.Enabled = false; 
      textBox7.Enabled = false; 
      textBox8.Enabled = false; 
      textBox9.Enabled = false; 
      textBox10.Enabled = false; 
      textBox11.Enabled = false; 
      textBox12.Enabled = false; 
      textBox13.Enabled = false; 
     } 
     if (Combobox_Process_Selected_Index == 2) 
     { 
      textBox2.Enabled = true; 
      textBox3.Enabled = true; 
      textBox4.Enabled = false; 
      textBox5.Enabled = false; 
      textBox6.Enabled = false; 
      textBox7.Enabled = false; 
      textBox8.Enabled = false; 
      textBox9.Enabled = false; 
      textBox10.Enabled = false; 
      textBox11.Enabled = false; 
      textBox12.Enabled = false; 
      textBox13.Enabled = false; 
     } 
     if (Combobox_Process_Selected_Index == 3) 
     { 
      textBox2.Enabled = true; 
      textBox3.Enabled = true; 
      textBox4.Enabled = true; 
      textBox5.Enabled = false; 
      textBox6.Enabled = false; 
      textBox7.Enabled = false; 
      textBox8.Enabled = false; 
      textBox9.Enabled = false; 
      textBox10.Enabled = false; 
      textBox11.Enabled = false; 
      textBox12.Enabled = false; 
      textBox13.Enabled = false; 
     } 
     if (Combobox_Process_Selected_Index == 4) 
     { 
      textBox2.Enabled = true; 
      textBox3.Enabled = true; 
      textBox4.Enabled = true; 
      textBox5.Enabled = true; 
      textBox6.Enabled = false; 
      textBox7.Enabled = false; 
      textBox8.Enabled = false; 
      textBox9.Enabled = false; 
      textBox10.Enabled = false; 
      textBox11.Enabled = false; 
      textBox12.Enabled = false; 
      textBox13.Enabled = false; 
     } 
     if (Combobox_Process_Selected_Index == 5) 
     { 
      textBox2.Enabled = true; 
      textBox3.Enabled = true; 
      textBox4.Enabled = true; 
      textBox5.Enabled = true; 
      textBox6.Enabled = true; 
      textBox7.Enabled = false; 
      textBox8.Enabled = false; 
      textBox9.Enabled = false; 
      textBox10.Enabled = false; 
      textBox11.Enabled = false; 
      textBox12.Enabled = false; 
      textBox13.Enabled = false; 
     } 
     if (Combobox_Process_Selected_Index == 6) 
     { 
      textBox2.Enabled = true; 
      textBox3.Enabled = true; 
      textBox4.Enabled = true; 
      textBox5.Enabled = true; 
      textBox6.Enabled = true; 
      textBox7.Enabled = true; 
      textBox8.Enabled = false; 
      textBox9.Enabled = false; 
      textBox10.Enabled = false; 
      textBox11.Enabled = false; 
      textBox12.Enabled = false; 
      textBox13.Enabled = false; 
     } 
     if (Combobox_Process_Selected_Index == 7) 
     { 
      textBox2.Enabled = true; 
      textBox3.Enabled = true; 
      textBox4.Enabled = true; 
      textBox5.Enabled = true; 
      textBox6.Enabled = true; 
      textBox7.Enabled = true; 
      textBox8.Enabled = true; 
      textBox9.Enabled = false; 
      textBox10.Enabled = false; 
      textBox11.Enabled = false; 
      textBox12.Enabled = false; 
      textBox13.Enabled = false; 
     } 
     if (Combobox_Process_Selected_Index == 8) 
     { 
      textBox2.Enabled = true; 
      textBox3.Enabled = true; 
      textBox4.Enabled = true; 
      textBox5.Enabled = true; 
      textBox6.Enabled = true; 
      textBox7.Enabled = true; 
      textBox8.Enabled = true; 
      textBox9.Enabled = true; 
      textBox10.Enabled = false; 
      textBox11.Enabled = false; 
      textBox12.Enabled = false; 
      textBox13.Enabled = false; 
     } 
     if (Combobox_Process_Selected_Index == 9) 
     { 
      textBox2.Enabled = true; 
      textBox3.Enabled = true; 
      textBox4.Enabled = true; 
      textBox5.Enabled = true; 
      textBox6.Enabled = true; 
      textBox7.Enabled = true; 
      textBox8.Enabled = true; 
      textBox9.Enabled = true; 
      textBox10.Enabled = true; 
      textBox11.Enabled = false; 
      textBox12.Enabled = false; 
      textBox13.Enabled = false; 
     } 
     if (Combobox_Process_Selected_Index == 10) 
     { 
      textBox2.Enabled = true; 
      textBox3.Enabled = true; 
      textBox4.Enabled = true; 
      textBox5.Enabled = true; 
      textBox6.Enabled = true; 
      textBox7.Enabled = true; 
      textBox8.Enabled = true; 
      textBox9.Enabled = true; 
      textBox10.Enabled = true; 
      textBox11.Enabled = true; 
      textBox12.Enabled = false; 
      textBox13.Enabled = false; 
     } 
     if (Combobox_Process_Selected_Index == 11) 
     { 
      textBox2.Enabled = true; 
      textBox3.Enabled = true; 
      textBox4.Enabled = true; 
      textBox5.Enabled = true; 
      textBox6.Enabled = true; 
      textBox7.Enabled = true; 
      textBox8.Enabled = true; 
      textBox9.Enabled = true; 
      textBox10.Enabled = true; 
      textBox11.Enabled = true; 
      textBox12.Enabled = true; 
      textBox13.Enabled = false; 
     } 
     if (Combobox_Process_Selected_Index == 12) 
     { 
      textBox2.Enabled = true; 
      textBox3.Enabled = true; 
      textBox4.Enabled = true; 
      textBox5.Enabled = true; 
      textBox6.Enabled = true; 
      textBox7.Enabled = true; 
      textBox8.Enabled = true; 
      textBox9.Enabled = true; 
      textBox10.Enabled = true; 
      textBox11.Enabled = true; 
      textBox12.Enabled = true; 
      textBox13.Enabled = true; 
     } 

    } 

回答

0

你需要一個容器控件類有它的所有文本框。最簡單的是Panel類。只需將所有texbox添加到panel.Controls,您就可以循環訪問它們。您還必須手動設置選項卡才能啓用其中的一部分。

+0

我會試試看,謝謝你的幫助。 – 2bytes

1

好吧,我只好非常感謝你h8red。我從大量的垃圾代碼轉到少於10行的代碼。

下面是通過將文本框放置在面板中,然後基於組合框用戶輸入啓用/禁用它們的代碼合併。

 private void comboBox2_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     //set a counter to track textbox number and compare with combobox selection 
     int countTB = 0; 

     //grab combobox selection and place as an integer 
     int Combobox_Process_Selected_Index = comboBox2.SelectedIndex; 

     //this looks at each child control in my panel (named panel1) 
     foreach (Control child in panel1.Controls) 
     { 
      //this looks at each textbox individually and enables them until combo selection is reached and disables the rest 
      TextBox textBox = child as TextBox;    
      if (countTB < Combobox_Process_Selected_Index) 
      { 
       textBox.Enabled = true; 
      } 
      else 
       textBox.Enabled = false; 
      countTB++; 
     } 
    } 
+0

然後正式接受我的答案。 – h8red