两个textbox 一个button  点击button的时候  开始运行,但是textbox2 却只动态显示了奇数次namespace TimeTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
     
        static int i = 0;     
        private void timer1_Tick(object sender, EventArgs e)
        {
            
          
            
            if (i <= 10)
            {
               
                textBox2.Text = ("正在运行第"+i+"次");
                i = i + 1;
               
            }
            else
            {
   
                timer1.Enabled = false;
                textBox1.Text = "定时器已经停止"; 
                
            }
           
        }                private void button1_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true;
            timer1.Tick+=new EventHandler(timer1_Tick);
            textBox1.Text  = ("定时器1已经启动");
        }          
    }
}

解决方案 »

  1.   

    我本来的意图是想 textbox2 能够显示每运行一次的变化。。timer1.Interval 设置的1000毫秒。但是却只看到了这样的结果:
    正在运行第1次
    正在运行第3次
    正在运行第5次
    正在运行第7次
      

  2.   

    是不是太快了你看不清?
    挂起1秒再看if (i <= 10)
                {
                   
                    textBox2.Text = ("正在运行第"+i+"次");
                    i = i + 1;
                   System.Threading.Thread.Sleep(1000);
                }
      

  3.   

    我测试,没问题,估计你的timer已经绑定了事件把,然你又一次绑定了,这样就会执行两次,i+2了,你看看timer的事件列表中是不是已经指定了