简体   繁体   中英

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

i have code that its working but its not as i would like.

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

i would like to use repeat structure like for or while, dont know if its possible.

For example, if i create more textbox/labels will be impossible have really big code.

See my 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 something like this be what you are looking for? If you have more labels just update iAcessorioContar and it will automatically check them as well as long as you name the labels incrementally.

private void cadeiaapagarcampos(TextBox _text, EventArgs e)
        {
            if (_text.Text == "")
            {
                int iAcessorioContar = 10;
                for (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;
                            }
                        }                    
                    }
                }
            }
        }

You could add the controls in pairs (label and textbox) to a list when you create them so you do not have to search the controls collection which can cost a lot of time.

You can then simply loop the list.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM