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

AI Thread Summary
The discussion revolves around a coding issue related to moving a picture box (referred to as "Robot") on a screen using a timer in a programming environment. The user is experiencing glitches due to improper handling of the Robot's position updates, particularly with nested If statements that are causing execution problems. Suggestions include rearranging the code to avoid nesting, adding Else statements for better flow control, and incorporating comments for clarity. These adjustments aim to improve code organization and functionality, ultimately resolving the movement issues with the Robot.
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.
 
Technology news 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!
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top