• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 257
  • Last Modified:

Timer (flashing color)

how can i run these 2 lines alternatively every 2 seconds to have the effect of a flashing LightPink color.

 Me.grdOther.TableStyles(0).BackColor = System.Drawing.Color.LightPink

 Me.grdOther.TableStyles(0).BackColor = System.Drawing.Color.Black

thanks for the help
0
allff
Asked:
allff
  • 3
1 Solution
 
S-TwilleyCommented:
Dim WithEvents tmrFlash As New Timers.Timer(2000)
Dim iAlternate As Integer = 0

Sub tmrev_Flash(ByVal sender as object, e as Timers.ElapsedEventArgs) Handles tmrFlash.Elapsed
    If iAlternate = 0 Then
         Me.grdOther.TableStyles(0).BackColor = System.Drawing.Color.LightPink
    Else
         Me.grdOther.TableStyles(0).BackColor = System.Drawing.Color.Black
    End If

    iAlternate = (iAlternate + 1) Mod 2
End Sub

Then when you want the flashing to start (maybe in your form load)

tmrFlash.Start
0
 
S-TwilleyCommented:
Just a note... if you start adding more code within the timer event... which may take some time... you may want to stop the timer as it enters the handler and start again afterwards... to prevent a later event occuring too soon.

Sub tmrev_Flash(ByVal sender as object, e as Timers.ElapsedEventArgs) Handles tmrFlash.Elapsed
    tmrFlash.Stop()
    If iAlternate = 0 Then
         Me.grdOther.TableStyles(0).BackColor = System.Drawing.Color.LightPink
    Else
         Me.grdOther.TableStyles(0).BackColor = System.Drawing.Color.Black
    End If

    iAlternate = (iAlternate + 1) Mod 2
    tmrFlash.Start()
End Sub
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
S-Twilley has already given you a perfectly good solution so please do not accept this as the answer.

Instead of:

    iAlternate = (iAlternate + 1) Mod 2

why not use a boolean variable and toggle its state as shown below?

    Private tmr As System.Timers.Timer

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        tmr = New System.Timers.Timer(2000)
        AddHandler tmr.Elapsed, AddressOf Me.tmr_Elapsed
        tmr.Enabled = True
    End Sub

    Private Sub tmr_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs)
        Static clr As Boolean

        clr = Not clr
        If clr Then
            Me.grdOther.TableStyles(0).BackColor = System.Drawing.Color.LightPink
        Else
            Me.grdOther.TableStyles(0).BackColor = System.Drawing.Color.Black
        End If
    End Sub

I think it looks neater...
0
 
S-TwilleyCommented:
Would agree it is neater... i suppose the only advantage of using the counter that I've used if you wanted to go for more than a 2 colour sequence (could improve on my code in that case... have an array of colours... and a counter that resets based on the length of the array... but I don't want to get carried away with a simple question :P).
0
 
allffAuthor Commented:
Both perfect,

Thanks a lot
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now