How do I make a more efficient color changer? VB.net

FNDevCJ

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
the_lotus

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.

edited at
0

Comments

0 comments
Login to comment

Related

How do I make this matching list code more efficient?

How do I make the selection function more efficient?

How do I make my pygame levels more efficient?

How do I make the following C structure more memory efficient?

How would I make this script more efficient?

How can I make this loop more efficient?

How can I make this more efficient in Android?

How would I make this more efficient? (python)

VB.net how do I make Uppercase is the as as small caps

Python: How to make this color thresholding function more efficient

How to make this "cropping"/background color changing function more efficient?

How do I make this function for concatenating Excel sheets from a single file more efficient?

How do I make my VBA code more efficient using For each loops for three different ranges?

How do I make this more efficient- Consider the fraction, n/d, where n and d are positive integers

How do I make this VBA code more efficient so it does not crash due to lack of memory?

How do I make efficient queries in Django?

How do i make efficient slots in an inventory?

How can I make a code more efficient and shorter?

How can I make this Python code more efficient

How can I make this C# code more efficient?

How can I make my pandas code more efficient?

How can I make my trie more efficient?

How can I make this PyTorch heatmap function faster and more efficient?

How can I make this search query more efficient?

How would I make this code more compact/efficient?

How can I make a recursive search for longest node more efficient?

How can I make this more efficient path finding?

How can I make this more efficient? (Merging arrays in C)

How can I refactor this code snippet to make it more efficient?