Results 1 to 5 of 5

Thread: [2005] image changing at timer interval

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2007
    Posts
    56

    [2005] image changing at timer interval

    Hi, I've had a problem with changing my Picture Box's image at a timer interval.

    The timer starts as disabled, and when enabled it has an interval of 1000.

    Here's the code:

    Code:
    Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
            If img_ball.Image Is Bouncing_Ball.My.Resources.ball2 Then
                img_ball.Image = Bouncing_Ball.My.Resources.ball1
            Else
                If img_ball.Image Is Bouncing_Ball.My.Resources.ball1 Then
                    img_ball.Image = Bouncing_Ball.My.Resources.ball2
                End If
            End If
        End Sub

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,458

    Re: [2005] image changing at timer interval

    whats the problem?

  3. #3

    Thread Starter
    Member
    Join Date
    Oct 2007
    Posts
    56

    Re: [2005] image changing at timer interval

    The image doesn't change, it should invert the colors, but it doesn't.

  4. #4
    Addicted Member
    Join Date
    Oct 2006
    Location
    Chennai, India
    Posts
    198

    Re: [2005] image changing at timer interval

    Check the initial starting values for the Timer object. Also set this object in Enabled state initially loading the application. Check the Properties values during design time of the Timer object.
    Regards
    Srinivasan Baskaran
    India

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,274

    Re: [2005] image changing at timer interval

    The problem is that your If conditions are ALWAYS going to evaluate to False. Every time you get a property value from My.Resources it creates a new object. If you get the same property twice it will return a different object each time, so the first object is not the same object as the second, so a comparison will always return False. What you need to do is get each resource once and once only:
    vb.net Code:
    1. Private ball1 As Image = My.Resources.ball1
    2. Private ball2 As Image = My.Resources.ball2
    Now your code becomes:
    vb.net Code:
    1. Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
    2.         If img_ball.Image Is Me.ball2 Then
    3.             img_ball.Image = Me.ball1
    4.         Else If img_ball.Image Is Me.ball1 Then
    5.             img_ball.Image = Me.ball2
    6.         End If
    7.     End Sub
    Then you'll be referring to the same two Image objects every time and it will work.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width