Solved

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

Posted on 2003-11-11
2
189 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

756 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