0

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.

7
  • do you mean automatically create the textboxes? Commented Mar 17, 2022 at 14:40
  • I already created the textboxes. I just want to go like an loop from textbox1 to textbox(lenght of the array) and write the value that is stored in the array. Commented Mar 17, 2022 at 14:42
  • Or if its easier to create the textboxes Commented Mar 17, 2022 at 14:43
  • I'm not sure I understand the question correctly. But if you want to "connect" the text of a Textbox with a variable in the .cs code you can use <Textbox Text="{Binding variable[i}"in the xaml file, where variable is your array and i your index. I hope to be proved helpful Commented Mar 17, 2022 at 14:44
  • he is using winform, not wpf. Commented Mar 17, 2022 at 14:45

3 Answers 3

1

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

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

Comments

1

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

Comments

0

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

1 Comment

Sorry, i for answering so late. Thanks it worked :D

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.