1

I have a form with this code assigned to a button:

    TextBox[] tbxCantServ = new TextBox[1];
    int i;

    for (i = 0; i < tbxCantServ.Length; i++)
    {
        tbxCantServ[i] = new TextBox();
    }

    foreach (TextBox tbxActualCant in tbxCantServ)
    {
        tbxActualCant.Location = new Point(iHorizontal, iVertical);
        tbxActualCant.Visible = true;
        tbxActualCant.Width = 44;
        tbxActualCant.MaxLength = 4;
        this.Controls.Add(tbxActualCant);
        iVertical = iVertical + 35;
    }

And this code creates textboxes dynamically, one for every "button click", so I can have an "add" button to call it and the user can write a list of things that is not limited.

The question is: How can I assign these "textboxes.Text" to a string? They haven't got a name :S

something like:

string sAllBoxes = tbx1.Text + tbx2.Text + "..." + tbxN.Text;

Thanks!!

3 Answers 3

3

If your tbxCantServ is defined as local to a method, then you have to assign a Name to your TextBoxes like:

int counter = 0;
foreach (TextBox tbxActualCant in tbxCantServ)
{
    tbxActualCant.Location = new Point(iHorizontal, iVertical);
    tbxActualCant.Name = "tbx" + counter++;
    tbxActualCant.Visible = true;
    tbxActualCant.Width = 44;
    tbxActualCant.MaxLength = 4;
    this.Controls.Add(tbxActualCant);
    iVertical = iVertical + 35;
}

And later in some other method if you want to get the joined text then you can do:

string sAllBoxes = string.Join(",", this.Controls.OfType<TextBox>()
                                .Where(r => r.Name.StartsWith("tbx"))
                                .Select(r => r.Text));

But if you have tbxCantServ defined at class level then you can do:

string sAllBoxes = string.Join(",", tbxCantServ
                                     .Where(r=> r != null)
                                     .Select(r => r.Text));

In string.Join, you can replace , with an empty string or any string depending on your requirement.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks, I am trying with this but I get a syntax error with the: tbxCantServ .Where(r=> r != null) .Select(r => r.Text)); part. How can I correct it?
@user3108594, there shouldn't be any space before .Where and .Select. , Should be like: tbxCantServ.Where(r=> r != null).Select(r => r.Text));
I got cannot convert from 'System.Collections.Generic.IEnumerable<string>' to 'string[]'.
What is your current .Net framework ? I believe you are using .Net framework 3.5. Just add ToArray like tbxCantServ.Where(r=> r != null).Select(r => r.Text).ToArray());
Thanks, now it's working! I use 2.0 for compatibility, so I used the LinqBridge to call it :P
|
3

You can do it in the same way you created them.

Try this:

string sAllBoxes="";
foreach (TextBox tbxActualCant in tbxCantServ)
{
     sAllBoxes+=tbxActualCant.Text;
}

OR

Using a StringBuilder:

StringBuilder textBuilder = new StringBuilder();
foreach (TextBox tbxActualCant in tbxCantServ)
{
     textBuilder.Append(tbxActualCant.Text);
}
string allText = textBuilder.ToString();

1 Comment

If I understand correctly you need StringBuilder over string ;)
2

If you have access to your textbox array, you can easily do this:

string sAllBoxes = string.Join(" ", tbxCantServ.Select(x => x.Text));

If you don't then use Control collection of your Form, and give name to your textboxes so you can access them using this.Controls[txtBoxName].

If you just want to concatanate your texts without a separator, you can also use string.Concat method:

string sAllBoxes = string.Concat(tbxCantServ.Select(x => x.Text));

3 Comments

Why space in Join which is not specified by OP?
that "..." confused me.I thought he wants a separator (most likely), otherwise string.Concat can be used.
I guess that ... emphasizes he may have N number of items. 1,2,3... and so on upto n.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.