if statement - C# Windowsform - How made this code better (runtime created textbox and label) change too many ifs to repetition structure -


i have code working not like.

in runtime create many new textbox/labels had use 10 ifs check each "future" textbox have textlength = 0 , != null

i use repeat structure or while, dont know if possible.

for example, if create more textbox/labels impossible have big code.

see 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 && txtacessorio2.textlength == 0)                 {                     gpbcategoria.controls.remove(txtacessorio2);                     gpbcategoria.controls.remove(lblacessorio2);                     btnsalvar.focus();                     if (test != 1)                     {                         n--;                         t++;                         if (n >= 1 && n <= 10)                         {                             testeapagar();                             test = 1;                         }                     }                                     }                 if (txtacessorio3 != null && txtacessorio3.textlength == 0)                 {                      gpbcategoria.controls.remove(txtacessorio3);                     gpbcategoria.controls.remove(lblacessorio3);                     btnsalvar.focus();                     if (test != 1)                     {                         n--;                         t++;                         if (n >= 1 && n <= 10)                         {                             testeapagar();                             test = 1;                         }                     }                 }                 if (txtacessorio4 != null && txtacessorio4.textlength == 0)                 {                     gpbcategoria.controls.remove(txtacessorio4);                     gpbcategoria.controls.remove(lblacessorio4);                     btnsalvar.focus();                     if (test != 1)                     {                         n--;                         t++;                         if (n >= 1 && n <= 10)                         {                             testeapagar();                             test = 1;                         }                     }                 }                 if (txtacessorio5 != null && txtacessorio5.textlength == 0)                 {                     gpbcategoria.controls.remove(txtacessorio5);                     gpbcategoria.controls.remove(lblacessorio5);                     btnsalvar.focus();                     if (test != 1)                     {                         n--;                         t++;                         if (n >= 1 && n <= 10)                         {                             testeapagar();                             test = 1;                         }                     }                 }                 if (txtacessorio6 != null && txtacessorio6.textlength == 0)                 {                     gpbcategoria.controls.remove(txtacessorio6);                     gpbcategoria.controls.remove(lblacessorio6);                     btnsalvar.focus();                      if (test != 1)                     {                         n--;                         t++;                         if (n >= 1 && n <= 10)                         {                             testeapagar();                             test = 1;                         }                     }                 }                 if (txtacessorio7 != null && txtacessorio7.textlength == 0)                 {                     gpbcategoria.controls.remove(txtacessorio7);                     gpbcategoria.controls.remove(lblacessorio7);                     btnsalvar.focus();                      if (test != 1)                     {                         n--;                         t++;                         if (n >= 1 && n <= 10)                         {                             testeapagar();                             test = 1;                         }                     }                 }                 if (txtacessorio8 != null && txtacessorio8.textlength == 0)                 {                     gpbcategoria.controls.remove(txtacessorio8);                     gpbcategoria.controls.remove(lblacessorio8);                     btnsalvar.focus();                     if (test != 1)                     {                         n--;                         t++;                         if (n >= 1 && n <= 10)                         {                             testeapagar();                             test = 1;                         }                     }                 }                 if (txtacessorio9 != null && txtacessorio9.textlength == 0)                 {                     gpbcategoria.controls.remove(txtacessorio9);                     gpbcategoria.controls.remove(lblacessorio9);                     btnsalvar.focus();                     if (test != 1)                     {                         n--;                         t++;                         if (n >= 1 && n <= 10)                         {                             testeapagar();                             test = 1;                         }                     }                 }             }         } 

would looking for? if have more labels update iacessoriocontar , automatically check them long name labels incrementally.

private void cadeiaapagarcampos(textbox _text, eventargs e)         {             if (_text.text == "")             {                 int iacessoriocontar = 10;                 (int icontador = 2; icontador <= iacessoriocontar; icontador++) {                     label lblacessorio = (label)gpbcategoria.controls.find("lblacessorio"+icontador, false).firstordefault();                     textbox txtacessorio = (textbox)gpbcategoria.controls.find("txtacessorio"+icontador, false).firstordefault();                      if (txtacessorio != null && txtacessorio.textlength == 0)                     {                         gpbcategoria.controls.remove(txtacessorio);                         gpbcategoria.controls.remove(lblacessorio);                         btnsalvar.focus();                         if (test != 1) {                             n--;                             t++;                             if (n >= 1 && n <= 10)                             {                                 testeapagar();                                 test = 1;                             }                         }                                         }                 }             }         } 

Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -