How to make a label blink - vb.net

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?

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.

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

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.

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

Related

Timer not stopping VB.NET

I'm making a simple Windows Forms App with Visual Studio 2019. I want to use a timer and a progress bar but i can't make the timer stop.
I've tried Timer1.Stop() and Timer1.Enabled = False but neither of them have worked. The timer waits 1 second.
Here's the full code:
(it has changed a lot but I still have the problem)
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If Button1.Text = "Stop" Then
ProgressBar1.Value = 0
Timer1.Enabled = False
End If
Button1.Text = "Stop"
Timer1.Enabled = True
Cursor = Cursors.AppStarting
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If Timer1.Enabled = True Then
ProgressBar1.Increment(1)
Else
ProgressBar1.Value = 0
End If
If ProgressBar1.Value = 100 Then
Timer1.Stop()
Button1.Text = "Done!"
End If
End Sub
I strongly suggest that you use a CheckBox rather than a Button. If you set the Appearance to Button then it will look just like a regular Button but you can use the Checked property to represent state. The control will appear depressed when Checked is True. You can then use code like this:
Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
Timer1.Enabled = CheckBox1.Checked
CheckBox1.Text = If(CheckBox1.Checked, "Stop", "Start")
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
ProgressBar1.PerformStep()
If ProgressBar1.Value = ProgressBar1.Maximum Then
CheckBox1.Checked = False
End If
End Sub

Change the color of a button(s) for a duration of time

