I am trying to make a more efficent button color changer for every 500ms. I don't want to make a ton of timers so I thought if I put a threading time in the tick it would work. NOPE. I failed that. My code just turns them all on at the same time.
Any help to accomplish this would be greatly appreciated.
Code:
Private Sub AnimateButtons_Tick(sender As Object, e As EventArgs) Handles AnimateButtons.Tick
BTN1.BackColor = Color.Red
Threading.Thread.Sleep(500)
BTN2.BackColor = Color.Red
Threading.Thread.Sleep(500)
BTN3.BackColor = Color.Red
Threading.Thread.Sleep(500)
BTN4.BackColor = Color.Red
Threading.Thread.Sleep(500)
BTN5.BackColor = Color.Red
Threading.Thread.Sleep(500)
BTN6.BackColor = Color.Red
Threading.Thread.Sleep(500)
End Sub
I don't have much information here but depending on which control you use, the timer will be running on the UI thread. This mean, the UI will update only when the "Tick" is finish. You'll need to set your timer to 500ms and change only one button on each tick. Something like this:
Dim buttonIndex As Integer = 1 ' Keep the value of which button to change
Private Sub AnimateButtons_Tick(sender As Object, e As EventArgs) Handles AnimateButtons.Tick
If buttonIndex = 1 Then BTN1.BackColor = Color.Red
If buttonIndex = 2 Then BTN2.BackColor = Color.Red
If buttonIndex = 3 Then BTN3.BackColor = Color.Red
If buttonIndex = 4 Then BTN4.BackColor = Color.Red
If buttonIndex = 5 Then BTN5.BackColor = Color.Red
If buttonIndex = 6 Then BTN6.BackColor = Color.Red
End Sub
buttonIndex += 1
Instead of having a global variable, you can initialize the index as static inside the method. I think this should work.
Private Sub AnimateButtons_Tick(sender As Object, e As EventArgs) Handles AnimateButtons.Tick
Static buttonIndex As Integer = 1
If buttonIndex = 1 Then BTN1.BackColor = Color.Red
If buttonIndex = 2 Then BTN2.BackColor = Color.Red
If buttonIndex = 3 Then BTN3.BackColor = Color.Red
If buttonIndex = 4 Then BTN4.BackColor = Color.Red
If buttonIndex = 5 Then BTN5.BackColor = Color.Red
If buttonIndex = 6 Then BTN6.BackColor = Color.Red
buttonIndex += 1
End Sub
If course, there are "better" ways of doing this like using a list but I hope you get the general idea.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments