Grover1221
Grover1221

Reputation: 17

How can i write the text of my String array into multiple Textboxes

i want to know how i can write the text of my String Array in multiple textboxes. For example i have an array of the length 5 so it should write inside the first five textboxes the text that is stored. The maximum is 14 textboxes that can be written. The string is filled with filenames that i read out of the given path and i want to display every filename in an textbox so the user can select which one he wants to use.

Upvotes: 0

Views: 161

Answers (3)

Sebastian
Sebastian

Reputation: 39

If you want the user to select a line of text, i would recommend to use a ListBox. The array should also be a List of strings.

The List for string would look like that:

List<string> texts = new List<string>();

Use to add sth to the List:

texts.Add("some text");

Finally to place every item in the list, use a foreach loop:

foreach (string item in texts)
{
    listBox1.Items.Add(item);
}

Let me know if it works

Upvotes: 0

Lei Yang
Lei Yang

Reputation: 4335

Can you try this code, using some linq.

Assumption

Your textboxes are directly put in Form, no other panels.

            foreach (var pair in strings.Take(14).Zip(
                this.Controls.OfType<TextBox>()))
            {
                pair.Second.Text = pair.First;
            }

Upvotes: 1

JonasH
JonasH

Reputation: 36361

Create a list/array of the textboxes

myTextboxes = new []{
   textbox1,
   textbox2,
    ....
}

and use .Zip to combine the lists so they can be looped over:

foreach(var (myTextbox, myString ) in myTextboxes.Zip(myStrings){
    myTextbox.Text = myString ;
}

Upvotes: 1

Related Questions