Hello, I was trying to build a Textbox name from a string. Textbox+index

Asked

Viewed 27 times

-4

    private void btnGravarNovosValores_Click_1(object sender, EventArgs e)
    {
        string descricao;
        string textBoxX;

        int i = 0;
        foreach (Object texto in lstTabelaProdutosPreco.Items)
        {
            descricao = texto.ToString();
            i++;
            textBoxX = ("textBox" + i.ToString());

            //MessageBox.Show(descricao + " " + textBox);

            TabelaAlterarProdutosCalibresValores tabelaAlterarProdutosCalibresValores = new TabelaAlterarProdutosCalibresValores();
            tabelaAlterarProdutosCalibresValores.idTabelaPrecos = Convert.ToInt32(lstNome.SelectedValue.ToString().Substring(0, 3));
            tabelaAlterarProdutosCalibresValores.idProduto = Convert.ToInt32(descricao.ToString().Substring(0, 4));
            tabelaAlterarProdutosCalibresValores.idCalibre = descricao.ToString().Substring(8, 5);
            tabelaAlterarProdutosCalibresValores.precoVenda = double.Parse(textBoxX.ToString().Trim());
            if (tabelaAlterarProdutosCalibresValores.UpDateDefault())
            {
                MessageBox.Show(lstNome.SelectedValue.ToString().Substring(0, 3) + descricao.ToString().Substring(0, 4) + descricao.ToString().Substring(8, 5) + textBoxX);
            }
        }
    }
  • 2

    I didn’t understand. Could I talk a little more in the body of the question? Just the title and this code didn’t tell me much

  • I can build Textbox names through a textbox cycle +i = textbox1, but I can’t use it as the name of the textbox tableAlteRodusCalibresValue = double. Parse(textBoxX.Tostring(). Trim());

  • "build the names" of textbox ?!?! try to be clearer young

  • string Description; string textBoxX; int i = 0; foreach (Object text in lstTabelaProductsPreco.Items) { Description = text.Tostring(); i++; textBoxX = ("textbox" + i.Tostring()); }

  • You want to access a textbox that is in your form by entering its name in a string. This ?

  • Welcome to Stackoverflow, to start make a [tour] and then see [Ask] and [Answer]. Always inform a minimal, complete and verifiable example

  • through the foreach cycle with you each turn get a description and build a textbox name. when I will use the textbox name

  • you are only asking the wrong question young, What you want is to access the Textbox on your screen dynamically, inside the loop. Ie by the string "textBox"+i.ToString() Voce wants access to control: string texto = textBox1.Text;

  • if I try to use the name created daigual a textbox (textboxX) in a messagebox appears the correct name, if I try to use the same Textboxx to insert the contents of the textbox in a database, from the error "input string with incorrect format"

  • that’s it, how do I get the string recognized as a textbox

  • Sorry is the 1 time I am using this site and do not know how to ask the questions

  • no problems, see if my answer help and access the tour indicated by Everson

Show 7 more comments

1 answer

3

You want to access several TextBox that exist in your Form, dynamically, inside the loop.

For this you will use the Method Find:

https://msdn.microsoft.com/en-us/library/system.windows.forms.control.controlcollection.find(v=vs.110). aspx

Example:

for (int i =0; i< 10; i++)
{

        Control[] controls = this.Controls.Find("textBox" + i.ToString(), true);
        if (controls.Length > 0)
        {
            TextBox txtBox = controls[0] as TextBox;
            if (txtBox != null)
            {
                tabelaAlterarProdutosCalibresValores.precoVenda = txtBox.Text; 
            }
        }
        else
        {
          //não achou o textbox
        }

 }
  • It Worked Obligated

  • Don’t forget to mark as reply @user9426886

Browser other questions tagged

You are not signed in. Login or sign up in order to post.