首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

计时器操作只执行了一次,该如何处理

2012-04-21 
计时器操作只执行了一次C# codefor (j 3 j 0 j--){this.timerup.Start()}C# code private void tim

计时器操作只执行了一次

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();
}

热点排行