I have a small VB.net app that has a LOT of buttons. I need to change the back color of the buttons when they are clicked and then set it back to its original color after a duration of 10 seconds. I am struggling with either using a timer or the time process both of which have their own issues.
Any ideas to make this work and work efficiently?
Code:
Private Sub MyButtons_Click(sender As Object, e As EventArgs) _
Handles Button1.Click,
Button2.Click
Dim myButton = DirectCast(sender, Button)
MakeCall()
myButton.BackColor = Color.Green
'TurnOnActiveCallCOLOR.Enabled = True
For i As Integer = 0 To 10000 - 1
Threading.Thread.Sleep(10000)
Next
myButton.BackColor = Color.FromArgb(64, 64, 64)
End Sub
Here is an example of using a Windows Forms Timer to accomplish what you need:
Private MyButton As Button
Private Sub MyButtons_Click(sender As Object, e As EventArgs) Handles Button1.Click, Button2.Click
MyButton = DirectCast(sender, Button)
MyButton.BackColor = Color.Green
Timer1.Enabled = True
MakeCall()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
MyButton.BackColor = Color.FromArgb(64, 64, 64)
Timer1.Enabled = False
End Sub
To handle an arbitrary number of buttons, you could have something like this:
Public Class Form1
Dim buttonTimers As New Dictionary(Of Control, ButtonTimer)
Class ButtonTimer
Property Timeout As Integer = 2000
Property Target As Control
Property ActiveColor As Color = Color.Green
Property DefaultColor As Color = Color.FromArgb(64, 64, 64)
Private tim As Timer
Sub TimTick(sender As Object, e As EventArgs)
tim.Stop()
Target.BackColor = DefaultColor
End Sub
Sub New()
' empty constructor
End Sub
Sub New(target As Control)
Me.Target = target
Me.Target.BackColor = Me.ActiveColor
tim = New Timer With {.Interval = Timeout}
AddHandler tim.Tick, AddressOf TimTick
tim.Start()
End Sub
Sub Restart()
Target.BackColor = Me.ActiveColor
If tim IsNot Nothing Then
tim.Stop()
tim.Start()
End If
End Sub
Public Sub DisposeOfTimer()
If tim IsNot Nothing Then
tim.Stop()
RemoveHandler tim.Tick, AddressOf TimTick
tim.Dispose()
End If
End Sub
End Class
Private Sub Button_Click(sender As Object, e As EventArgs) Handles Button1.Click, Button2.Click
Dim myButton = DirectCast(sender, Button)
'MakeCall()
If buttonTimers.ContainsKey(myButton) Then
buttonTimers(myButton).Restart()
Else
buttonTimers.Add(myButton, New ButtonTimer(myButton))
End If
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
For Each x In buttonTimers
x.Value.DisposeOfTimer()
Next
End Sub
End Class
If a button is clicked again before the timeout, the time is restarted.
You can add other constructors if you want to have a different timeout/colours for different buttons.
The MyBase.FormClosing code should be included in your form closing handler (if there is one) so that the timers are cleaned up properly.
I expect it would be tidier overall to make your own custom button class which inherits from Button, so you might want to investigate doing that. (How to: Inherit from Existing Windows Forms Controls.)
asynh and await if you don't want use timer. Simple method using task.delay
Private Async Sub ButtonClick(sender As Object, e As EventArgs) Handles Button1.Click, Button2.Click, Button3.Click, Button4.Click
DirectCast(sender, Button).BackColor = Color.Red 'after click setcolor to red
Await setColorAfterDelay(sender) 'Add this comand on button click and don't forget add asynh before sub in this method
End Sub
Public Async Function setColorAfterDelay(sender As Object) As Task
Await Task.Delay(1000) ''Milisecound how long you wana dealey
DirectCast(sender, Button).BackColor = Color.White 'and set colorto white
End Function
With the use of lambda expressions (and a lookup table if you want to interact with it further) you can do this pretty easily:
'Lookup table for if you want to be able to interact with the timers even more.
Dim ButtonTimers As New Dictionary(Of Button, Timer)
Private Sub MyButtons_Click(sender As Object, e As EventArgs) _
Handles Button1.Click, Button2.Click
MakeCall()
Dim myButton = DirectCast(sender, Button)
myButton.BackColor = Color.Green
'If a timer already exists for the button, restart it.
Dim existingTimer As Timer = Nothing
If ButtonTimers.TryGetValue(myButton, existingTimer) Then
existingTimer.Stop()
existingTimer.Start()
Return 'Do not execute the rest of the code.
End If
'Create the timer and set its Interval to 10000 ms (10 seconds).
Dim buttonTimer As New Timer() With {.Interval = 10000}
'Add a handler to its Tick event.
AddHandler buttonTimer.Tick, _
Sub(tsender As Object, te As EventArgs)
myButton.BackColor = Color.FromArgb(64, 64, 64)
'Dispose timer and remove from lookup table.
ButtonTimers.Remove(myButton)
buttonTimer.Stop()
buttonTimer.Dispose()
End Sub
ButtonTimers.Add(myButton, buttonTimer)
buttonTimer.Start()
End Sub
If you want to interact with a button's timer (if one exists) you can do:
Dim buttonTimer As Timer = Nothing
If ButtonTimers.TryGetValue(yourButtonHere) Then
'Do something with buttonTimer...
End If
This could probably be done better by dynamically creating timer controls so each button has it's own timer but here is what I came up with.
Oh, an set your timer to 1000 interval and enabled to 'False'.
Public Class Form1
Dim T1 As Integer = 0
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
T1 = T1 + 1
If T1 = 10 Then
For Each button In Controls
button.backcolor = Color.FromArgb(225, 225, 225)
Next
Timer1.Stop()
T1 = 0
End If
Me.Text = T1
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Button1.BackColor = Color.Red
Timer1.Start()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Button2.BackColor = Color.Orange
Timer1.Start()
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Button3.BackColor = Color.Yellow
Timer1.Start()
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
Button4.BackColor = Color.Green
Timer1.Start()
End Sub
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
Button5.BackColor = Color.Blue
Timer1.Start()
End Sub
End Class

Change PictureBox Visibility After Certain Time VB.Net

I currently have it to where picturebox1 is visible on loadup and I would like to change it to where picturebox2 is visible and picturebox1 is not after 3 seconds. I have been unable to get this to visibly work. Any suggestions? I have looked around and saw the Picturebox.refresh & picturebox.update but have not been able to get these to work. I am open to suggestions on how to do this differently as well. Thanks for the help!
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
PictureBox1.BackgroundImage = My.Resources.Resources._024689
PictureBox2.BackgroundImage = My.Resources.Resources._152522206296244269
PictureBox1.Visible = True
PictureBox2.Visible = False
InitializeComponent()
'starts timer
StartTimer.Interval = 1000
StartTimer.Start()
End Sub
Private Sub StartTimer_Tick(sender As Object, e As EventArgs) Handles StartTimer.Tick
time += 1
Debug.Print("Time = " & time)
If time = 3 Then
PictureBox2.Visible = True
PictureBox1.Visible = False
StartTimer.Stop()
End If
End Sub
For the record, this worked exactly as expected:
'Ensure that resources are loaded once only.
Private ReadOnly firstImage As Image = My.Resources.Capture__56x81_
Private ReadOnly secondImage As Image = My.Resources.Capture__70x264_
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
PictureBox1.Image = firstImage
Timer1.Start()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Timer1.Stop()
PictureBox1.Image = secondImage
End Sub
Note that the Interval of Timer1 was set to 3000 in the designer.

