简体   繁体   中英

ArgumentOutOfRangeException when writing C# List<string> to multiple textbox

How to put list to multiple TextBoxes? I have tried to use several way to do it. All of them can put only one value from List<string> into TextBox and show an error of System.ArgumentOutOfRangeException

Below is the code I have tried this far:

List<string> txtlist = new List<string>();
 for (int ix = 1; ix < 16; ix++)
{
   string test = command.Get(appendCommand);
   txtlist.Add(test);
   txt_1.Text = txtlist.ElementAt(0);  
   txt_2.Text = txtlist.ElementAt(1); //System.ArgumentOutOfRangeException
   txt_3.Text = txtlist.ElementAt(2);
   txt_4.Text = txtlist.ElementAt(3);
   ...
   txt_4.Text = txtlist.ElementAt(15);
}

List txtlist data show as below:

[0] "test1" [1] "test2" [2] "test3" .... [15] "test16"

I want to put list one by one to the textbox, please show me some example, thank you.

First build txtList and after that there are items to get. No more ArgumentOutOfRangeException

List<string> txtlist = new List<string>();
for (int i = 0; i < 16; i++)
{
   string test = command.Get(appendCommand);
   txtlist.Add(test);
}

txt_1.Text = txtlist.ElementAt(0);  
txt_2.Text = txtlist.ElementAt(1); 
txt_3.Text = txtlist.ElementAt(2);
txt_4.Text = txtlist.ElementAt(3);
...
txt_4.Text = txtlist.ElementAt(15);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM