Solved

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

Posted on 2003-11-11
2
185 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
Comment Utility
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
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now