Visual studio c#计时器不工作
当按钮被点击时,我希望按钮的颜色在5秒后变为黑色,但我就是不能让它工作。我已经将计时器的间隔设置为5000,并在属性中启用为trueVisual studio c#计时器不工作,c#,visual-studio-2015,C#,Visual Studio 2015,当按钮被点击时,我希望按钮的颜色在5秒后变为黑色,但我就是不能让它工作。我已经将计时器的间隔设置为5000,并在属性中启用为true using System; using System.Drawing; using System.Windows.Forms; namespace WindowsFormsApplication4 { public partial class Form1 : Form { public Form1() {
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秒后
,需要将按钮1.BackColor
的分配放置在timer1\u Tick
事件处理程序中。另外,别忘了停止计时器的滴答声
private void timer1_Tick(object sender, EventArgs e)
{
button1.BackColor = Color.Black;
timer1.stop();
}
试试这个:
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();
}
}
您必须将按钮黑色背景色的触发器置于计时器的滴答声事件上。最佳解决方案是
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;
}
在timer_tick事件中写入颜色更改代码
private void timer1_Tick(object sender, EventArgs e)
{
button1.BackColor = Color.Black;
timer1.Enabled = false;
}
你用的是什么定时器?您应该使用windows.Forms.Timer。看起来您已经在设计时添加了它,请确保要延迟的操作在Tick事件中代码>放入勾号事件将在winforms计时器中变大,启用它并启动两者做相同的事情。我同意。这是为了确保计时器只会滴答一次:)将更新我的答案。