Home > Back-end >  How can i write the text of my String array into multiple Textboxes
How can i write the text of my String array into multiple Textboxes

Time:03-18

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.

CodePudding user response:

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 ;
}

CodePudding user response:

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;
            }
  • Related