-
Oct 13th, 2021, 01:56 AM
#1
Thread Starter
Junior Member
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
-
Oct 13th, 2021, 02:08 AM
#2
Re: Moving PictureBox with Timer
Start the Timer:
vb.net Code:
PictureBox1.Top = 0
Timer1.Start()
Move the PictureBox:
Stop the Timer:
vb.net Code:
If PictureBox1.Top >= ClientSize.Height Then
Timer1.Stop()
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.
Last edited by jmcilhinney; Oct 13th, 2021 at 02:30 AM.
-
Oct 13th, 2021, 02:13 AM
#3
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!
-
Oct 13th, 2021, 09:18 AM
#4
Thread Starter
Junior Member
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.
-
Oct 13th, 2021, 09:38 AM
#5
Thread Starter
Junior Member
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.
Originally Posted by si_the_geek
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!
-
Oct 13th, 2021, 09:39 AM
#6
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:
Private movingDown As Boolean = True
Private Sub Timer1_Tick(...) Handles Timer1.Tick
If movingDown Then
PictureBox1.Top += 9
If PictureBox1.Top >= ClientSize.Height Then
Timer1.Stop()
movingDown = False
End If
Else
PictureBox1.Top -= 9
If PictureBox1.Top <= 0 Then
Timer1.Stop()
movingDown = True
End If
End If
End Sub
-
Oct 13th, 2021, 10:02 AM
#7
Thread Starter
Junior Member
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
Originally Posted by jmcilhinney
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:
Private movingDown As Boolean = True
Private Sub Timer1_Tick(...) Handles Timer1.Tick
If movingDown Then
PictureBox1.Top += 9
If PictureBox1.Top >= ClientSize.Height Then
Timer1.Stop()
movingDown = False
End If
Else
PictureBox1.Top -= 9
If PictureBox1.Top <= 0 Then
Timer1.Stop()
movingDown = True
End If
End If
End Sub
-
Oct 13th, 2021, 10:37 AM
#8
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
-
Oct 13th, 2021, 02:49 PM
#9
Thread Starter
Junior Member
Re: Moving PictureBox with Timer
Sorry, I'm new to programming so I don't know much. Thank you, though.
-
Oct 14th, 2021, 10:46 AM
#10
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
-
Oct 14th, 2021, 07:04 PM
#11
Re: Moving PictureBox with Timer
Originally Posted by passel
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.
-
Oct 14th, 2021, 08:34 PM
#12
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 14th, 2021, 08:58 PM
#13
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|