Results 1 to 22 of 22

Thread: This Old Loop

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 2000
    Location
    The Glove state
    Posts
    28

    Exclamation

    Hey
    I have a loop with some stuff in it and know I was hoping some one chould tell me how to make it so when I press Esc the loop stops

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    If you stick in a DoEvents somewhere in your loop, then put a definition for the Form_KeyPress event, and check for the Escape key.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Aug 2000
    Location
    The Glove state
    Posts
    28

    Question

    Ok
    how do I do that?

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Add a Label and a CommandButton. Then add the following code to the form:
    Code:
    Dim gbStop As Boolean
    Private Sub Command1_Click()
        Dim i As Long
        For i = 1 To 100000
            DoEvents
            If gbStop Then Exit For
            Label1.Caption = i
        Next i
    End Sub
    Private Sub Command1_KeyPress(KeyAscii As Integer)
        If KeyAscii = vbKeyEscape Then gbStop = True
    End Sub
    Private Sub Form_Load()
        gbStop = False
    End Sub
    Run it, then press the button. Hit escape when you want it to stop.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  5. #5
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    Declare a module level variable that you can use as a flag:
    Code:
    Private blnEscapePressed As Boolean
    Set this flag to True in the KeyDown event if the key pressed is Escape:
    Code:
    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
        If KeyCode = vbKeyEscape Then
            blnEscapePressed = True
        End If
    End Sub
    Check for this flag in your loop and if it's set to True then exit:
    Code:
    Do While True
        'your code
        DoEvents
        If blnEscapePressed Then
            Exit Do
        End If
    Loop
    Use Exit For if you have a For..Next loop instead of a Do loop.

    You must set the KeyPreview property of the Form to True for this to work!

    Good luck!

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Aug 2000
    Location
    The Glove state
    Posts
    28

    Thumbs down

    ok
    but my loop is sill going

  8. #8
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Are you sure that the flag variable is a GLOBAL variable?
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Aug 2000
    Location
    The Glove state
    Posts
    28

    Question

    A GLOBAL variable?

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Aug 2000
    Location
    The Glove state
    Posts
    28

    Exclamation

    Ok
    Thanks Joacim Andersson, That code looks like what I want, but it still doesn't work! Please someone HELP ME!!!!!!!

  11. #11
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    Can you post your loop code here? It's much easier to help if we get your code.

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Aug 2000
    Location
    The Glove state
    Posts
    28

    Thumbs down

    No I dont really want my loop to be seen by everyone that looks but I will email you if you dont mind.

  13. #13
    Addicted Member Guru's Avatar
    Join Date
    May 2000
    Location
    sulking in the cupboard under the stairs
    Posts
    237
    Don't be shy Droidking!

    My code is crap as well

  14. #14

    Thread Starter
    Junior Member
    Join Date
    Aug 2000
    Location
    The Glove state
    Posts
    28

    Red face

    Ok Ok here is my code if you can help please do! dont laughor ill have to change me user name!

    Dim t As Long
    -----------------------------------------------------------
    Sub MouseDown(Optional Left As Boolean = True, Optional Right As Boolean, Optional Middle As Boolean, Optional Click As Boolean)
    mouse_event -Left * 2 - Right * 8 - Middle * 32, 0&, 0&, 0&, 0&
    If Click Then mouse_event -Left * 4 - Right * 16 - Middle * 64, 0, 0, 0, 0
    End Sub
    -----------------------------------------------------------
    Sub MouseUp(Optional Left As Boolean = True, Optional Right As Boolean, Optional Middle As Boolean)
    mouse_event -Left * 4 - Right * 16 - Middle * 64, 0, 0, 0, 0
    End Sub
    -----------------------------------------------------------
    Private Sub Command1_Click()
    Form1.Hide
    x = 20
    y = 40
    Do While True
    If x = 450 Then x = 0
    If y = 450 Then y = 0
    x = x + 5
    y = y + 5
    t = SetCursorPos(x, y)
    If x = 300 Then MouseDown
    If x = 300 Then MouseUp
    If x = 300 Then MouseDown
    If x = 300 Then MouseUp
    Call Sleep(75)
    DoEvents
    If blnEscapePressed Then
    Exit Do
    End If
    Loop
    End Sub
    -----------------------------------------------------------
    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = vbKeyEscape Then
    blnEscapePressed = True
    End If
    End Sub
    ------------------------------------------------------------ This is a Module


    Private blnEscapePressed As Boolean

    Declare Function SetCursorPos& Lib "user32" (ByVal x As Long, ByVal y As Long)

    Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

    Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)


  15. #15
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    You put the declaration of the blnEscapePressed in a module outside of the form. So it can't be used in the form. The solution is to change the declaration from Private into Public.
    Code:
    Public blnEscapePressed As Boolean
    Now it should work!
    Good luck!

  16. #16

    Thread Starter
    Junior Member
    Join Date
    Aug 2000
    Location
    The Glove state
    Posts
    28

    Unhappy

    Sorry
    Still doen't manybe its my computer can someone else tryit Please!

  17. #17

    Thread Starter
    Junior Member
    Join Date
    Aug 2000
    Location
    The Glove state
    Posts
    28

    Exclamation AAAAAAAAAHHHHHHHHHH!!!!!!!!!!!!

    AAAAAAAAAAAHHHHHHHHHHHHHHHHHHHHHHHHH
    Im Going CRAZY trying to get this #&$@ing program to work can some one please help!
    Always let the Wookiee Win!

  18. #18
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    I don't have the answer but I can tell you why you are pulling your hair out..

    Once inside the loop the app doesn't recoginize anything outside the loop...how to get around it is a horse of a different color.

    ie.
    Code:
    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
       blnEscapePressed = False
        If vbKeyEscape Then
            blnEscapePressed = True
            MsgBox blnEscapePressed
        End If
     
    End Sub
    if you don't click the command with the loop it messages
    and it changes the value of blnescapepressed.
    however once you click the command button
    it doesnt' work...why??? that is the question
    not the code or variables...

    how do you get inside the loop is the question.
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  19. #19

    Thread Starter
    Junior Member
    Join Date
    Aug 2000
    Location
    The Glove state
    Posts
    28

    Smile

    Thanks
    Ok so if there is no way to get out of a loop is there something I can use instead of a loop????
    Always let the Wookiee Win!

  20. #20
    Frenzied Member Mark Sreeves's Avatar
    Join Date
    Nov 1999
    Location
    UK
    Posts
    1,845
    Apart from scope problems,
    You are trying to detect a kepress in the form but the form is hidden so the event is never triggered.

    try this instead:


    paste this in your form
    Code:
    Private Declare Function GetKeyState Lib "user32.dll" (ByVal nVirtKey As Long) As Integer
    
    
    Function EscapeIsPressed() As Boolean
    
    
      Dim keystate As Integer  
      
      keystate = GetKeyState(vbKeyEscape)
      ' Check the &H8000 bit of the return value.
      EscapeIsPressed = keystate And &H8000
    End Function
    and this in your loop

    Code:
    DoEvents
    If EscapeIsPressed Then
    Exit Do
    End If
    don't forget to unload or re-show your form again after the loop has exited.


    Mark
    -------------------

  21. #21
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    the problem with your code is in the
    Me.Hide
    since you are asking it to change the value from a form event Ie..form keypress...and the form is invisible it can't work...it you leave the form visible then your code will work.
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  22. #22

    Thread Starter
    Junior Member
    Join Date
    Aug 2000
    Location
    The Glove state
    Posts
    28

    Smile Mark Sreeves You RULE!!!!!!

    THANKS!!
    Thanks man it worked! your lucky you aren't here Because I chould kiss you(At least a Huge)!!!!!YOU RULE!!!!!!

    And HeSaidJoe Thanks for your help too
    Always let the Wookiee Win!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width