473,406 Members | 2,954 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

exit a loop with key press

3
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.
Jan 1 '14 #1

✓ answered by zmbd

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.

Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2. Dim CancelLoop As Boolean
  3.  
  4. Private Sub Command1_Click()
  5. '
  6.    Dim failsafe as long
  7. '
  8.    Screen.MousePointer = vbHourglass
  9.    CancelLoop = False
  10.    failsafe = 0
  11.    Do     
  12.       DoEvents     
  13.       'do your stuff
  14. '
  15. 'When I start testing loops I usually include a failsafe
  16. 'to prevent an infinite loop condition. You just can't press
  17. 'that break key fast enough!!!
  18.       failsafe = failsafe + 1
  19.       if failsafe > 775807 then exit do
  20.    Loop Until CancelLoop
  21. '
  22.    Screen.MousePointer = vbDefault
  23. EndSub
  24.  
  25. Private Sub Form_KeyPress(KeyAscii As Integer)
  26.    CancelLoop = KeyAscii = vbKeyEscape
  27. 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.

5 6844
zmbd
5,501 Expert Mod 4TB
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.
Jan 1 '14 #2
Ronn
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

Expand|Select|Wrap|Line Numbers
  1. Public zx
  2. Private Declare Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As Long)
  3. --------------------------------------------------------------
  4.  
  5. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
  6. On Error Resume Next
  7. Select Case KeyCode
  8. Case vbKeyEnd:
  9.  
  10. zx = 3: 'when the end key is pressed zx value = 3 so the loop can exit when zx > 1
  11. Print "xx": 'this just to show when this routine has been implemented (not part of programe)
  12. End Select
  13. End Sub
  14. --------------------------------------------------------------
  15.  
  16. Private Sub Label1_Click()
  17. 'zx = zx + 1: ' when label1 is clicked to start scroll then value zx is increased to 1
  18. Cls
  19. Image2.Visible = False:
  20. st: ' start of image1 scroll loop
  21.  
  22.    Rem: image1 and image 2.top  are initially set at position 6000 and will scroll up to 100 aprox
  23.  
  24. Image1.Top = Image1.Top - 16: '
  25. If zx > 1 Then GoTo fin1: 'if the end key has been pressed then zx should = 3 and prog should jump to fin
  26.  
  27. Sleep (2): 'alter this value to slow down scroll
  28.  
  29. If Image1.Top <= 100 Then GoTo fin1 Else GoTo st: ' checks position of image1 and if value more than 100 it goes-
  30.    '- to st: to continue loop or goes to fin1:
  31.  
  32.  
  33.  
  34. fin1: zx = 0: Image1.Visible = False: Image1.Top = 6000: ' resets zx and hides image1 and re-seta image1 top to 6000
  35. Image2.Visible = True
  36. st2: ' start of image2 scroll loop
  37. Image2.Top = Image2.Top - 16: '
  38. If zx > 1 Then GoTo fin2: 'if the end key has been pressed then zx should = 3 and prog should jump to fin2
  39. Sleep (2)
  40. If Image2.Top <= 100 Then GoTo fin2 Else GoTo st2:
  41.  
  42. fin2:
  43.  
  44.  
  45.  
  46. End Sub
Jan 1 '14 #3
zmbd
5,501 Expert Mod 4TB
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.

Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2. Dim CancelLoop As Boolean
  3.  
  4. Private Sub Command1_Click()
  5. '
  6.    Dim failsafe as long
  7. '
  8.    Screen.MousePointer = vbHourglass
  9.    CancelLoop = False
  10.    failsafe = 0
  11.    Do     
  12.       DoEvents     
  13.       'do your stuff
  14. '
  15. 'When I start testing loops I usually include a failsafe
  16. 'to prevent an infinite loop condition. You just can't press
  17. 'that break key fast enough!!!
  18.       failsafe = failsafe + 1
  19.       if failsafe > 775807 then exit do
  20.    Loop Until CancelLoop
  21. '
  22.    Screen.MousePointer = vbDefault
  23. EndSub
  24.  
  25. Private Sub Form_KeyPress(KeyAscii As Integer)
  26.    CancelLoop = KeyAscii = vbKeyEscape
  27. 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.
Jan 1 '14 #4
Ronn
3
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.
Jan 6 '14 #5
zmbd
5,501 Expert Mod 4TB
part of the key to this is having the
Expand|Select|Wrap|Line Numbers
  1. Dim CancelLoop As Boolean 
outside of the procedures for the form...

and the more I think about that it might should be
Expand|Select|Wrap|Line Numbers
  1. Public CancelLoop As Boolean 
The other thing is that we might have to change the code in the keypress event procedure from:
Expand|Select|Wrap|Line Numbers
  1. CancelLoop = KeyAscii = vbKeyEscape
to:
Expand|Select|Wrap|Line Numbers
  1. if KeyAscii = vbKeyEscape then
  2.    CancelLoop = True
  3. else
  4.    CancelLoop = False
  5. end if
Jan 6 '14 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: drose0927 | last post by:
Please help! I can't get my program to exit if the user hits the Escape button: When I tried exit(EXIT_SUCCESS), it wouldn't compile and gave me this error: Parse Error, expecting `'}''...
8
by: Edwinah63 | last post by:
Hi Everyone, in vb6 i was able to execute the following code and it would close the children is the reverse order they were opened eg the last child opened was the first child to close. in...
4
by: Microsoft | last post by:
I have a for loops that goes from 1 to 256 I test the number and exception is thrown if there is an error. How do I get the program to stop in the middle of the loop and go to the next...
32
by: cj | last post by:
When I'm inside a do while loop sometimes it's necessary to jump out of the loop using exit do. I'm also used to being able to jump back and begin the loop again. Not sure which language my...
18
by: Vijaykumar Dave | last post by:
I have a program for base X power N as under. The problem is that when the range specified in loop is given it works well, but when any character is pressed, it goes to infinite loop. Second...
2
by: nabman | last post by:
I am reading a txt file, I have a txt file(customers.txt) that is read, with account numbers and passwords. Once the user enters a account number and password it should read the file and if there in...
11
by: Andreig | last post by:
Gentlemen I cannot figure out the following: I have a loop inPrivate Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint Pressing a key I am...
4
by: kenneth6 | last post by:
for(int i=0; i<100; i++) { if(mark==TMark) { return i; cout << i << endl; break; } else {
11
by: icarus | last post by:
Hi, this is a simple temperature converter (Fahrenheit to Celsius to Kelvin). Using gcc 4.0. The user is supposed to enter q or any other non-character to exit the program. Problem with this...
7
by: csharpula csharp | last post by:
Hello, I am iterating through objects and have the following case: foreach (classA objectA in classAList) { if (classA.count0) { //now I need to get out of the foreach loop if ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.