title: C# Windowsform - How made this code better (runtime created textbox and label) change too many ifs to repetition structure
body: <p>i have code that its working but its not as i would like.</p>
  
  <p>in runtime i create many new textbox/labels so i had to use 10 ifs to check each "future" textbox have textlength = 0 and != null</p>
  
  <p>i would like to use repeat structure like for or while, dont know if its possible.</p>
  
  <p>For example, if i create more textbox/labels will be impossible have really big code.</p>
  
  <p>See my code:</p>
  
  <pre><code>private void cadeiaapagarcampos(TextBox _text, EventArgs e)
          {           
              if (_text.Text == "")
              {
                  Label lblAcessorio2 = (Label)gpbCategoria.Controls.Find("lblAcessorio2", false).FirstOrDefault();
                  TextBox txtAcessorio2 = (TextBox)gpbCategoria.Controls.Find("txtAcessorio2", false).FirstOrDefault();
                  Label lblAcessorio3 = (Label)gpbCategoria.Controls.Find("lblAcessorio3", false).FirstOrDefault();
                  TextBox txtAcessorio3 = (TextBox)gpbCategoria.Controls.Find("txtAcessorio3", false).FirstOrDefault();
                  Label lblAcessorio4 = (Label)gpbCategoria.Controls.Find("lblAcessorio4", false).FirstOrDefault();
                  TextBox txtAcessorio4 = (TextBox)gpbCategoria.Controls.Find("txtAcessorio4", false).FirstOrDefault();
                  Label lblAcessorio5 = (Label)gpbCategoria.Controls.Find("lblAcessorio5", false).FirstOrDefault();
                  TextBox txtAcessorio5 = (TextBox)gpbCategoria.Controls.Find("txtAcessorio5", false).FirstOrDefault();
                  Label lblAcessorio6 = (Label)gpbCategoria.Controls.Find("lblAcessorio6", false).FirstOrDefault();
                  TextBox txtAcessorio6 = (TextBox)gpbCategoria.Controls.Find("txtAcessorio6", false).FirstOrDefault();
                  Label lblAcessorio7 = (Label)gpbCategoria.Controls.Find("lblAcessorio7", false).FirstOrDefault();
                  TextBox txtAcessorio7 = (TextBox)gpbCategoria.Controls.Find("txtAcessorio7", false).FirstOrDefault();
                  Label lblAcessorio8 = (Label)gpbCategoria.Controls.Find("lblAcessorio8", false).FirstOrDefault();
                  TextBox txtAcessorio8 = (TextBox)gpbCategoria.Controls.Find("txtAcessorio8", false).FirstOrDefault();
                  Label lblAcessorio9 = (Label)gpbCategoria.Controls.Find("lblAcessorio9", false).FirstOrDefault();
                  TextBox txtAcessorio9 = (TextBox)gpbCategoria.Controls.Find("txtAcessorio9", false).FirstOrDefault();
                  Label lblAcessorio10 = (Label)gpbCategoria.Controls.Find("lblAcessorio10", false).FirstOrDefault();
                  TextBox txtAcessorio10 = (TextBox)gpbCategoria.Controls.Find("txtAcessorio10", false).FirstOrDefault();
  
                  if (txtAcessorio2 != null &amp;&amp; txtAcessorio2.TextLength == 0)
                  {
                      gpbCategoria.Controls.Remove(txtAcessorio2);
                      gpbCategoria.Controls.Remove(lblAcessorio2);
                      btnSalvar.Focus();
                      if (test != 1)
                      {
                          n--;
                          t++;
                          if (n &gt;= 1 &amp;&amp; n &lt;= 10)
                          {
                              testeapagar();
                              test = 1;
                          }
                      }                    
                  }
                  if (txtAcessorio3 != null &amp;&amp; txtAcessorio3.TextLength == 0)
                  {
  
                      gpbCategoria.Controls.Remove(txtAcessorio3);
                      gpbCategoria.Controls.Remove(lblAcessorio3);
                      btnSalvar.Focus();
                      if (test != 1)
                      {
                          n--;
                          t++;
                          if (n &gt;= 1 &amp;&amp; n &lt;= 10)
                          {
                              testeapagar();
                              test = 1;
                          }
                      }
                  }
                  if (txtAcessorio4 != null &amp;&amp; txtAcessorio4.TextLength == 0)
                  {
                      gpbCategoria.Controls.Remove(txtAcessorio4);
                      gpbCategoria.Controls.Remove(lblAcessorio4);
                      btnSalvar.Focus();
                      if (test != 1)
                      {
                          n--;
                          t++;
                          if (n &gt;= 1 &amp;&amp; n &lt;= 10)
                          {
                              testeapagar();
                              test = 1;
                          }
                      }
                  }
                  if (txtAcessorio5 != null &amp;&amp; txtAcessorio5.TextLength == 0)
                  {
                      gpbCategoria.Controls.Remove(txtAcessorio5);
                      gpbCategoria.Controls.Remove(lblAcessorio5);
                      btnSalvar.Focus();
                      if (test != 1)
                      {
                          n--;
                          t++;
                          if (n &gt;= 1 &amp;&amp; n &lt;= 10)
                          {
                              testeapagar();
                              test = 1;
                          }
                      }
                  }
                  if (txtAcessorio6 != null &amp;&amp; txtAcessorio6.TextLength == 0)
                  {
                      gpbCategoria.Controls.Remove(txtAcessorio6);
                      gpbCategoria.Controls.Remove(lblAcessorio6);
                      btnSalvar.Focus(); 
                      if (test != 1)
                      {
                          n--;
                          t++;
                          if (n &gt;= 1 &amp;&amp; n &lt;= 10)
                          {
                              testeapagar();
                              test = 1;
                          }
                      }
                  }
                  if (txtAcessorio7 != null &amp;&amp; txtAcessorio7.TextLength == 0)
                  {
                      gpbCategoria.Controls.Remove(txtAcessorio7);
                      gpbCategoria.Controls.Remove(lblAcessorio7);
                      btnSalvar.Focus(); 
                      if (test != 1)
                      {
                          n--;
                          t++;
                          if (n &gt;= 1 &amp;&amp; n &lt;= 10)
                          {
                              testeapagar();
                              test = 1;
                          }
                      }
                  }
                  if (txtAcessorio8 != null &amp;&amp; txtAcessorio8.TextLength == 0)
                  {
                      gpbCategoria.Controls.Remove(txtAcessorio8);
                      gpbCategoria.Controls.Remove(lblAcessorio8);
                      btnSalvar.Focus();
                      if (test != 1)
                      {
                          n--;
                          t++;
                          if (n &gt;= 1 &amp;&amp; n &lt;= 10)
                          {
                              testeapagar();
                              test = 1;
                          }
                      }
                  }
                  if (txtAcessorio9 != null &amp;&amp; txtAcessorio9.TextLength == 0)
                  {
                      gpbCategoria.Controls.Remove(txtAcessorio9);
                      gpbCategoria.Controls.Remove(lblAcessorio9);
                      btnSalvar.Focus();
                      if (test != 1)
                      {
                          n--;
                          t++;
                          if (n &gt;= 1 &amp;&amp; n &lt;= 10)
                          {
                              testeapagar();
                              test = 1;
                          }
                      }
                  }
              }
          }
  </code></pre>