日期:2014-05-18  浏览次数:21209 次

计时器操作只执行了一次
C# code
for (j = 3; j > 0; j--)
                        {
                            
                            this.timerup.Start();
                            
                            
                        }


C# code
 private void timerup_Tick(object sender, EventArgs e)
        {



                
                this.ElevatorStyleLeft.Location = new Point(ElevatorStyleLeft.Location.X, ElevatorStyleLeft.Location.Y - 80);
               
                    this.timerup.Stop();
               
            
        }


为什么timer只执行了一次,怎样才能执行3次


------解决方案--------------------
int count=0;


if(count>3)
{
this.ElevatorStyleLeft.Location = new Point(ElevatorStyleLeft.Location.X, ElevatorStyleLeft.Location.Y - 80);
}else{
this.timerup.Stop();
}

------解决方案--------------------
private void timerup_Tick(object sender, EventArgs e)
{




this.ElevatorStyleLeft.Location = new Point(ElevatorStyleLeft.Location.X, ElevatorStyleLeft.Location.Y - 80);

this.timerup.Stop();//执行计时器的Stop()方法后,计时器将停止工作。


}

------解决方案--------------------
设置timer.autoreset = true.
不然你的timer执行一次以后就自动停止了。在你的事件处理函数里面计数,超过3次才调用timer.stop。其余时候不调用。
------解决方案--------------------
设置全局变量int i =0; 函数内每执行一次就i++

在函数内用
if(i>=3)
{
timer.Stop();
}