Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 143
  • Last Modified:

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.
0
Linky
Asked:
Linky
  • 6
  • 2
  • 2
  • +2
1 Solution
 
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
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!

 
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

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 6
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now