using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;namespace texttest
{
    public partial class Form1 : Form
    {
        List<string> alist = new List<string>();
        int times= 0;
        public Form1()
        {
            InitializeComponent();
        }        private void button1_Click(object sender, EventArgs e)
        {
            timer1.Interval = 1000;
            timer1.Enabled = true;
        }        private void timer1_Tick(object sender, EventArgs e)
        {
            readline();
        }
        private void readline()
        {
            richTextBox1.Text += alist[0] + "\r\n";
            alist.Remove(alist[0]);
            times++;
            if (times == 2)
            {
                timer1.Enabled =false ;
                MessageBox.Show("输出第二行暂停,点击button2继续");
            }
            if (alist.Count == 0)
                timer1.Enabled = false;
            
        }        private void button2_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true;
        }        private void Form1_Load(object sender, EventArgs e)
        {
            string line;
            StreamReader sr = new StreamReader("a.txt", Encoding.Default);
            while ((line = sr.ReadLine()) != null)
            {
                alist.Add(line);
            }
        }    }
}
启动调试后,发现第一次运行还是正常的,但是如果再次点击button1时,发现会出现一个对话框,上面写着“索引超出范围,必须为非负值并小于集合大小”,可以先调试一下试试~
不知道是什么地方出的问题,能不能修改一下,第二次甚至第三次点击后,不会在出现问题
不知道说清楚了没有...
调试一下试试吧~~

解决方案 »

  1.   

    把ReadLine()中的代码用try{}catch(){}包装一下
      

  2.   

    我调试了,没有出现你的问题,啊.
    你先确定你的a.txt在输出目录吗?里面有内容吗?
      

  3.   

            private void button1_Click(object sender, EventArgs e)
            {
                times = 0;
                alist.Clear();
                string line;
                StreamReader sr = new StreamReader("a.txt", Encoding.Default);
                while ((line = sr.ReadLine()) != null)
                {
                    alist.Add(line);
                }
                timer1.Interval = 1000;
                timer1.Enabled = true;
                        }将button1的click修改成上面的代码,就可以了,
    这是因为你的readline()函数对alist元素进行了删除造成的.
      

  4.   

    是不是如果你的a.txt没有内容的话,你button1_Click后面触发的alist[0]就是超出索引范围
      

  5.   

    或者你Form1_Load有问题,没有正确读取到文件内容
      

  6.   

    如果想在出现第三行后同时出现“输出第二行暂停,点击button2继续”,是不是在
    private void readline() 
            { 
                richTextBox1.Text += alist[0] + "\r\n"; 
                alist.Remove(alist[0]); 
                times++; 
                if (times == 2) 
                { 
                    timer1.Enabled =false ; 
                    MessageBox.Show("输出第二行暂停,点击button2继续"); 
                } 
                if (alist.Count == 0) 
                    timer1.Enabled = false; 
                 
            } 
    中在添加一个if就可以:
    private void readline() 
            { 
                richTextBox1.Text += alist[0] + "\r\n"; 
                alist.Remove(alist[0]); 
                times++; 
                if (times == 2) 
                { 
                    timer1.Enabled =false ; 
                    MessageBox.Show("输出第二行暂停,点击button2继续"); 
                } 
              if (alist.Count == 0) 
                    timer1.Enabled = false; 
                 
                 if (times == 3) 
                { 
                    timer1.Enabled =false ; 
                    MessageBox.Show("输出第三行暂停,点击button3继续"); 
                if (alist.Count == 0) 
                    timer1.Enabled = false; 
                              
            } 
      

  7.   

    private void readline() 
            { 
                richTextBox1.Text += alist[0] + "\r\n"; 
                alist.Remove(alist[0]); 
                times++; 
                if (times == 2) 
                { 
                    timer1.Enabled =false ; 
                    MessageBox.Show("输出第二行暂停,点击button2继续"); 
                } 
                if (alist.Count == 0) 
                    timer1.Enabled = false; 
                 if (times == 3) 
                { 
                    timer1.Enabled =false ; 
                    MessageBox.Show("输出第三行暂停,点击button3继续"); 
                } 
                if (alist.Count == 0) 
                    timer1.Enabled = false; 
                 
            } 如果想在出现第三行后同样会出现第二行后的效果,这么添加一个if可以不?
    如果不可以,怎么改动?