?
Solved

Pausing a Program

Posted on 2003-02-20
12
Medium Priority
?
142 Views
Last Modified: 2010-05-01
I have a program with a "For" Loop and I want to pause the loop until "spacebar" is press. Can someone please help me? Code would be nice.
0
Comment
Question by:Linky
[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
  • 6
  • 2
  • 2
  • +2
12 Comments
 
LVL 17

Expert Comment

by:inthedark
ID: 7990567
Seth the form's keypreview propety to true, then use the folloing code in the form...

Private Sub Form_KeyPress(KeyAscii As Integer)
If KeyAscii = 32 Then
    MsgBox "Wpace bar pressed!"
End If
End Sub

Hop this helps:~)
0
 
LVL 2

Author Comment

by:Linky
ID: 7990597
Its hard to example. Here is what I am shooting for:

Event 1
Pause Until Space is press
Then go on to Event 2.
0
 
LVL 2

Accepted Solution

by:
Sweat earned 800 total points
ID: 7990719
Linky,

You can use the comment from inthedark, but where he has the MsgBox, set a boolean variable to True.  That same variable is being tested within your For/Next (a Do/Loop would be easier to demonstrate) to see if it's true.  When it is, you drop out of the loop.

   Do
      Doevents   ' So other things are processed
      ' Your pause code or other code goes here
      If bCancel = True Then
         bCancel = False ' Turn it off again
         Exit Do
      End If
   Loop

   Private Sub Form_KeyPress(Keyascii As Integer)
      If Keyascii = 32 Then
         bCancel = True
      End If
   End Sub

You would put the declaration in your Sub Main:
   Global bCancel As Boolean

Be sure to set the KeyPreview property on the form to True or the KeyPress event on the form will not fire.

If you use the For/Next structure, just issue Exit For to drop out of the For/Next structure.

There ya go.

Sweat


0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Author Comment

by:Linky
ID: 7991054
Are there any alternatives to Form_KeyPress?
0
 
LVL 2

Author Comment

by:Linky
ID: 7991078
Are there any alternatives to Form_KeyPress?
0
 
LVL 27

Expert Comment

by:Dabas
ID: 7991207
Sure! There also is Form_KeyDown and Form_KeyUp, but I assume you do not like them either?

Another alternative to achieve your objective is to use a timer object, but essentially you will still need to know if the space key has been pressed.

Why do you not like the solutions proposed above?

Dabas
0
 
LVL 2

Author Comment

by:Linky
ID: 7991263
Well I am trying to get it to work, it works in a stand alone program. But not mine.
0
 
LVL 2

Author Comment

by:Linky
ID: 7991488
I got it to work. Thanks.
0
 
LVL 17

Expert Comment

by:inthedark
ID: 7991904
One last thing, we forgot to say....if you use a keydown or keypress event set the value to zero so that a space won't appear somwhere else in the current field Keyascii =0.

Private Sub Form_KeyPress(Keyascii As Integer)
     If Keyascii = 32 Then
        Keyascii = 0 ' DO THIS
        bCancel = True
     End If
  End Sub
0
 
LVL 2

Author Comment

by:Linky
ID: 7993022
Ok, thanks.
0
 
LVL 5

Expert Comment

by:kcm76
ID: 7998399
hi,
This code is fine. But when u run this, my Taskmanager always shows CPU Usage as (100%). Wont this make harm to the computer.?

0
 
LVL 2

Expert Comment

by:Sweat
ID: 8000680
kcm76,

What is shown is as an example only.  You certainly wouldn't just have a Do/Loop doing nothing.  The presumption is that there would be programming code to do some processing and upon completion of one iteration of that process instead of dropping out of the loop, the process would return to the top and begin executing again.

The original question was how to be inside a looping structure and then break out of it when the program operator pressed the SpaceBar.

Hope this helps clarify your concern.

Sweat


0

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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 process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses
Course of the Month14 days, 16 hours left to enroll

770 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