问题描述
各位高手请指点小弟:以下是我写的一个交通信号灯模拟程序,当点击按钮时启用Time控件,然后信号灯按指定的时间自动变换图片,可是不知道为什么我点击开始按钮图片直接就显示为绿灯的图片,请各位高手予以指点。usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Text;usingSystem.Windows.Forms;namespaceExample3{publicpartialclassFrmLand:Form{publicFrmLand(){InitializeComponent();}privatevoidbtnStart_Click(objectsender,EventArgse){timer1.Start();intnum1=5;intnum2=3;intnum3=3;if(num1==5){this.picLand.Image=Image.FromFile(Application.StartupPath+"/images/red.gif");num1--;}else{num1++;}if(num2==3){this.picLand.Image=Image.FromFile(Application.StartupPath+"/images/yellow.gif");num2--;}else{num2++;}if(num3==3){this.picLand.Image=Image.FromFile(Application.StartupPath+"/images/green.gif");num3--;}else{num3++;}}}}
解决方案
解决方案二:
timer1里的东西呢?if(num1==5){this.picLand.Image=Image.FromFile(Application.StartupPath+"/images/red.gif");num1--;}else{num1++;}if(num2==3){this.picLand.Image=Image.FromFile(Application.StartupPath+"/images/yellow.gif");num2--;}else{num2++;}if(num3==3){this.picLand.Image=Image.FromFile(Application.StartupPath+"/images/green.gif");num3--;}else{num3++;}怎么说这个也要放在定时器里,没仔细看合不合逻辑
解决方案三:
同上程序逻辑没看。主要你那些判断可以写在一个方法里用timer调用。timer写进那个事件。
解决方案四:
btnStart_Click事件中只是让timer开始计时,逻辑也该在timer1_tick事件中实现staticinttick=0;privatevoidtimer1_Tick(objectsender,EventArgse){tick++;if(tick>=0&&tick<=5){this.picLand.Image=Image.FromFile(Application.StartupPath+"/images/red.gif");}if(tick>5&&tick<=8){this.picLand.Image=Image.FromFile(Application.StartupPath+"/images/green.gif");}if(tick>8&&tick<=11){this.picLand.Image=Image.FromFile(Application.StartupPath+"/images/yellow.gif");}if(tick>11)tick=0;}
解决方案五:
kankan
解决方案六:
UPTIMER???