(VB.NET) How To Make a Label Blink, But Be On longer than Off For Easier Reading?

I want a warning label (which is an image with text on it) to be on say for 800ms then blink off for 200ms rather than just the standard same interval for on/off. There are other labels and I need this one to stand out. I find it hard to read if it is blinking too fast and it would be off too long if I kept it on for say a few seconds with just a single timer. The headerflag comes from a different function to signify if it is needed.

Would something like this be possible? I have 2 timers thinking it would be easy, but the label just kind of freaks out as I play with the intervals. Maybe my code is just too simple for what I need? Bad intervals? More timers?

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer_ON.Tick
    Timer_ON.Interval = 800     ' Blink on for 800ms
    If HeaderFlag = 1 Then
        Warning_Header.Visible = Not Warning_Header.Visible
    End If
End Sub

Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer_OFF.Tick
    Timer_OFF.Interval = 200    ' Blink off for 200ms
    If HeaderFlag = 1 Then
        Warning_Header.Visible = Not Warning_Header.Visible
    End If
End Sub

>Solution :

Just use a single Timer. Always use a single Timer if you can get away with it. In your case, set the Interval to 200 and then wait four Ticks to hide the Label, e.g.

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    Static tickCount As Integer = 1

    If Not Label1.Visible OrElse tickCount = 4 Then
        Label1.Visible = Not Label1.Visible
        tickCount = 1
    Else
        tickCount += 1
    End If
End Sub

Leave a Reply