timer problem
-
Evening all ! I've created a new timer in a form's constructor to make a button on the form flash. For some reason the program is stepping through the code for the timer tick ONCE and thats it ? Would appreciate if someone could take a look at it and tell me whats wrong, it's porbably something trivial and i've just been looking at the code too long now ! Thanks in advance Neil
public Form1()
{
InitializeComponent();buttonTimer = new Timer(); buttonTimer.Tick += new EventHandler(button\_Tick); buttonTimer.Interval = (50); populate.BackColor = Color.Black; populate.ForeColor = Color.Orange; buttonTimer.Start(); } void button\_Tick(object sender, EventArgs e) { if (populate.BackColor == Color.Orange) { populate.BackColor = Color.Black; populate.ForeColor = Color.Orange; } if (populate.BackColor == Color.Black) { populate.BackColor = Color.Orange; populate.ForeColor = Color.Black; } }
}
-
Evening all ! I've created a new timer in a form's constructor to make a button on the form flash. For some reason the program is stepping through the code for the timer tick ONCE and thats it ? Would appreciate if someone could take a look at it and tell me whats wrong, it's porbably something trivial and i've just been looking at the code too long now ! Thanks in advance Neil
public Form1()
{
InitializeComponent();buttonTimer = new Timer(); buttonTimer.Tick += new EventHandler(button\_Tick); buttonTimer.Interval = (50); populate.BackColor = Color.Black; populate.ForeColor = Color.Orange; buttonTimer.Start(); } void button\_Tick(object sender, EventArgs e) { if (populate.BackColor == Color.Orange) { populate.BackColor = Color.Black; populate.ForeColor = Color.Orange; } if (populate.BackColor == Color.Black) { populate.BackColor = Color.Orange; populate.ForeColor = Color.Black; } }
}
Instead of this:
buttonTimer.Tick += new EventHandler(button_Tick);
Try this:
buttonTimer.Elapsed += new EventHandler(button_Elapsed);
Don't know if it makes any difference, but it's worth a try.
Kristian Sixhoej sixhoej.net - forums.sixhoej.net
-
Evening all ! I've created a new timer in a form's constructor to make a button on the form flash. For some reason the program is stepping through the code for the timer tick ONCE and thats it ? Would appreciate if someone could take a look at it and tell me whats wrong, it's porbably something trivial and i've just been looking at the code too long now ! Thanks in advance Neil
public Form1()
{
InitializeComponent();buttonTimer = new Timer(); buttonTimer.Tick += new EventHandler(button\_Tick); buttonTimer.Interval = (50); populate.BackColor = Color.Black; populate.ForeColor = Color.Orange; buttonTimer.Start(); } void button\_Tick(object sender, EventArgs e) { if (populate.BackColor == Color.Orange) { populate.BackColor = Color.Black; populate.ForeColor = Color.Orange; } if (populate.BackColor == Color.Black) { populate.BackColor = Color.Orange; populate.ForeColor = Color.Black; } }
}
Giorgi Dalakishvili #region signature My Articles Asynchronous Registry Notification Using Strongly-typed WMI Classes in .NET [^] My blog #endregion
-
Evening all ! I've created a new timer in a form's constructor to make a button on the form flash. For some reason the program is stepping through the code for the timer tick ONCE and thats it ? Would appreciate if someone could take a look at it and tell me whats wrong, it's porbably something trivial and i've just been looking at the code too long now ! Thanks in advance Neil
public Form1()
{
InitializeComponent();buttonTimer = new Timer(); buttonTimer.Tick += new EventHandler(button\_Tick); buttonTimer.Interval = (50); populate.BackColor = Color.Black; populate.ForeColor = Color.Orange; buttonTimer.Start(); } void button\_Tick(object sender, EventArgs e) { if (populate.BackColor == Color.Orange) { populate.BackColor = Color.Black; populate.ForeColor = Color.Orange; } if (populate.BackColor == Color.Black) { populate.BackColor = Color.Orange; populate.ForeColor = Color.Black; } }
}
I had this problem, the cause is that you are setting your event in the load method, which exits so the event is no longer registered. buttonTimer.Tick += new EventHandler(button_Tick); Put that in another method so that it is set again. The way I explained this is probably not the most tech savvy way, but I'm 100% sure that your event gets triggered only once because the form load method quits after your form is loaded.
-
Evening all ! I've created a new timer in a form's constructor to make a button on the form flash. For some reason the program is stepping through the code for the timer tick ONCE and thats it ? Would appreciate if someone could take a look at it and tell me whats wrong, it's porbably something trivial and i've just been looking at the code too long now ! Thanks in advance Neil
public Form1()
{
InitializeComponent();buttonTimer = new Timer(); buttonTimer.Tick += new EventHandler(button\_Tick); buttonTimer.Interval = (50); populate.BackColor = Color.Black; populate.ForeColor = Color.Orange; buttonTimer.Start(); } void button\_Tick(object sender, EventArgs e) { if (populate.BackColor == Color.Orange) { populate.BackColor = Color.Black; populate.ForeColor = Color.Orange; } if (populate.BackColor == Color.Black) { populate.BackColor = Color.Orange; populate.ForeColor = Color.Black; } }
}
-
Looks like you are testing the Color for Orange and if so, setting it to Black. Right after that you test for Black and set it back to Orange. After the ForeColor = Color.Orange, add a return ;.
Bingo! This is just what I was thinking.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008 -
Evening all ! I've created a new timer in a form's constructor to make a button on the form flash. For some reason the program is stepping through the code for the timer tick ONCE and thats it ? Would appreciate if someone could take a look at it and tell me whats wrong, it's porbably something trivial and i've just been looking at the code too long now ! Thanks in advance Neil
public Form1()
{
InitializeComponent();buttonTimer = new Timer(); buttonTimer.Tick += new EventHandler(button\_Tick); buttonTimer.Interval = (50); populate.BackColor = Color.Black; populate.ForeColor = Color.Orange; buttonTimer.Start(); } void button\_Tick(object sender, EventArgs e) { if (populate.BackColor == Color.Orange) { populate.BackColor = Color.Black; populate.ForeColor = Color.Orange; } if (populate.BackColor == Color.Black) { populate.BackColor = Color.Orange; populate.ForeColor = Color.Black; } }
}
it isn't stepping through once - stick a breakpoint in to prove that. it just has no effect the 2nd and subsequent times... replace
if (populate.BackColor == Color.Black)
with "else" and all will be well. Inidentally, if I were you, I would put ((Timer)sender).Stop(); at the start of yor buttin_Click event handler, and ((Timer)sender).Start(); at the end. This stops the timer triggering while the code is still executing - and helps when you are debugging too!
If I knew then what I know today, then I'd know the same now as I did then - then what would be the point? .\\axxx (That's an 'M')
-
I had this problem, the cause is that you are setting your event in the load method, which exits so the event is no longer registered. buttonTimer.Tick += new EventHandler(button_Tick); Put that in another method so that it is set again. The way I explained this is probably not the most tech savvy way, but I'm 100% sure that your event gets triggered only once because the form load method quits after your form is loaded.
That makes no sense at all.
-
Instead of this:
buttonTimer.Tick += new EventHandler(button_Tick);
Try this:
buttonTimer.Elapsed += new EventHandler(button_Elapsed);
Don't know if it makes any difference, but it's worth a try.
Kristian Sixhoej sixhoej.net - forums.sixhoej.net
-
Looks like you are testing the Color for Orange and if so, setting it to Black. Right after that you test for Black and set it back to Orange. After the ForeColor = Color.Orange, add a return ;.
Better yet, use an else:
if (populate.BackColor == Color.Orange) {
populate.BackColor = Color.Black;
populate.ForeColor = Color.Orange;
} else {
populate.BackColor = Color.Orange;
populate.ForeColor = Color.Black;
}Despite everything, the person most likely to be fooling you next is yourself.