关于winform的问题,如何解决?

设计winform通过定时器每五秒读取txt文件中的数据显示到textbox控件中,达到遍历数据的目的

img

img

 private void button1_Click(object sender, EventArgs e)
        {
            
            timer2.Start();
            timer2.Interval = 5000;
            timer2.Enabled = true;
            timer2.Tick += new EventHandler(timer2Eventprocessor);
}

public void timer2Eventprocessor(object sender, EventArgs e)
        {
            string path = @"C:\Users\sun\Desktop\Xzuobiao.txt";
            StreamReader sr = new StreamReader(path, Encoding.Default);
            String line;
            line = sr.ReadLine();
            textBox1.Text = "";
            textBox1.Text += line + "\r\n";
        }

每次运行只会读取到第一个数据0,不会往下读取

private string[] lines = new string[] {}; 
private int curr = 0;
private void button1_Click(object sender, EventArgs e)
        {
            lines = System.IO.File.ReadAllLines(@"C:\Users\sun\Desktop\Xzuobiao.txt");
            timer2.Interval = 5000;
            timer2.Tick += new EventHandler(timer2Eventprocessor);
            timer2.Enabled = true;
}
 
public void timer2Eventprocessor(object sender, EventArgs e)
        {
            if (curr < lines.Count())
            {
                if (textBox1.Text != "") textBox1.Text += "\r\n";
                textBox1.Text += lines[curr++];                
            }
        }
 

不知道你这个问题是否已经解决, 如果还没有解决的话:

      此设置只需要更改一个属性即可完成:MaxDropDownItems 想要显示几条就改为几条就可以了


如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^