用timer做红绿灯的小程序

用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;
        }

       
      
    }
}
大概就是这样啦~~·

  • 3
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值