How to make a shortcut shortcut

I have Stopwatch

in my form with the Interval = 1000

displayed format hh:mm:ss

.

When it reaches the 5th second, it should start flickering the background of the label as green, but for now I can only make the background color so that it doesn't turn green without any flash.

This is how I turn the background color green:

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    Label1.Text = SW.Elapsed.ToString("hh\:mm\:ss")
    If Label1.Text = "00:00:05" Then
        Label1.BackColor = Color.Green
    End If
End Sub

      

How to make a shortcut shortcut?

+3


source to share


3 answers


You can use a simple way to do this Async

.

The following code will give Label1

a blinking effect. Since we used it While True

, this will continue indefinitely as soon as you hit "00:00:05".

Private Async Sub Flash()
    While True
        Await Task.Delay(100)
        Label1.Visible = Not Label1.Visible
    End While
End Sub

      

You would call this in your method Timer1_Tick

:

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    Label1.Text = SW.Elapsed.ToString("hh\:mm\:ss")
    If Label1.Text = "00:00:05" Then
        Label1.BackColor = Color.Green
        Flash()
    End If
End Sub

      



If you only want to flash a couple of times, we can make a simple change Flash()

:

Private Async Sub Flash()
    For i = 0 To 10
        Await Task.Delay(100)
        Label1.Visible = Not Label1.Visible
    Next

    'set .Visible to True just to be sure
    Label1.Visible = True
End Sub

      

By changing number 10 to the number of your choice, you can shorten or lengthen the time it takes to flash. I added in Label1.Visible = True

after the loop For

to make sure what we see Label

after the blink finishes.

You will need to import System.Threading.Tasks

in order to use Task.Delay

.

+4


source


Try adding something like this in your Timer1_Tick event handler -

Label1.Visible = Not Label1.Visible

      



Set a timer to turn on and it will complete the task.

0


source


If you specify the color when the text is 00:00:05 then you also have to specify what should be Backcolor

when the text is something else ie 00:00:06

Try this and see if it works:

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Label1.Text = SW.Elapsed.ToString("hh\:mm\:ss")
If Label1.Text = "00:00:05" Then
    Label1.BackColor = Color.Green
else
    Label1.Backcolor = Color.Yellow '(Change color as needed)
End If
End Sub

      

0


source







All Articles