我有代码可以工作,但不是我想要的。
在运行时我创建了许多新的文本框/标签,所以我不得不使用 10 个 ifs 来检查每个“未来”文本框是否有 textlength = 0 和 != null
我想使用 for 或 while 之类的重复结构,不知道是否可能。
例如,如果我创建更多的文本框/标签将不可能有真正的大代码。
查看我的代码:
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;
                        }
                    }
                }
            }
        }