Reputation: 938
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?
Upvotes: 4
Views: 20273
Reputation: 1
Another Solution:
Public Class Form1
Private blinkingTimer As New Timer()
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' Set Timer
blinkingTimer.Interval = 500 ' Time to keep blinking in milliseconds
AddHandler blinkingTimer.Tick, AddressOf BlinkLabel
' Start Timer
blinkingTimer.Start()
End Sub
Private Sub BlinkLabel(sender As Object, e As EventArgs)
If Label1.ForeColor = Color.Red Then
Label1.ForeColor = Color.Black
Else
Label1.ForeColor = Color.Red
End If
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
' Stop timer when app is closed
blinkingTimer.Stop()
End Sub
End Class
Upvotes: 0
Reputation: 31
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
Upvotes: 1
Reputation: 4489
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 ofTask.Delay
.
Upvotes: 8
Reputation: 659
If you specify the color when the Text is 00:00:05 then you should also specify what the Backcolor
should be when the text is something else i.e 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
Upvotes: 0
Reputation: 465
Try to put something like this in Timer1_Tick event handler -
Label1.Visible = Not Label1.Visible
Set the timer to enabled and it will do the job.
Upvotes: 0