Solved

interrupting a FORloop temporarily to determine whether a MouseDown event has occured

Posted on 2003-11-11
2
187 Views
Last Modified: 2010-05-01
ok, i have a decent knowledge of programming but am struggling with VB a bit at the moment...

i have an amimation sequence that moves some pictures about the screen in a FOR loop.  I then have a MouseDown procedure which determines where the user has clicked.  What i want to do is every time the new positions are determined in teh FOR loop is to chekc to see if the mouse button has been pressed and if its coordinates match those of one of the animated pictures... simple... but not for me

what happens is that the animation loop runs till completion and cant be interrupted by the MouseDown event... which only occurs either before or after running the animation sub.  I think i need a CALL or EVALUATE kind of command but i cant seem to get CALL to check whether the MouseDown has occurred

thanks in advance for any comments... they are much appreciated

Andrew

my code looks a little like this
_________________________________________________
Public Sub cmdStart_Click()
'the animation loop
    For j = 1 To 2000

       CODE FOR THE ANIMATION HERE
       
       'need to evaluate the mousedown procedure here somehow to see
'whether the mouse click position matches that of one of the animated dots
       
    Next j
End Sub
____________________________________________________
Public Sub Background_MouseDown(Button As Integer, _
Shift As Integer, X As Single, Y As Single)

'Background is the object picture box in which the animation occurs

If Button = 1 Then
    For i = 0 To n - 1
        If Abs(X - dot(i).Left - 72.5) < 100 _
            And Abs(Y - dot(i).Top - 72.5) < 100 _
            Then MsgBox "Well done, you got one"
 
    Next i
End If

End Sub
_______________________________________________________
0
Comment
2 Comments
 
LVL 44

Accepted Solution

by:
Arthur_Wood earned 500 total points
ID: 9723772
add a DoEvents call in the For...Next Loop -like this

 For j = 1 To 2000

       CODE FOR THE ANIMATION HERE

       if j Mod 100 then DoEvents

       'need to evaluate the mousedown procedure here somehow to see
'whether the mouse click position matches that of one of the animated dots
       
    Next j
This will invoke the DoEvents method (to allow the operaiting system to handle any pending events, like the MouseDown, but only every so often - once every 100 iterations of the Loop.  If you invoke DoEvents TOO frequently, the overall perfomance will suffer, and if you invoke it too INFREQUENTLY, the responsiveness will suffer.

AW
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 9725367
An alternative would be to move your animation code into a timer sub and set the timer interval to how long you want inbetween animation frames.  Like this:

Dim j As Integer

Private Sub Form_Load()
    j = 1
End Sub

Private Sub Timer1_Timer()
    ' CODE FOR THE ANIMATION OF ONE FRAME HERE
    ' You may have to make some of your variables static, or global to keep their values
    ' form Timer event to event, or you could create a class to keep the Animation
    ' values persistent
   
    j = j + 1
    If j > 2000 Then
        j = 1
    End If
End Sub

Public Sub Background_MouseDown(Button As Integer, _
    Shift As Integer, X As Single, Y As Single)

    'Background is the object picture box in which the animation occurs
    If Button = 1 Then
        For i = 0 To n - 1
            If Abs(X - dot(i).Left - 72.5) < 100 _
                    And Abs(Y - dot(i).Top - 72.5) < 100 Then
                MsgBox "Well done, you got one"
            End If
        Next i
    End If
End Sub

Idle_Mind
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VBA filters 2 61
Error when passing an argument to a vb6 that contains  '/' 7 37
MsgBox 4 61
Set email body to html using vbscript 6 41
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question