[英]Return array values into listbox (C#)
我有一個從文本文件獲取其值的數組。 每行都是一個數組值。 我試圖使每個數組值進入列表框。 如
[0] - Hello
[1] - Goodbye
在列表框中,第一個選項是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();
這是列表框的代碼:
this.listBox1.Items.AddRange(new object[] {
// Values go here
// i want the arrays here, so that its like "hello", "goodbye",
});
非常感謝您的幫助。 我正在使用MS Visual Studio 2010。
您可以為列表框分配數據源:
this.listBox1.DataSource = object[];
HTH。
我尚未測試過,但我認為您可以這樣做:
this.listBox1.Items.AddRange(linea);
編輯:
剛剛測試過,效果很好! :)
編輯:只是意識到它不需要強制轉換
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.