繁体   English   中英

Visual Studio C#计时器不起作用

[英]Visual studio c# timer does not work

单击按钮后,我希望按钮的颜色在5秒钟后变为黑色,但我无法使其正常工作。 我已经在属性中将计时器的间隔设置为5000,并将Enabled设置为true。

using System;
using System.Drawing;
using System.Windows.Forms;

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


        private void button1_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true;
            timer1.Start();

            button1.BackColor = Color.Black;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {

        }
    }
}

如果您希望颜色变为黑色并保持这种状态,则在5 seconds ,需要将button1.BackColor的分配button1.BackColortimer1_Tick事件处理程序中。 另外,不要忘记停止计时器的计时。

private void timer1_Tick(object sender, EventArgs e)
{
      button1.BackColor = Color.Black;
      timer1.stop();
}

最好的灵魂是

 private void button1_Click(object sender, EventArgs e)
        {            
            Timer MyTimer = new Timer();
            MyTimer.Interval = 4000; 
            MyTimer.Tick += new EventHandler(MyTimer_Tick);
            MyTimer.Start();

        }

        private void MyTimer_Tick(object sender, EventArgs e)
        {
            button1.BackColor = Color.Black;

        }

尝试这个:

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

    private void timer1_Tick(object sender, EventArgs e)
    {

        button1.BackColor = Color.Black;
        timer1.Stop();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        timer1.Enabled = true;
        timer1.Interval = 5000;
        timer1.Start();


    }
}

您必须将按钮的黑色背景色的触发器放置在计时器的刻度事件上。

在timer_tick事件中写入颜色更改代码

private void timer1_Tick(object sender, EventArgs e)
    {
      button1.BackColor = Color.Black;
      timer1.Enabled = false;
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM