Visual Basic/VB >> Exiting from a loop with a keystroke

by giannis » Thu, 09 Oct 2003 19:46:17 GMT

I have a code as:

Do Until .....
.........
.........
Loop

I need when i press a key
exit from this loop.
How I can this ?
I cant use the key events for the Form
because this code is written at the
Form_Activate and Form cant receive
any keystroke............




Visual Basic/VB >> Exiting from a loop with a keystroke

by Al Reid » Thu, 09 Oct 2003 20:05:55 GMT


giannis,

Works for me!!! Perhaps you should post your code.

[ Has the Signal to Noise Ratio degraded or
is there a global oxygen deficit? ]






Visual Basic/VB >> Exiting from a loop with a keystroke

by Jon Mundsack » Thu, 09 Oct 2003 20:25:34 GMT





Put DoEvents inside the loop and you'll be OK:

Do Until .....
.........
.........
DoEvents
Loop

HTH-Jon





Exiting from a loop with a keystroke

by giannis » Thu, 09 Oct 2003 21:36:03 GMT

When i put the DOEVENTS the Key Events or the Form work ?








Exiting from a loop with a keystroke

by giannis » Thu, 09 Oct 2003 21:37:34 GMT

My code is a simple Do-Loop.
I want when i press a key (:esc)
exit from this loop.
How can do this ?








Exiting from a loop with a keystroke

by Veign » Thu, 09 Oct 2003 22:03:03 GMT

DoEvents just allow the OS to handle other events (i.e. a keystroke being
pressed). What you have to do is inside the loop periodically check for
your key being pressed and then Exit for that....

(side note: DoEvents also returns the number of loaded forms (try it
Debug.Print DoEvents)

--
Chris Hanscom
MVP (Visual Basic)
http://www.veign.com
Application Design Section
http://www.veign.com/information/application/info_app.html
------










Exiting from a loop with a keystroke

by Al Reid » Thu, 09 Oct 2003 22:43:40 GMT

giannis,

jon already told you to put a DoEvents inside of your Loop:


This will allow the form to process keystrokes. Now all you need to do is detect a keystroke in, maybe Form_KeyPress, set a module
level flag, then use the flag to terminate your loop.

I could write this for you in 30 seconds, but I don't think it will help you in the long run.

[ Has the Signal to Noise Ratio degraded or
is there a global oxygen deficit? ]








Exiting from a loop with a keystroke

by giannis » Thu, 09 Oct 2003 23:51:00 GMT

1)HOW CAN SET A MODULE FLAG ?
How can i know that this key in the key event
of Form is for the do-loop? What flag must
i insert in the Key Event procedure ?
Please say me more thoroughly.

2)There is not a function or statement that can
insert at the Do-Loop and see if a keystroke
is pressed ? (Clipper had a sutch function)



detect a keystroke in, maybe Form_KeyPress, set a module
you in the long run.








Exiting from a loop with a keystroke

by Veign » Fri, 10 Oct 2003 00:07:03 GMT

Module flag is just a Variable that has Module scope.

i.e. Dimensioned in the declaration section of the Form as Private

Sample

Option Explicit
Private lngKeyCode As Long

Private Sub Command1_Click()

Call MyLoop

End Sub

Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)

lngKeyCode = KeyCode

End Sub

Private Sub MyLoop()

'Reset the Flag
lngKeyCode = 0

Dim i As Long
Do Until i = 1000000
DoEvents
If lngKeyCode = vbKeyA Then Exit Do

'Do Something
i = i + 1
Loop

End Sub


--
Chris Hanscom
MVP (Visual Basic)
http://www.veign.com
Application Design Section
http://www.veign.com/information/application/info_app.html
------




is








Exiting from a loop with a keystroke

by Al Reid » Fri, 10 Oct 2003 00:09:04 GMT

giannis,

You really need to find and read a good book or tutorial on VB.



Do you have any idea about the scope of variables declared in a vb project?


Declare a module level Boolean variable in your form. It will be False by default. Use this variable to terminate your loop:

Do Until blnFlag
...
...
DoEvents
Loop

In one of the Form "Key" Events, trap any key or the key of your choice and set the Boolean Flag (blnFlag) to True.

That is all there is to it.


If there is such a native vb function, I am not aware of it.

[ Has the Signal to Noise Ratio degraded or
is there a global oxygen deficit? ]




Exiting from a loop with a keystroke

by Al Reid » Fri, 10 Oct 2003 00:22:50 GMT

Give a man a fish; you have fed him for today. Teach a man to fish; and you have fed him for a lifetime"

[ Has the Signal to Noise Ratio degraded or
is there a global oxygen deficit? ]
"Veign" < XXXX@XXXXX.COM > wrote in message news:Oe7gt# XXXX@XXXXX.COM ...




Exiting from a loop with a keystroke

by Rick Rothstein » Fri, 10 Oct 2003 00:25:38 GMT

> 2)There is not a function or statement that can

VB is an **event driven** language... I believe Clipper is, like the BASIC's
of old, a **procedural** language. You won't get very far in VB is you try
to apply procedural language concepts to this event driven environment. I
would suggest you take Al's advice when he said

