Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用C语言对自动交通灯进行编码_C#_Winforms_Visual Studio - Fatal编程技术网

C# 用C语言对自动交通灯进行编码

C# 用C语言对自动交通灯进行编码,c#,winforms,visual-studio,C#,Winforms,Visual Studio,我正在尝试使用WinForms应用程序在Visual C中编写一个红绿灯程序。基本上我想要的是三个面板,每一个都是一种颜色:红色、黄色和绿色。当一个面板“打开”时,其他面板将为灰色。我希望“打开”的面板每15秒更换一次 比如说,, 1默认情况下,红色为“开”,黄色和绿色为灰色。 2 15秒后,黄色为“开”,红色和绿色为灰色 3 15秒后,绿色为“开”,红色和黄色为灰色 4 15秒后,红色为“开”,黄色和绿色为灰色 我成功地编写了一个版本,在这个版本中,我可以通过按下按钮来改变颜色,但是,我不知道

我正在尝试使用WinForms应用程序在Visual C中编写一个红绿灯程序。基本上我想要的是三个面板,每一个都是一种颜色:红色、黄色和绿色。当一个面板“打开”时,其他面板将为灰色。我希望“打开”的面板每15秒更换一次

比如说,, 1默认情况下,红色为“开”,黄色和绿色为灰色。 2 15秒后,黄色为“开”,红色和绿色为灰色 3 15秒后,绿色为“开”,红色和黄色为灰色 4 15秒后,红色为“开”,黄色和绿色为灰色

我成功地编写了一个版本,在这个版本中,我可以通过按下按钮来改变颜色,但是,我不知道如何实现15秒计时器

以下是我当前的源代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Traffic_Light_Form
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            panel_Red.BackColor = Color.Red;
            panel_Yellow.BackColor = Color.Gray;
            panel_Green.BackColor = Color.Gray;

        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            if ((panel_Yellow.BackColor == Color.Gray) == (panel_Green.BackColor == Color.Gray))
            {
                panel_Red.BackColor = Color.Gray;
                panel_Yellow.BackColor = Color.Yellow;
            }

            else if (panel_Yellow.BackColor == Color.Yellow)
            {
                panel_Yellow.BackColor = Color.Gray;
                panel_Green.BackColor = Color.Green;
            }

            else
            {
                panel_Green.BackColor = Color.Gray;
                panel_Red.BackColor = Color.Red;
            }
        }
    }
}
第一个按钮将舞台设置为“红色”“灰色”“灰色”,单击第二个按钮后,舞台将变为“灰色”“黄色”“灰色”>“灰色”“灰色”“绿色”>“红色”“灰色”“灰色”,依此类推


我希望更改以15秒的间隔自动进行。

从Toolbax->Components拖动计时器,在表单上列出最后一项。转到“属性”窗口,将“间隔属性”设置为15000,以每15秒引发一次勾号事件。现在,在“属性”窗口上选择事件,然后双击勾号事件以生成正确的事件处理程序。请注意,默认情况下计时器为Enable=false,这意味着您必须从代码中将其打开。将此代码添加到应用程序中不要忘记标记答案:

public partial class Form1 : Form
{
    private List<Panel> _panels;
    private int _currentPanelIndex; 
    private List<Color> _colors;      

    public Form1()
    {
        InitializeComponent();
        _panels = new List<Panel> { panel_Red, panel_Yellow, panel_Green };
        _colors = new List<Color> {Color.Red,Color.Yellow,Color.Green};
        _currentPanelIndex = 0;

        timer1.Start();
    }

    private void UpdatePanels()
    {
        for (int index = 0; index < 3; index++)
        {
            if (index.Equals(_currentPanelIndex))
            {
                //current panel to be on
                _panels[index].BackColor = _colors[index];
            }
            else
            {
                //others are gray
                _panels[index].BackColor = Color.Gray;
            }
        }
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        //set current colors
        UpdatePanels();
        //move to next panel
        _currentPanelIndex++;
        //reset to start from first panel
        if (_currentPanelIndex.Equals(3))
        {
            _currentPanelIndex = 0;
        }
    }
}

您必须将计时器组件添加到表单中。设置适当的时间间隔并在Timer_Tick事件中进行检查。计时器:使计时器具有15000个时间间隔确保其设置为Enabled,然后使计时器代码中的整数增加1。如果int%3=0红色亮起,则int%2=0绿色亮起,否则黄色亮起