将数组值返回到列表框(C#)

Jar*_*red 2 c# arrays listbox streamreader visual-studio

我有一个数组从文本文件中获取其值.每一行都是一个数组值.我试图让每个数组值进入列表框.如

[0] - Hello
[1] - Goodbye
Run Code Online (Sandbox Code Playgroud)

在列表框中,第一个选项是hello,第二个选项是再见等.

文本文件是分开的行上的Hello和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();
Run Code Online (Sandbox Code Playgroud)

这是我的列表框代码:

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

非常感谢帮助.我正在使用MS Visual Studio 2010.

Thi*_*hbk 5

您可以为Listbox分配DataSource:

this.listBox1.DataSource = object[];
Run Code Online (Sandbox Code Playgroud)

HTH.