Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Quitting a loop using escape 1

Status
Not open for further replies.

etoucan

IS-IT--Management
Aug 23, 2004
16
0
0
GB
I have a loop going which adds records to a db table. I would like to be able to get out of the loop by hitting the ECS key. Can anybody tell me how to do this?
 

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Dim l_iLoop As Integer
m_bBreak = False
For l_iLoop = 0 To 1000000000
Application.DoEvents()
If m_bBreak Then
Exit For
End If
Next
End Sub


Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyUp
If e.KeyCode = Keys.Escape Then
m_bBreak = True
End If
End Sub


set up a modular boolean (eg m_bBreak) that gets set to true when esc key is pressed (keyup event below)

because it can be a real waste of processor speed to call do events for every loop it's best to add some code so that it will only fire every 100 times or 1000 times depending on what your loop is doing

eg...
if l_iLoop mod 1000=0 then

application.doevents
etc...

end if


Lastly, but most importantly don't forget to set the Form Keypreview property to true or all your work is for nought!

 
Great! That worked just fine. Thanks also for the tip about DoEvents.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top