2016-05-02 21 views
-2

Ich bin nur ein gewöhnlicher Typ, der lernt, wie man programmiert. Ich mag es nicht, wenn Code schlampig aussieht und konsolidiert werden kann. Nachdem ich gesagt habe, dass ich etwas Code konsolidieren muss, habe ich keine Informationen dazu gefunden.C# Aktivieren/Deaktivieren mehrerer Textfelder basierend auf der Auswahl von Kombinationsfeldern

Ich versuche, mehrere Textfeld basierend auf Combobox-Auswahl zu aktivieren/deaktivieren.

Hier ist die lange und hässliche Code I

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

    } 

Antwort

0

Sie benötigen einen Containersteuerklasse zu konsolidieren versuche alle darin die Textfelder zu haben. Die einfachste ist Panel Klasse. Fügen Sie einfach alle Texboxen zu panel.Controls hinzu und Sie können auf sie in einer Schleife zugreifen. Sie müssen auch eine Registerkarte manuell festlegen, um nur einige davon zu aktivieren.

+0

Ich werde das versuchen, danke für die Hilfe. – 2bytes

1

Okay, ich muss nur vielen Dank sagen h8red. Ich ging von einer riesigen Menge von Junk-Code zu weniger als 10 Zeilen Code.

Hier ist die resultierende Konsolidierung von Code durch Platzieren der Textfelder in einem Panel und dann aktivieren/deaktivieren Sie sie auf der Grundlage der Benutzereingabe Combobox.

 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

Formelle Annahme meiner Antwort würde dann geschätzt werden. – h8red