I'm trying to get a couple labels to flash on a button click. With the current code, the first click works properly, and each click afterwards only does half the amount of blinking it should (to white and back to black). Any ideas on how to improve/fix this? Here's my current code:
private int counter;
System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
private void button1_Click_2(object sender, EventArgs e)
{
//Labels start out black, then play a sequence
//of changing to white and back to black twice
lb1.BackColor = Color.White;
lb2.BackColor = Color.White;
counter = 0;
timer.Interval = 300;
timer.Tick += new EventHandler(TimerElapsed);
timer.Enabled = true;
timer.Start();
}
void TimerElapsed(object sender, EventArgs e)
{
if (counter ==2)
{
timer.Stop();
timer.Enabled = false;
counter = 0;
}
else
{
if (lb2.BackColor == Color.Black)
{
lb1.BackColor = Color.White;
lb2.BackColor = Color.White;
}
else
{
lb1.BackColor = Color.Black;
lb2.BackColor = Color.Black;
}
counter += 1;
}
}