"You really need to find and read a
good book or tutorial on VB."

You are really doing yourself a disservice if you continue to try and learn
VB line-by-line (that is what you appear to be doing if I read all the
questions you have posted to date correctly). I know you are using an
illegal copy of VB to do your programming -- you said so in a previous post
(you haven't bought a legitimate copy yet like you said you were going to,
right?), but you should definitely consider spending the money to buy a good
beginner's book and learn what VB is all about. In most cases, you are
asking simple question... getting simple answers... and being confused by
what to do with them. Stepping back now, reading up on what VB is, how it
works and how to work with it should reduce the need for you to ask so many
questions and should allow you to better understand the answer you will
receive to the question you do ask in the future.

Rick - MVP




Exiting from a loop with a keystroke

by Al Reid » Fri, 10 Oct 2003 00:33:32 GMT

Rick,

I couldn't have said it better myself ;^)

[ Has the Signal to Noise Ratio degraded or
is there a global oxygen deficit? ]





Exiting from a loop with a keystroke

by Ken Halter » Fri, 10 Oct 2003 00:42:36 GMT

Old fisherman never die... they just smell like they're dead.

--
Ken Halter - MS-MVP-VB - http://www.vbsight.com
Please keep it in the groups..




him for a lifetime"




Exiting from a loop with a keystroke

by Al Reid » Fri, 10 Oct 2003 00:43:12 GMT

Addressing just a small segment of your question:

<snip>
On another thread currently in progress here I read that "Form_Activate" cannot handle key input unless DoEvents is called.
</snip>

Don't believe everything you read here. The poster that made that statement is very inexperienced and knows not what he says. If
you put your code in a tight loop ANYWHERE events will be missed and the judicious use of DoEvents becomes necessary.


[ Has the Signal to Noise Ratio degraded or
is there a global oxygen deficit? ]





Similar Threads

1. Exiting nested For loops - VB.Net

2. Exiting a nested loop

Hi all,

It may be that I am getting this wrong or doing it the wrong way but 
this has been stuck.

I have a loop inside a loop, based on a certain condition i want to 
cause the primary loop "to loop".  If that makes sense!  see the pseudo 
code below:


For Each MyFile in Files

	For Each FileExtension In ArrayList
		If Path.GetExtension(FileExtension)="doc" Then
		Exit This For Eacg
		Move To Next MyFile
	Next

Next


So, basically, if the file extension is equal to one of the values in my 
list then I just want to ignore and grab the next file to process.

-- 

Daniel
MCSE, MCP+I, MCP in Windows 2000/NT

--------------------------------------
remove the 2nd madrid from my mail address to contact me.

3. exiting a for loop. - VB.Net

4. Sub Main and how not to exit, need to loop or else

"NNM.RU" < XXXX@XXXXX.COM > wrote in
news:7BLNa.494$ XXXX@XXXXX.COM : 

> I got something:
> ------------------------------------
> Sub Main
>      Add event
>      Add event
>      ??? - no idea what to put here so it would not reach End Sub.
> end sub
> 
> Event subs go here
> -----------------------------------------
> 
> 

At the end of your sub main put Application.Run().  Somewhere else you will 
have to do Application.Exit to terminate the program.

Chris

-- 
If you don't like lunchmeat, please remove it from my e-mail address to 
send me an e-mail

5. VBScript Exit Loops

6. exit loop after timeout (5 min)

hi,

I use the execnotificationquery to check the state of a service.  what my 
code does is stop a service and then I use the check notification query to 
see if the service is actually stopped by examining state attribute.  Once it 
is changed to "stopped" I simply start the service and continue with code.  
In essence it is doing a restart.  The code works perfectly.

.
.
.
set colServices = objWMIService.ExecNotificationQuery("Select * from 
__instancemodificationevent within 5 where TargetInstance isa 
'Win32_Service'")

returncode = objService.StopService()
so until servicestate = "Stopped"
    set objService = colServices.NextEvent
    if objservice.targetinstance.state = "Stopped" then servicestate = 
"Stopped"
loop
.
.
.


what I want to do is if after 5 minutes the state of service hasn't changed 
to "stopped" (for instance it is hung on stopping) I want to terminate my 
loop and produce an error to log file.  Unfortunately,  the way 
ExecNotificationQuery works it only reports events that have changed and if 
service state doesn't change I am stuck in an infinite loop because it simply 
sits at the nextevent method.  Loop never continues.

Any help on exiting loop after 5 minute timeout would be greatly appreciated.

thanks.

7. Need to exit loop early

8. Exiting from a Do Loop

I have the following code snippet of a Gosub routine which has several
individual loops but I am only showing 2 of them for the example.:

TEST:

Do
.....
.....
.....
if answer=true then
    return
endif
......
......
Loop until count=5


Do
.....
.....
.....
if answer=true then
    return
endif
......
......
Loop until count=5

Return

My question is can I leave the loop using the return command without
worrying about any problems since I have not exited the loop using the EXIT
DO command.  Somewhere in the past I thought I remebered that leaving a loop
like this can casue a buffer problem since I have not told it to exit the
loop.  Using the return command will that exit the loop properly and act as
if I did exit the do loop?

Thanks,

Les