Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Animation effect by changing colors

Animation effect by changing colors

Scheduled Pinned Locked Moved Visual Basic
helpquestion
9 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Razanust
    wrote on last edited by
    #1

    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?

    W D 3 Replies Last reply
    0
    • R Razanust

      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?

      W Offline
      W Offline
      Wayne Gaylard
      wrote on last edited by
      #2

      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

      D 1 Reply Last reply
      0
      • W Wayne Gaylard

        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

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #3

        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

        W 1 Reply Last reply
        0
        • R Razanust

          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?

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          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

          L 1 Reply Last reply
          0
          • D 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

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            :thumbsup:

            Luc Pattyn [My Articles] Nil Volentibus Arduum

            1 Reply Last reply
            0
            • R Razanust

              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?

              W Offline
              W Offline
              Wayne Gaylard
              wrote on last edited by
              #6

              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!

              D 1 Reply Last reply
              0
              • D Dave Kreskowiak

                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

                W Offline
                W Offline
                Wayne Gaylard
                wrote on last edited by
                #7

                Thanks, 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!

                1 Reply Last reply
                0
                • W Wayne Gaylard

                  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!

                  D Offline
                  D Offline
                  Dalek Dave
                  wrote on last edited by
                  #8

                  That's an excellent answer.

                  ------------------------------------ I will never again mention that I was the poster of the One Millionth Lounge Post, nor that it was complete drivel. Dalek Dave CCC Link[^] Trolls[^]

                  W 1 Reply Last reply
                  0
                  • D Dalek Dave

                    That's an excellent answer.

                    ------------------------------------ I will never again mention that I was the poster of the One Millionth Lounge Post, nor that it was complete drivel. Dalek Dave CCC Link[^] Trolls[^]

                    W Offline
                    W Offline
                    Wayne Gaylard
                    wrote on last edited by
                    #9

                    Cheers.

                    Live for today. Plan for tomorrow. Party tonight!

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups