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
Solved

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

Posted on 2003-11-11
2
188 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
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…

839 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