Text scrolling up, but after closing the form. The text wont back on the start. vb.net

I was able to scroll the text up, put a stop on it when it reached a certain location on Y.
However, when I was trying to load the credit form again. It will not be back on the start.
Here's the code I'm working on.
Public Class creditsform
Private Sub creditsform_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed
Label1.Location = New Point(Label1.Location.X, Label1.Location.Y = 304) ' put back the label where it is.
End Sub
Private Sub creditsform_Load(sender As Object, e As EventArgs) Handles Me.Load
Timer1.Enabled = True
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Label1.Location = New Point(Label1.Location.X, Label1.Location.Y - 5) ' scroll up the label up
If (Label1.Location.Y = -1506) Then ' closes the form if he reach the end of credit roll
Timer1.Enabled = False ' disable the timer
Me.Close() ' then close the form
End If
End Sub
End Class
EDIT: HERE IS THE FIRST LOAD AND THE SECOND LOAD OF THE FORM.
Also after the 2nd load, it doesn't execute the if else statement to close automatically the form.
1ST LOAD & 2ND LOAD
Here's where creditsform called.
Public Class EndGame
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Dim Red, Green, Blue As Integer
Dim RandomNumGenerator As New Random
Red = RandomNumGenerator.Next(0, 255)
Green = RandomNumGenerator.Next(0, 255)
Blue = RandomNumGenerator.Next(0, 255)
Me.Label4.ForeColor = Color.FromArgb(Red, Green, Blue)
Me.Label5.ForeColor = Color.FromArgb(Red, Green, Blue)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click '' this button closes the form and shows the creditsform
Me.Close()
creditsform.ShowDialog()
End Sub
End class
If I understood the problem correctly since it's not very clear what you are trying to do.
This is normal your using a class but not creating new instance of it so your variables don't reset. you should try this:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click '' this button closes the form and shows the creditsform
Me.Close()
Dim MyCredits As New creditsform
MyCredits.ShowDialog()
End Sub

How to generate multiple picture boxes and control them using a timer?

My uni asked us to make a game using VB, and I really don't know much about the language.
I'm trying to make a game where balloons go up to them top of the screen and must be popped before getting there.
Public Class Form1
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If PictureBox1.Top = 0 Then
PictureBox1.Visible = False
Timer1.Enabled = False
End If
PictureBox1.Top = PictureBox1.Top - 1
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Timer1.Interval = 1
Timer1.Enabled = True
End Sub
Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click
PictureBox1.Visible = False
End Sub
End Class
This is my code so far, when I click the button, the balloon starts to go up, if I click the balloon, it disappears, it also disappears if it reaches the top and the timer stops.
How can I generate more balloons and control them using that timer?
Now all you have let to do is add the functionality of adding more PictureBoxes, maybe a second timer and when you create them use an Addhandler statement to point them the the pbs_Click event that I made and add them to the List I made as well.
Public Class Form1
Private PBs As New List(Of PictureBox)
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
For Each pb As PictureBox In PBs
If pb.Top = 0 Then
pb.Visible = False
Timer1.Enabled = False
Else
pb.Top = pb.Top - 1
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Timer1.Interval = 1000'ms
Timer1.Enabled = True
End Sub
Private Sub pbs_Click(sender As Object, e As EventArgs)
Dim pb As PictureBox = DirectCast(sender, PictureBox)
PBs.Remove(pb)
End Sub
Private Sub makeNewPB()
Dim pb As New PictureBox
Addhandler pb.Click, AddressOf pbs_Click
'don't forget to make them the size you need
PBs.Add(pb)
End Sub
End Class