红绿灯的设计
时间:2011-02-23 来源:hfliyi
当单击“启动”按钮的时候,我们把Timer控件的Enabled的值设置为true,也就是启用Timer这个控件;
然后再Timer的Tick事件函数里面写如下代码:
View Codeint i =0;
private void timer1_Tick(object sender, EventArgs e)
{
if (i == 0)
{
this.red.BackColor = Color.Red;
this.yellow.BackColor = Color.Black;
this.green.BackColor = Color.Black;
i = 1;
}
else if (i == 1)
{
this.red.BackColor = Color.Black;
this.yellow.BackColor = Color.Yellow;
this.green.BackColor = Color.Black;
i = 2;
}
else if (i == 2)
{
this.red.BackColor = Color.Black;
this.yellow.BackColor = Color.Black;
this.green.BackColor = Color.Green;
i = 0;
}
}
另外我们在Form_Load事件函数里面写上如下代码,作用是在加载Form的时候,把各个PictureBox控件的背景颜色设置为黑色。
View Code1 private void Form1_Load(object sender, EventArgs e)
2 {
3 this.red.BackColor = Color.Black;
4 this.green.BackColor = Color.Black;
5 this.yellow.BackColor = Color.Black;
6 }
相关阅读 更多 +