[英]How to get a Listbox to read to a Listbox
我正在嘗試打開任何選定的文本文件,並將輸入的文本發送到列表框...最初,我為一個文本框編寫了此代碼,該代碼現在非常有用,因為我將其轉換為列表框,它似乎沒有太大作用。 我保留了默認的項目名稱,以便更好地了解發生了什么。
private void button1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
label1.Text = openFileDialog1.FileName;
listBox1.Items.Add = File.ReadAllText(label1.Text);
}
}
嘗試這個 :
listBox1.Items.AddRange(File.ReadLines(label1.Text).ToArray());
listBox1.Items.AddRange(File.ReadAllLines(label1.Text));
.Add()
是一種方法,您將其.Add()
屬性。
請嘗試以下代碼:
listBox1.Items.Add(File.ReadAllText(label1.text));
private void button1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
label1.Text = openFileDialog1.FileName;
//till here the same
//open filestream
System.IO.StreamReader file = new System.IO.StreamReader(openFileDialog1.FileName);
//loop trough lines
while ((line = file.ReadLine()) != null)
{
//add line to listbox
listBox1.Items.Add ( line);
}
}
}
string[] lines = File.ReadLines("SomeFile.txt").ToArray();
foreach (var line in lines)
{
listBox1.Items.Add(line);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.