2016-05-02 11 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クラスです。すべてのテクスチャボックスを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