2011-11-28 22 views
2

我有一个从文本文件中获取其值的数组。每一行都是一个数组值。我试图让每个数组值进入列表框。如将数组值返回到列表框(C#)

[0] - Hello 
[1] - Goodbye 

,并在列表框中,第一个选项会打招呼,第二个会再见等

文本文件是为你好,再见的分离式线

继承人我的代码从文本文件中的数组进行排序:

StreamReader sr = new StreamReader("text.txt"); 
int counter = 0; 
string line; 
string[] linea; 
linea = new string[100]; 
while ((line = sr.ReadLine()) != null) 
{ 
    linea[counter] = line; 
    counter++; 
} 
sr.Close(); 

这是我的列表框代码:

this.listBox1.Items.AddRange(new object[] { 
// Values go here 
// i want the arrays here, so that its like "hello", "goodbye", 
}); 

非常感谢帮助。 我使用微软的Visual Studio 2010中

回答

5

您可以为列表框指定数据源:

this.listBox1.DataSource = object[]; 

HTH。

1

我没有测试过,但是我觉得你可以这样做:

this.listBox1.Items.AddRange(linea); 

编辑:

只是测试它和它的伟大工程! :)

编辑:刚刚意识到,它不需要投