Results 1 to 13 of 13

Thread: Moving PictureBox with Timer

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2021
    Posts
    19

    Moving PictureBox with Timer

    I want to create a program wherein when I click a Button, the PictureBox that is of the same width and height as the form moves down but I want the Timer to stop right after the PictureBox leaves the frame/form. And when I click another Button, the PictureBox will move back up but it will stop when it's at the center of the form, basically at the same location it was before moving down. The form's size is 700, 1000 if that helps. With my code right now, it does not stop right after it leaves the frame or when it's already at the center. Here is my code:

    Code:
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Timer1.Enabled = True
        End Sub
    
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            PictureBox1.Location = New Point(PictureBox1.Location.X, PictureBox1.Location.Y + 9)
            If (PictureBox1.Location = New Point(700, 1100)) Then
            Timer1.Enabled = False
            End If
    
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            Timer2.Enabled = True
        End Sub
    
        Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
            PictureBox1.Location = New Point(PictureBox1.Location.X, PictureBox1.Location.Y - 9)
            If (PictureBox1.Location = New Point((Me.Width / 700) - (PictureBox1.Width / 700), (Me.Height / 1000) - (PictureBox1.Height / 1000))) Then
                Timer2.Enabled = False
            End If
        End Sub

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

    Re: Moving PictureBox with Timer

    Start the Timer:
    vb.net Code:
    1. PictureBox1.Top = 0
    2. Timer1.Start()
    Move the PictureBox:
    vb.net Code:
    1. PictureBox1.Top += 9
    Stop the Timer:
    vb.net Code:
    1. If PictureBox1.Top >= ClientSize.Height Then
    2.     Timer1.Stop()
    3. End If
    OT, don't just accept the default, generic names for components you add in the designer. Change the name to something meaningful and descriptive when you add them. For quick and dirty demos and tests, maybe it's not a big deal. For anything else, there's good naming conventions.

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: Moving PictureBox with Timer

    Welcome to VBForums!

    Instead of checking the entire .Location , you should be checking just the part that changes (the .Location.Y).

    It would also be a good idea to not check for an exact number, but also allow any value over (or under) that value to work too.

    eg:
    Code:
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            PictureBox1.Location = New Point(PictureBox1.Location.X, PictureBox1.Location.Y + 9)
            If (PictureBox1.Location.Y >= 1100) Then
               Timer1.Enabled = False
            End If
    
        End Sub
    edit: oops, I was slow to type!

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Oct 2021
    Posts
    19

    Re: Moving PictureBox with Timer

    This works but how can I do it that when the PictureBox stops moving down after it leaves the frame, it will move back up at the same location where it was before it moved down when I click a button? It already stops after it leaves the frame, this is the only problem I have left.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Oct 2021
    Posts
    19

    Re: Moving PictureBox with Timer

    Hello, thank you for the welcome. I also tried this code and like the one posted above, it works. Now, I want the PictureBox to move back up and stop at where it was before it moved down when I click a button. The PictureBox is the same width and height as the form so it has to stop at the center of the form, I think? The PictureBox fit the entire form before it moved down, by the way.

    Quote Originally Posted by si_the_geek View Post
    Welcome to VBForums!

    Instead of checking the entire .Location , you should be checking just the part that changes (the .Location.Y).

    It would also be a good idea to not check for an exact number, but also allow any value over (or under) that value to work too.

    eg:
    Code:
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            PictureBox1.Location = New Point(PictureBox1.Location.X, PictureBox1.Location.Y + 9)
            If (PictureBox1.Location.Y >= 1100) Then
               Timer1.Enabled = False
            End If
    
        End Sub
    edit: oops, I was slow to type!

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

    Re: Moving PictureBox with Timer

    I think I misinterpreted your original question. I thought you meant that you wanted it to jump straight back to where it was and move down again, but now I think that you mean that you want it to move gradually back up again. In that case, I'd do this:
    vb.net Code:
    1. Private movingDown As Boolean = True
    2.  
    3. Private Sub Timer1_Tick(...) Handles Timer1.Tick
    4.     If movingDown Then
    5.         PictureBox1.Top += 9
    6.  
    7.         If PictureBox1.Top >= ClientSize.Height Then
    8.             Timer1.Stop()
    9.             movingDown = False
    10.         End If
    11.     Else
    12.         PictureBox1.Top -= 9
    13.  
    14.         If PictureBox1.Top <= 0 Then
    15.             Timer1.Stop()
    16.             movingDown = True
    17.         End If
    18.     End If
    19. End Sub

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Oct 2021
    Posts
    19

    Re: Moving PictureBox with Timer

    I tried it and it does move back up but it doesn't stop at its original position, it moves up then exits the frame/form.

    Code:
    Private movingDown As Boolean = True
    
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
                If movingDown Then
                PictureBox1.Top += 9
    
                If PictureBox1.Top >= ClientSize.Height Then
                    Timer1.Stop()
                    movingDown = False
                End If
             End If
    
        Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
            PictureBox1.Top -= 9
    
            If PictureBox1.Top <= 0 Then
                Timer1.Stop()
                movingDown = True
            End If
        End Sub
    
    Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click
            Timer1.Start()
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Timer2.Start()
        End Sub
    Quote Originally Posted by jmcilhinney View Post
    I think I misinterpreted your original question. I thought you meant that you wanted it to jump straight back to where it was and move down again, but now I think that you mean that you want it to move gradually back up again. In that case, I'd do this:
    vb.net Code:
    1. Private movingDown As Boolean = True
    2.  
    3. Private Sub Timer1_Tick(...) Handles Timer1.Tick
    4.     If movingDown Then
    5.         PictureBox1.Top += 9
    6.  
    7.         If PictureBox1.Top >= ClientSize.Height Then
    8.             Timer1.Stop()
    9.             movingDown = False
    10.         End If
    11.     Else
    12.         PictureBox1.Top -= 9
    13.  
    14.         If PictureBox1.Top <= 0 Then
    15.             Timer1.Stop()
    16.             movingDown = True
    17.         End If
    18.     End If
    19. End Sub

  8. #8
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Moving PictureBox with Timer

    So, fix it.
    Code:
     If PictureBox1.Top <= 0 Then
    Save the original position and check for that instead of 0 in the above line.
    Or calculate the center position you mentioned and use that instead of 0.

    If you want it exactly in the same place, then you should set it to that place specifically when you turn the timer off, to prevent "creeping".
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Oct 2021
    Posts
    19

    Re: Moving PictureBox with Timer

    Sorry, I'm new to programming so I don't know much. Thank you, though.

  10. #10
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Moving PictureBox with Timer

    Ok, I didn't look at your code, just jmc's.

    In your code, you're using two timers.
    You use the second timer to move the picturebox up, but which timer do you stop when you've reached the top?
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

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

    Re: Moving PictureBox with Timer

    Quote Originally Posted by passel View Post
    In your code, you're using two timers.
    You use the second timer to move the picturebox up, but which timer do you stop when you've reached the top?
    There is no good reason, based on the information provided here, to use a second Timer. There's only one thing happening at a time so there's only a need for one Timer. The OP has made some changes based on my code but left some things unchanged. That's the mistake.

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

    Re: Moving PictureBox with Timer

    I'd probably do something like this if you just want to reverse the movement when you exceed the Y position boundaries...

    Code:
    Public Class Form1
    
        Private WithEvents tmr As New Timer With {.Interval = 250}
        Private increment As Integer = 9
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            PictureBox1.Top = 12
            increment = 9
            tmr.Enabled = Not tmr.Enabled
        End Sub
    
        Private Sub tmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmr.Tick
            PictureBox1.Top += increment
            If PictureBox1.Top <= 0 Or PictureBox1.Bottom >= Me.ClientSize.Height Then increment = -increment
        End Sub
    
    End Class

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

    Re: Moving PictureBox with Timer

    To answer your original question...

    Code:
    Public Class Form13
    
        Private WithEvents tmr As New Timer With {.Interval = 100}
        Private increment As Integer = 9
    
        Private Sub Buttons_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDown.Click, btnUp.Click
            tmr.Start
        End Sub
    
        Private Sub tmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmr.Tick
            PictureBox1.Top += increment
            If PictureBox1.Top <= 500 Or PictureBox1.Top >= Me.ClientSize.Height Then increment = -increment : tmr.Stop()
        End Sub
    
    End Class

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