Animation effect by changing colors
-
I have written a code to change the back color of a label twice during execution to give it an animation effect. The idea says that :
when the button is pressed change the color from black to red.
Then a null loop is placed to create some delay and then the color changes back to black.<pre lang="vb">Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
box.BackColor = Color.Red
For delay As Double = 0 To 900000000 Step 1
Next
box.BackColor = Color.Black
End Sub</pre>But its not performing as desired. The label doesnt become red and i doubt the program is executing the delay first. Any idea whats the issue?
-
I have written a code to change the back color of a label twice during execution to give it an animation effect. The idea says that :
when the button is pressed change the color from black to red.
Then a null loop is placed to create some delay and then the color changes back to black.<pre lang="vb">Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
box.BackColor = Color.Red
For delay As Double = 0 To 900000000 Step 1
Next
box.BackColor = Color.Black
End Sub</pre>But its not performing as desired. The label doesnt become red and i doubt the program is executing the delay first. Any idea whats the issue?
You will need to change the colour back using a separate thread. Something like this:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim newThread As New Threading.Thread(New Threading.ThreadStart(AddressOf ChangeBGColour)) newThread.Start() Label1.BackColor = Color.Red
End Sub
Private Sub ChangeBGColour()
Threading.Thread.Sleep(3000) 'adjust time as necessary Label1.BackColor = Color.Yellow
End Sub
Hope this helps
Live for today. Plan for tomorrow. Party tonight!
modified on Tuesday, August 30, 2011 3:31 AM
-
You will need to change the colour back using a separate thread. Something like this:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim newThread As New Threading.Thread(New Threading.ThreadStart(AddressOf ChangeBGColour)) newThread.Start() Label1.BackColor = Color.Red
End Sub
Private Sub ChangeBGColour()
Threading.Thread.Sleep(3000) 'adjust time as necessary Label1.BackColor = Color.Yellow
End Sub
Hope this helps
Live for today. Plan for tomorrow. Party tonight!
modified on Tuesday, August 30, 2011 3:31 AM
You're getting downvoted because you cannot touch a UI control from any other thread other than the thread that created it (the startup thread.)
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
I have written a code to change the back color of a label twice during execution to give it an animation effect. The idea says that :
when the button is pressed change the color from black to red.
Then a null loop is placed to create some delay and then the color changes back to black.<pre lang="vb">Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
box.BackColor = Color.Red
For delay As Double = 0 To 900000000 Step 1
Next
box.BackColor = Color.Black
End Sub</pre>But its not performing as desired. The label doesnt become red and i doubt the program is executing the delay first. Any idea whats the issue?
Your code is running on the startup thread, also called UI thread. If the thread is blocked there is no way it can process the Windows PAINT messages that tell the controls to repaint themselves. This is why your label doesn't change color until your button Click method is done executing. You have to move your work to a background thread. The problem is you cannot make changes to a control, such as changing the background color, from anything other that the UI thread. Once your background thead "work" is done the background thread can notify the foreground thread to change the label color as appropriate. Read this article[^] for a discussion and alternate methods of modifying controls from background threads.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
Your code is running on the startup thread, also called UI thread. If the thread is blocked there is no way it can process the Windows PAINT messages that tell the controls to repaint themselves. This is why your label doesn't change color until your button Click method is done executing. You have to move your work to a background thread. The problem is you cannot make changes to a control, such as changing the background color, from anything other that the UI thread. Once your background thead "work" is done the background thread can notify the foreground thread to change the label color as appropriate. Read this article[^] for a discussion and alternate methods of modifying controls from background threads.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak:thumbsup:
Luc Pattyn [My Articles] Nil Volentibus Arduum
-
I have written a code to change the back color of a label twice during execution to give it an animation effect. The idea says that :
when the button is pressed change the color from black to red.
Then a null loop is placed to create some delay and then the color changes back to black.<pre lang="vb">Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
box.BackColor = Color.Red
For delay As Double = 0 To 900000000 Step 1
Next
box.BackColor = Color.Black
End Sub</pre>But its not performing as desired. The label doesnt become red and i doubt the program is executing the delay first. Any idea whats the issue?
You will need to change the colour back using a separate thread. Something like this:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim newThread As New Threading.Thread(New Threading.ThreadStart(AddressOf ChangeBGColour)) newThread.Start() Label1.BackColor = Color.Red End Sub Delegate Sub changeLabelColour() Private Sub ChangeBGColour() Threading.Thread.Sleep(3000) If Me.Label1.InvokeRequired Then Dim c As New changeLabelColour(AddressOf ChangeBGColour) Me.Invoke(c) Else Label1.BackColor = Color.Yellow End If End Sub
I added proper UI thread access . My apologies for the basic thread error. Hope this helps
Live for today. Plan for tomorrow. Party tonight!
-
You're getting downvoted because you cannot touch a UI control from any other thread other than the thread that created it (the startup thread.)
A guide to posting questions on CodeProject[^]
Dave KreskowiakThanks, very elementary mistake. Note to self, think before posting. I have corrected the answer below(first one was removed).
Live for today. Plan for tomorrow. Party tonight!
-
You will need to change the colour back using a separate thread. Something like this:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim newThread As New Threading.Thread(New Threading.ThreadStart(AddressOf ChangeBGColour)) newThread.Start() Label1.BackColor = Color.Red End Sub Delegate Sub changeLabelColour() Private Sub ChangeBGColour() Threading.Thread.Sleep(3000) If Me.Label1.InvokeRequired Then Dim c As New changeLabelColour(AddressOf ChangeBGColour) Me.Invoke(c) Else Label1.BackColor = Color.Yellow End If End Sub
I added proper UI thread access . My apologies for the basic thread error. Hope this helps
Live for today. Plan for tomorrow. Party tonight!
-
Cheers.
Live for today. Plan for tomorrow. Party tonight!