Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2003-11-11
2
Medium Priority
?
193 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 2000 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 86

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

704 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