Timer and Label Control

newguy

Well-known member
Joined
Jun 30, 2008
Messages
611
Location
Denver Co, USA
Programming Experience
1-3
Hi All, I need a timer to change a label's .backcolor for a very short time (like a flash) and I need to setup another interval for how often this color change occurs based on an equation from two diff numbers. Using a Start and Stop button for starting and stopping the timer.

I don't understanding the loop statement or the timer control very well.
 
Timer's run the code in their Tick() event based on an interval. So in this case you'd set the label's backcolor to a different color and start the timer (the timer should have an interval = 900, just less than 1 second) and also in the Tick event be sure to have the timer stop itself

Then for repeated occurrences of this "flash", I would simply use another timer with the timer that does the flash
 
Ok, so when I start the tick_event I can modify the .interval with the two numbers that control the interval of the flashes, right?
How do I set the stop in the tick_event, when I am wanting to use the button to stop the timer?
 
I set a var as boolean, with the startButton, it declares it to be true
with the stopButton, it declares it false.

In the tick_event, if var is true then .start... else .stop...

This correct?
 
Here's a quick example of how to do exactly what I said in my previous post:
VB.NET:
'There's 1 button named Button1, 1 label named Label1 and 2 timers named Timer1 and FlashTimer
Public Class Form1

    Private m_TimerRunning As Boolean = True

    Private Sub FlashTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FlashTimer.Tick
        Label1.BackColor = SystemColors.Control
        FlashTimer.Stop()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        m_TimerRunning = Not m_TimerRunning
        If m_TimerRunning = False Then
            Button1.Text = "&Stop"
            Call FlashLabel()
            Timer1.Start()
        Else
            Button1.Text = "&Start"
            Timer1.Stop()
            FlashTimer.Stop()
        End If
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Call FlashLabel()
    End Sub

    Private Sub FlashLabel()
        Label1.BackColor = Color.Red
        FlashTimer.Start()
    End Sub
End Class
To change the duration of the flash itself simply change the interval of FlashTimer and to change the amount of time between each flash change Timer1's interval property
 
Cool, I get it, I only need one button, and do I set the interval in the tick_event or in a sub()?
 
Cool, I get it, I only need one button, and do I set the interval in the tick_event or in a sub()?
I just set it in the designer
Timer1's Interval was set to 3000 (3 seconds)
FlashTimer's Interval was set to 400

You can set a timer's interval in code too, even if the timer's already running. The timer's interval property works the same as setting a button's Text property in code.

PS: Yes you only need one button, but it can be done using two buttons too.
 
ok, timer two which tell the flash timer when to tick needs to be set be a small equation based on the numbers in two textboxes.

timer2.interval = cint(textbox1.text) * Cint(textbox2.text) * 1000 'milliseconds

will this work? and where do I put it?
 

Latest posts

Back
Top