I need to get an image to move a number of twips at a timer interval determined by a variable (no problem) for a number of iterations through a for/next loop (problem).
I've tried it without variables without success:
first:
Form_Load()
Dim jumps as Integer
jumps = 1
For jumps = 1 To 5
Image1.Left = Image1Left + 200
Image1.Top = Image1.Top - 50
Timer4.Enabled = True
MiniCAM.Refresh
Next

then the timer, with interval of 1000:
Private Sub Timer4_Timer()
Label9.Visible = True
Timer4.Enabled = False
End Sub

Result is a single iteration that moves the image left +200 up by 50. Label9 does it's thing once, after 1 second, but that's all.

I've spent too many hours trying this different ways - Why doesn't this work?
Thanks in advance!

Hi,

Try moving the Image location bit to the timer:

Private Sub Timer4_Timer()
Image1.Left = Image1Left + 200
Image1.Top = Image1.Top - 50
Timer4.Enabled = False
End Sub

and then use the loop to turn the timer on:

For jumps = 1 To 5
Timer4.Enabled = True
Next jumps

HTH,

Chris.

Hi,

Try moving the Image location bit to the timer:

Private Sub Timer4_Timer()
Image1.Left = Image1Left + 200
Image1.Top = Image1.Top - 50
Timer4.Enabled = False
End Sub

and then use the loop to turn the timer on:

For jumps = 1 To 5
Timer4.Enabled = True
Next jumps

HTH,

Chris.

Thanks, but that diesn't work either.
For kicks, I created a label and set the caption = the 'jumps' variable in the timer loop. Result was one timer cycle later, the image moved 1 x the distance, yet the label caption read '5'.
Hmmm....?????

Oops!

Ok:

Declaration in Form Header (after Option Explicit):

Private intCounter As Integer

then -

Form_Load()
intCounter = 0
Timer4.Enabled = True
End Sub

Private Sub Timer4_Timer()
If intCounter < 6 Then
Image1.Left = Image1.Left + 200
Image1.Top = Image1.Top - 50
intCounter = intCounter + 1
Else
Timer4.Enabled = False
End If
End Sub

Chris.

Amazing.

I would have thought that declaring the loop counter as a static variable would have worked, yet it didn't. How does the Private declaration accomplish this?

From one Chris to another,
Thanks!

Placing the intCounter in the header as a Private variable makes it available to all the subs and functions in the form (or module for that matter).

Crucially, it's independent of the Timer event - even though the Timer inherits it.

I know you can use Static variables to achieve the same thing in a Timer but since I already have a method that works perfectly well I've never gone to the trouble to find out how (I have enough bother with the bits that don't work!).

Glad I could help.

Chris.

Well, I never did get this to work with a Static variable. Thanks to you, I'm not going to try anything else either. Now if I could only make the time to get this into the procedure it needs to be in........with the real timer event....
Thank-you again for taking the time to share your solution.

This article has been dead for over six months. Start a new discussion instead.