简体   繁体   English

从TextFile中读取每一行并将其添加到ListBox C#

[英]Reading Each line from a TextFile and adding it to a ListBox C#

private void button2_Click(object sender, EventArgs e)
{
     string[] lines = System.IO.File.ReadAllLines(this.textBox2.Text);
     foreach (string line in lines)
     {
          this.listProxy.Items.Add(lines);
     }
}

When I click the button it is not writing it out to the ListBox . 当我单击按钮时,它没有写到ListBox

How can I make it add each line in order to the ListBox ? 我如何才能将每行添加到ListBox

Your should use line instead lines 您应该使用line代替lines

this.listProxy.Items.Add(line);

look the code edited below: 查看下面编辑的代码:

private void button2_Click(object sender, EventArgs e)
{
    string[] lines = System.IO.File.ReadAllLines(this.textBox2.Text);
    foreach (string line in lines)
    {
        this.listProxy.Items.Add(line);
    }
}

First, for whta i see in the code provided you are getting text from textbox and not from a TextFile. 首先,我在代码中看到的是您从文本框而不是从TextFile获取文本。

Second, make sure that the TextFile you are going to use, contains information. 其次,确保要使用的TextFile包含信息。

Third, you could use a try catch block in order to detect errors and be able to correct them before sending this to production. 第三,您可以使用try catch块来检测错误并能够在将其发送到生产环境之前对其进行更正。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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