How can I stop shape after 5 seconds of moving?

I know how to move it and how to stop it but don't know how to make it stop IN 5 SECONDS.
This is the rest of the code that I have done but the 5 seconds part is still left. BTW, I tried to use loop but it didn't work out.
Thanks everyone in advance. :)

Private Sub Command1_Click()
Timer1.Enabled = True
End Sub

Private Sub Form_Load()
Command1.Caption = "Move"
End Sub

Private Sub Timer1_Timer()
Shape1.Left = Shape1.Left + 50
End Sub

Dani AI

Generated

Good start by — using a Timer is the right idea. The reason a loop didn’t work is simple: a tight loop blocks the UI thread, so the Timer events never get a chance to run. A more robust pattern is to record the start time when movement begins and then stop the Timer when elapsed time reaches 5,000 ms. That avoids depending on an exact tick count and keeps motion smooth.

An easy, reliable implementation in classic VB is to use the Windows millisecond counter (GetTickCount) and a short Timer interval for smooth movement. Example:

Private Declare Function GetTickCount Lib "kernel32" () As Long

Private startTick As Long

Private Sub Command1_Click()
    startTick = GetTickCount()
    Timer1.Interval = 50   ' 50 ms for smoother motion
    Timer1.Enabled = True
End Sub

Private Sub Timer1_Timer()
    If GetTickCount() - startTick >= 5000 Then
        Timer1.Enabled = False
    Else
        Shape1.Left = Shape1.Left + 5
    End If
End Sub

Notes and quick troubleshooting:

  • Timer.Interval is in milliseconds; lower values give smoother motion but more CPU wakeups. Tune the increment vs. interval (e.g., 5 px @ 50 ms yields ~100 px/s).
  • Avoid doing long work on the UI thread while the Timer is enabled — that will pause Timer events. Use timers or background processing instead of loops or blocking calls.
  • VB’s built‑in Timer() (seconds since midnight) wraps at midnight; GetTickCount is usually preferable for short intervals (it wraps only after many days).
  • If the shape still “overshoots” exactly 5 seconds, reduce the interval and calculate per-tick displacement so total travel matches the time window.

Recommended Answers

All 3 Replies

i could be wrong,
the timers interval is in miliseconds. set it to 1,000
and set the timer disabled.

private x as integer
 
private sub form_load()
x% = 1
end sub
 
private sub command1_click()
timer1.enabled = true
end sub
 
private sub timer1_timer
shape1.left = shape1.left + 50
x% = x% + 1
if x% = 5 then timer1.enabled = false
end sub

the interval might not be 1,000.
im not sure, basically you would just want the timer to fire off every second, everytime it does x increments by 1 until it reaches 5 and the timer shuts off.

Thank you so much. I didn't realize that it was so simple. I thought I needed a loop or something.

no problem

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.