Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
There are one label, two buttons (start timer, stop timer) and one timer control on my form. When I click on the start button the color of the label shall change according to the timer: 1st second red color, 2nd second blue, 3rd second red, 4th blue etc.. When I click on the stop button then the timer and thus the color change shall stop.

Please help me.
Posted
Updated 17-Feb-11 1:04am
v2
Comments
Sergey Alexandrovich Kryukov 16-Feb-11 22:46pm    
Any effort? This problem is too simple... Nobody would be interested to write this boring code for your.
--SA

The usual answer is using timer, but there using System.ComponentModel.BackgroundWorker is much easier and more robust.

By the way, don't use timer control (as a rule of a thumb, never), use System.Timers.Timer instead. Using the timer is simple, you won't need any extra instruction, just read its help. You need Control.Invoke to change your colors, see below.

Anyway, if you wanted to use timer control, its accuracy would not be better then that of thread.

This will give you idea how to work with BackgroundWorker: http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx[^], and this — how to stop (cancel) the background work: http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/064fa2b3-8969-4060-970e-79785b854083[^].

Organize infinite loop in BackgroundWorker.DoWork, use System.Threading.Thread.Sleep(1000) between changes in color. Do color change via Control.Invoke. In the loop, the code should check BackgroundWorker.CancellationPending in order to support cancellation mechanism described in the reference above.

That's it.

—SA
 
Share this answer
 
Comments
[no name] 17-Feb-11 5:46am    
these is a perfect solution.
Sergey Alexandrovich Kryukov 17-Feb-11 20:03pm    
Oh, thank you.
--SA
Espen Harlinn 17-Feb-11 9:35am    
Good effort, my 5
Sergey Alexandrovich Kryukov 17-Feb-11 20:03pm    
Thank you.
--SA
Code is:
VB
button_start_click()
{
    timer1.interval=timer1.interval+1000
    if timer1.interval=1000 then
        label1.backcolor=red
    elseif timer1.interval=2000 then
        label2.backcolor=green
    end if
}

button_stop()
{
    timer1.enable=false
    timer1.interval=0
}


[Edit: Added code tags.]
 
Share this answer
 
v3
Comments
Manfred Rudolf Bihy 17-Feb-11 6:58am    
Your code uses a timer to do absolutely timer unrelated stuff. What are you doing here?
If you really are going to use a timer you should at least have a handler for it. Switching the color in the start button's handler is not exactly what OP was asking for.
Please rethink you answer. I'll check back later and decide to vote then.
Sergey Alexandrovich Kryukov 17-Feb-11 20:03pm    
I rather agree with Manfred, also see my note on Forms timer
Also, anonymous method at the point of invocation list update is much better.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900