Pausing a Program

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.
LVL 2
LinkyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

inthedarkCommented:
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
LinkyAuthor Commented:
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
SweatCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

LinkyAuthor Commented:
Are there any alternatives to Form_KeyPress?
0
LinkyAuthor Commented:
Are there any alternatives to Form_KeyPress?
0
DabasCommented:
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
LinkyAuthor Commented:
Well I am trying to get it to work, it works in a stand alone program. But not mine.
0
LinkyAuthor Commented:
I got it to work. Thanks.
0
inthedarkCommented:
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
LinkyAuthor Commented:
Ok, thanks.
0
Chandramouli kArchitectCommented:
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
SweatCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.