Problem moving image around screen

I have wrote a program in VB to move a image of a car around the screen. The image should start at the bottom left hand side of the screen, move to the top and then down to where it began.
I can get the image to move to the right and top but I am unable to move it to the left and down to the starting position.  Option Explicit

Private Sub cmdExit_Click()
End
End Sub

Private Sub cmdGo_Click()
Do
imgCar.Left = imgCar.Left + txtSpeed.Text
Loop Until imgCar.Left > 10000
Do
imgCar.Top = imgCar.Top - txtSpeed.Text
Loop Until imgCar.Top > 2000

Do
imgCar.Left = imgCar.Left - txtSpeed.Text
Loop Until imgCar.Left < 1000

Do
imgCar.Top = imgCar.Top - txtSpeed.Text
Loop Until imgCar.Top > 1680


End Sub

Private Sub cmdStart_Click()


lblPrompt.Caption = "Enter Speed then press Go"
'here the speed box appears for the first time
'and the above text appears after the next statement
txtSpeed.Visible = True
txtSpeed.Text = "0"
cmdGo.Enabled = False
'move the pointer into the speed setting box
txtSpeed.SetFocus

End Sub

Private Sub txtSpeed_Change()
cmdGo.Enabled = True
End Sub

GsmithAsked:
Who is Participating?
 
caraf_gConnect With a Mentor Commented:
Option Explicit

Private Sub cmdExit_Click()
End
End Sub

Private Sub cmdGo_Click()
Do
imgCar.Left = imgCar.Left + txtSpeed.Text
Loop Until imgCar.Left >= 10000
Do
imgCar.Top = imgCar.Top - txtSpeed.Text
Loop Until imgCar.Top <= 2000

Do
imgCar.Left = imgCar.Left - txtSpeed.Text
Loop Until imgCar.Left <= 1000

Do
imgCar.Top = imgCar.Top + txtSpeed.Text
Loop Until imgCar.Top >= 8000 'or some other large number.

End Sub

Private Sub cmdStart_Click()


lblPrompt.Caption = "Enter Speed then press Go"
'here the speed box appears for the first time
'and the above text appears after the next statement
txtSpeed.Visible = True
txtSpeed.Text = "0"
cmdGo.Enabled = False
'move the pointer into the speed setting box
txtSpeed.SetFocus

End Sub

Private Sub txtSpeed_Change()
cmdGo.Enabled = True
End Sub
0
 
smeggheadCommented:
What's the engine size of the car ??
0
 
GsmithAuthor Commented:
Edited text of question.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
GsmithAuthor Commented:
oops !Is that better smeghead?
0
 
GsmithAuthor Commented:
Edited text of question.
0
 
caraf_gCommented:
Try changing
Do
imgCar.Top = imgCar.Top - txtSpeed.Text
Loop Until imgCar.Top > 1680


to
Do
imgCar.Top = imgCar.Top - txtSpeed.Text
Loop Until imgCar.Top < 1680


0
 
smeggheadCommented:
And probably...

imgCar.Top = imgCar.Top - txtSpeed.Text
Loop Until imgCar.Top > 2000

to

imgCar.Top = imgCar.Top - txtSpeed.Text
Loop Until imgCar.Top < 2000
0
 
smeggheadCommented:
Just a thought, you might find it easier to use a for...next loop for this purpose, it's a bit easier to maintain..

For CurX=1 to 2000 step val(txtspeed.text)
imgcar.Left=CurX
Next CurX
0
 
caraf_gCommented:
Ah. oops! I forgot, you want to move from bottom left to top right and back...

Do
imgCar.Left = imgCar.Left + txtSpeed.Text
Loop Until imgCar.Left > 10000
Do
imgCar.Top = imgCar.Top - txtSpeed.Text
Loop Until imgCar.Top < 2000

Do
imgCar.Left = imgCar.Left - txtSpeed.Text
Loop Until imgCar.Left < 1000

Do
imgCar.Top = imgCar.Top + txtSpeed.Text
Loop Until imgCar.Top > a bigger number, e.g. 10000?




0
 
Erick37Commented:
And also...

Private Sub cmdExit_Click()
End
End Sub

to

Private Sub cmdExit_Click()
    Unload Me
End Sub

Using End statement will terminate the program without unloading from memory.

And maybe use Val(txtSpeed.Text) to convert the string to a number.
0
 
GsmithAuthor Commented:
I have altered the program, I find that I can move the image from the bottom left corner to the bottom right corner, the image also moves to the top right hand corner and then to the top left hand corner. The problem I have now with the loop is that I cannot move it from the top left hand corner back to the bottom left hand corner



Option Explicit

Private Sub cmdExit_Click()
End
End Sub

Private Sub cmdGo_Click()
Do
imgCar.Left = imgCar.Left + txtSpeed.Text
Loop Until imgCar.Left >= 10000
Do
imgCar.Top = imgCar.Top - txtSpeed.Text
Loop Until imgCar.Top <= 2000

Do
imgCar.Left = imgCar.Left - txtSpeed.Text
Loop Until imgCar.Left <= 1000

Do
imgCar.Top = imgCar.Top - txtSpeed.Text
Loop Until imgCar.Top <= 2000

End Sub

Private Sub cmdStart_Click()


lblPrompt.Caption = "Enter Speed then press Go"
'here the speed box appears for the first time
'and the above text appears after the next statement
txtSpeed.Visible = True
txtSpeed.Text = "0"
cmdGo.Enabled = False
'move the pointer into the speed setting box
txtSpeed.SetFocus

End Sub

Private Sub txtSpeed_Change()
cmdGo.Enabled = True
End Sub
0
 
GsmithAuthor Commented:
Thanks, program is now working

 
0
All Courses

From novice to tech pro — start learning today.