How to make a label blink

15,579

Solution 1

You could use a simple Async method to do this.

The following code will give Label1 the effect of flashing. Since we have used While True this will continue indefinitely once 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 inside your Timer1_Tick method:

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 to 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 the number 10 to a number of your choice you can shorten or lengthen the time taken to flash. I have added in Label1.Visible = True after the For loop just to be sure that we see the Label once the flashing has finished.

You will have to import System.Threading.Tasks to make use of Task.Delay.

Solution 2

You need a label, two textboxes, and a button. The screen allows you to 'set' a couple of colours - this could be taken further, by adding an Error colour, a Warning colour (where you haven't filled a field in...?) and more. This colour selection would, in a real application, be done by an admin person, from a separate screen, and stored in the DB. The timer frequency would also be set in the Admin screen/function. This particular screen needs the textboxes to be double-clicked, and a colour selected for each one. The back colour for each box changes. Then press the Start button. If you press the Start button again, it toggles the timer (on/off)

Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load ' not quite correct for what I want, but close... ' https://bytes.com/topic/visual-basic-net/answers/368433-blinking-text Me.Label1.Text = "A blinking text box" Me.Label1.BackColor = TextBox2.BackColor End Sub

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    If Me.Label1.BackColor = TextBox2.BackColor Then
        Me.Label1.BackColor = TextBox1.BackColor
    Else
        Me.Label1.BackColor = TextBox2.BackColor
    End If
End Sub

Private Sub TextBox1_MouseDoubleClick(sender As Object, e As MouseEventArgs) Handles TextBox1.MouseDoubleClick
    Dim dlg As New ColorDialog()
    If dlg.ShowDialog() = DialogResult.OK Then
        TextBox1.BackColor = dlg.Color
    End If
End Sub

Private Sub TextBox2_MouseDoubleClick(sender As Object, e As MouseEventArgs) Handles TextBox2.MouseDoubleClick
    Dim dlg As New ColorDialog()
    If dlg.ShowDialog() = DialogResult.OK Then
        TextBox2.BackColor = dlg.Color
    End If
End Sub

Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click
    Timer1.Enabled = Not Timer1.Enabled
End Sub

End Class

Share:
15,579
Eaten Taik
Author by

Eaten Taik

Updated on June 04, 2022

Comments

  • Eaten Taik
    Eaten Taik almost 2 years

    I have a Stopwatch in my form with Interval = 1000 displayed in the hh:mm:ss format.

    When it reaches the 5th second it should start to blink the label background as green but so far I can only make the background color turn to green without any flash.

    This is how I turn the background color to 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 do I make the label blink?