exit a loop with key press

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ronn
    New Member
    • Jan 2014
    • 3

    exit a loop with key press

    Hi guys, I've just joined the community today 01/01/2014 but I have used the site in the past just by browsing for answers, so first I must thank you guys for all the help I recieved in the past. Thanks to You all.

    A little about my self.
    I have been programing in vb since vb3 but I am getting a bit long in the tooth to start useing anything above vb6 which I now use to do all my programing.
    I am far from being proficient, but I usualy get by, and finish what I start, so please go easy on me and don't assume I will know what you may think of as common knowlege.
    Also I don't ask for help untill I have exhausted all I know trying to solve the problem.

    So to my present problem guys.
    I am creating a program for my 13 year old grandaughter that will scroll her sheet music up screen and it works very well untill I try to stop the scroll.
    I have spent 14 days trying everything In know without success. I have tried using the keydown code which works, but only when the scroll has finished (pointless)
    Stopping the scroll may not seem very important, but when you consider there could be 4 or more pages it can take some time to scroll to the end. I would even consider some way to stop it after it has scrolled 1 page but every thing I try only works after it has reached the end of the routine I am running.
    I need some help guys, does anyone know a way to stop it scrolling with a key press or a mouse click.
    Thanks guys.
  • zmbd
    Recognized Expert Moderator Expert
    • Mar 2012
    • 5501

    #2
    Hello Ron:

    Welcome to being an active member of the community!

    I am sure that you are expecting this: please post the code that you are using, the target OS, the compiler version, and the record source for the music (it need not be the actual information just how/where you are getting/feeding the data to the program).

    Without that information it may not be possible to answer your question - and I'm sure that our other Experts and Mods would like to lend a hand if possible.

    Please remember to use the [CODE/] button to format your posted script.

    We would also greatly appreciate it if your code was well commented/documented. There are a few things more tedious than trying to read thru a hundred lines of code that use non-descript variables and no commenting.

    Comment

    • Ronn
      New Member
      • Jan 2014
      • 3

      #3
      'Hi zmbd Thank you for the welcome and for trying to help me
      'I am not sure what (the target OS, the compiler version) means so i have sent-
      '-this tiny exerpt from my origanal program it will be sufficient to work with, I HOPE
      'I am using vb6 if this is what you mean by compiler version

      'you will need 1 form
      '1 label
      '2 image boxes with stretch set to true and any 2 images you choose on your pc-
      '- in my real program these are chosen by the user from a folder on the c drive-
      '- you will need to enter these in the properties box picture element for image1 and 2-
      '-to pick up the images you have chosen
      'the image boxes are about 6 inch(150mm) wide by about 8 inch(200mm) high-
      '-and my form is about 12 inches(300mm) wide by about 10 inches(250mm) high-
      '-but these are not critical sizes

      Code:
      Public zx
      Private Declare Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As Long)
      --------------------------------------------------------------
      
      Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
      On Error Resume Next
      Select Case KeyCode
      Case vbKeyEnd:
      
      zx = 3: 'when the end key is pressed zx value = 3 so the loop can exit when zx > 1
      Print "xx": 'this just to show when this routine has been implemented (not part of programe)
      End Select
      End Sub
      --------------------------------------------------------------
      
      Private Sub Label1_Click()
      'zx = zx + 1: ' when label1 is clicked to start scroll then value zx is increased to 1
      Cls
      Image2.Visible = False:
      st: ' start of image1 scroll loop
      
         Rem: image1 and image 2.top  are initially set at position 6000 and will scroll up to 100 aprox
      
      Image1.Top = Image1.Top - 16: '
      If zx > 1 Then GoTo fin1: 'if the end key has been pressed then zx should = 3 and prog should jump to fin
      
      Sleep (2): 'alter this value to slow down scroll
      
      If Image1.Top <= 100 Then GoTo fin1 Else GoTo st: ' checks position of image1 and if value more than 100 it goes-
         '- to st: to continue loop or goes to fin1:
      
         
       
      fin1: zx = 0: Image1.Visible = False: Image1.Top = 6000: ' resets zx and hides image1 and re-seta image1 top to 6000
      Image2.Visible = True
      st2: ' start of image2 scroll loop
      Image2.Top = Image2.Top - 16: '
      If zx > 1 Then GoTo fin2: 'if the end key has been pressed then zx should = 3 and prog should jump to fin2
      Sleep (2)
      If Image2.Top <= 100 Then GoTo fin2 Else GoTo st2:
      
      fin2:
      
      
      
      End Sub
      Last edited by zmbd; Jan 1 '14, 10:39 PM. Reason: [Z{Please use the [CODE/] button to format posted script and formated text - Please read the FAQ}]

      Comment

      • zmbd
        Recognized Expert Moderator Expert
        • Mar 2012
        • 5501

        #4
        OS:=operating system:= unix/windows/mac and version
        Looks like it's one of the Windows.

        Compiler:= what you are writing the script in and creating the executable with. I'll guess you are using one of the Microsoft Visual Studio tools

        I noticed a lot of "goto" and lable branches... this really isn't considered the new norm for programing.

        So, I'm not going to rewrite your code; however, here's what I would do:

        Write your code using a Do...Until loop
        Have a DoEvents command in the loop to yield to the system, this way, the code should pickup on the escapekey press(es)
        Set the KeyPreview property of your form to "true"
        Then say we want to use the [ESC] key (common key - no) to break the loop so we need code that runs something like:

        HEADS-UP: I don't have a VB environment to test this with so I have to leave this to you to troubleshoot.

        Code:
        Option Explicit
        Dim CancelLoop As Boolean
        
        Private Sub Command1_Click()
        '
           Dim failsafe as long
        '
           Screen.MousePointer = vbHourglass
           CancelLoop = False
           failsafe = 0
           Do     
              DoEvents     
              'do your stuff
        '
        'When I start testing loops I usually include a failsafe
        'to prevent an infinite loop condition. You just can't press
        'that break key fast enough!!!
              failsafe = failsafe + 1
              if failsafe > 775807 then exit do
           Loop Until CancelLoop
        '
           Screen.MousePointer = vbDefault
        EndSub
        
        Private Sub Form_KeyPress(KeyAscii As Integer)
           CancelLoop = KeyAscii = vbKeyEscape
        End Sub
        What I would do, is take the above framework, and insert your basic scrolling code... I was trying to locate something straight forward and simple... however, I'm not finding anything to my liking. I did run across some web references that Microsoft has an add-in for enabling the mouse scroll wheel... that might be a nice feature to add....

        Hope this gets you on the right track.
        Last edited by zmbd; Jan 6 '14, 01:57 PM.

        Comment

        • Ronn
          New Member
          • Jan 2014
          • 3

          #5
          Thanks zmbd for the code I put a print line in imediately after the key code and it recognised it instantly but did not exit the loop, however with the new loop you sent me it was possible to exit the loop with a mouse click on a command box making it un-neccisary to have the keypress code in at all, so job done and thanks again.

          Comment

          • zmbd
            Recognized Expert Moderator Expert
            • Mar 2012
            • 5501

            #6
            part of the key to this is having the
            Code:
            Dim CancelLoop As Boolean
            outside of the procedures for the form...

            and the more I think about that it might should be
            Code:
            Public CancelLoop As Boolean
            The other thing is that we might have to change the code in the keypress event procedure from:
            Code:
            CancelLoop = KeyAscii = vbKeyEscape
            to:
            Code:
            if KeyAscii = vbKeyEscape then
               CancelLoop = True
            else
               CancelLoop = False
            end if

            Comment

            Working...