用C#的timer做了个红绿灯,可按按钮,可自动,可闪烁,可倒计时。这个程序很小啦,所以不怎么解释,贴了再说~~~
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace changIM
{
public partial class Form1 : Form
{
public int i, j;
public string redB;
public string greenB;
public string yellowB;
public Form1()
{
InitializeComponent();
this.pictureBox1.Load("default.jpg");
this.pictureBox2.Visible = false;
this.pictureBox3.Visible = false;
this.pictureBox4.Visible = false;
this.label1.Visible = false;
this.textBox1.Text = "3000";
this.textBox2.Text = "3000";
this.textBox3.Text = "3000";
j = 5;
}
private void red_Click(object sender, EventArgs e)
{
getRed();
}
private void yellow_Click(object sender, EventArgs e)
{
getYellow();
}
private void green_Click(object sender, EventArgs e)
{
getGreen();
}
public void getRed() {
this.pictureBox2.Load("red.jpg");
this.pictureBox2.Visible = true;
this.pictureBox3.Visible = false;
this.pictureBox4.Visible = false;
}
public void getYellow() {
this.pictureBox3.Load("yellow.jpg");
this.pictureBox2.Visible = false;
this.pictureBox4.Visible = false;
this.pictureBox3.Visible = true;
}
public void getGreen() {
this.pictureBox4.Load("green.jpg");
this.pictureBox3.Visible = false;
this.pictureBox2.Visible = false;
this.pictureBox4.Visible = true;
}
public void Initial() {
this.pictureBox2.Visible = false;
this.pictureBox3.Visible = false;
this.pictureBox4.Visible = false;
}
public void turn(int i) {
this.i = i;
if (i == 1)
{ this.timer1.Interval = Convert.ToInt32(redB); getRed(); }
if (i == 2||i==4)
{
this.timer1.Interval= 500; Initial();
}
if (i == 3||i==5)
{ this.timer1.Interval = 500; getRed(); }
if (i == 6)
{ this.timer1.Interval = Convert.ToInt32(yellowB); getYellow(); }
if (i == 7||i==9)
{
this.timer1.Interval = 500; Initial();
}
if (i == 8||i==10)
{
this.timer1.Interval = 500; getYellow();
}
if (i == 11) {
this.timer1.Interval = Convert.ToInt32(greenB); getGreen();
}
if (i == 12||i==14||i==16||i==18||i==20||i==22)
{ this.timer1.Interval = 500; Initial(); this.label1.Text = j.ToString(); j--; this.label1.Visible = true; }
if (i == 13 || i == 15 || i == 17 || i == 19 || i == 21||i==23)
{ this.timer1.Interval = 500; getGreen(); }
}
private void Form1_Load(object sender, EventArgs e)
{
timer1.Start();
}
private void timer1_Tick_1(object sender, EventArgs e)
{
if (i == 24)
{ i = 1; j = 5; this.label1.Visible = false; }
turn(i);
i++;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
this.redB = this.textBox1.Text;
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
this.yellowB = this.textBox2.Text;
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
this.greenB = this.textBox3.Text;
}
}
}
大概就是这样啦~~·