Fixing the Timer1 Glitch - Moving a Picture Box on a Screen

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
1 replies · 2K views
Marghk
Messages
20
Reaction score
0
Just have to get a picture box to move around a screen, I'm running the program by a timer. I'll show set integers below.

Timer = Timer1
Picture Box = Robot
Dim Robotpace as integer


Basically, when you click on the start button it enables the timer. Here's the coding.



Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

RobotPace = 8
Robot.Left -= RobotPace
If Robot.Left <= 1 Then
Robot.Left = False
Robot.Top -= RobotPace
If Robot.Top <= 1 Then
Robot.Top = False
+ Robot.Left += RobotPace
+ If Robot.Left <= 1 Then
+ Robot.Left = False


End If
End If

End Sub



The lines with the (+'s) at the start of them are the beginning of the error. At this point, I'm aware that the timer mistakes the + and - statements in the Robot.Left movements and it completely glitches.

I just can't fix this.
 
on Phys.org


Hi there,

From what I can see, it looks like the issue might be with the placement of your If statements. You have an If statement nested within another If statement, and it may be causing the code to not execute properly. I would suggest rearranging the code so that the If statements are not nested, and also adding an Else statement to handle the case when the Robot.Left is not <= 1.

Also, it might be helpful to add some comments to your code to make it more clear what each section is doing. For example, you could add a comment above the line "RobotPace = 8" saying something like "Set the pace of the robot's movement" to help make your code more organized and easier to understand.

I hope this helps, and good luck with your project!