key code

mirzao

Active member
Joined
Nov 10, 2005
Messages
44
Location
Malaysia
Programming Experience
Beginner
How to write a code so that when I pressed the start button it will continuously looping until I pressed a key to make it stop. i've been told that i can use keycode to interupt the loop but i don't know how to do it. the code i wrote is like this :

Do until keycode=13

For i = 3 To 4
Try

With oCP
.Port = i
.BaudRate = 9600
.Parity = Rs232.DataParity.Parity_None
.DataBit = 8
.StopBit = Rs232.DataStopBit.StopBit_1
End With
end try
loop
 
Last edited:
interrupt my For Loop using a KeyPress() event

http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_20418232.html

Interrupting a for loop using Keypress:

1) Set the form's Keypreview property to True
2) declare a module level variable (let's call it m_bKeyPressed)
3) declare another module_level variable (let's call it m_bCheckForKeyPress)
4) Before you start your loop, set m_bCheckForKeyPress = True and at the end of the loop, set it to false
5) In the Form_Keydown add the following code:
if m_bCheckForKeyPress = true then
m_bKeyPressed = True
Keycode = 0
else
m_bKeyPressed = false
endif
6) During your for...loop check for m_bKeyPressed = True condition
for x = 1 to 1000
if m_bKeyPressed = True then
m_bKeyPressed = False
exit for
endif
... Your code

next x



Not really usable code, huh? It can get you started. In the form Keypress you can certainly check for a specific key to stop the loop, not just any key like in the example.

Code that checks for <enter> as example:

VB.NET:
     Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress

        If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Enter) Then

        Else

        End If

    End Sub

Hope this helps get you headed in the right direction..
 
Don't forget that you need to call Application.DoEvents in the loop or else the app will be so busy processing the loop that the code in the event handler will never be executed. Also note that performance will degrade if you call DoEvents every iteration of a short loop.
 
Thanks for the replies. I've tried to include all the necessary code but it didn't stop when I pressed ENTER. The following are my full code :

Private Sub Form1_Load1(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Show()
doloop()
End Sub
Private Sub doloop()

Do
m_bCheckForKeyPress = True
For i = 2 To 3

If m_bKeyPressed = True Then
m_bKeyPressed = False

Exit For
End If
'---------------------------------------------
Application.DoEvents()

Try
'startTime = DateTime.Now.Ticks
With oCP
.Port = i
.BaudRate = 9600
.Parity = Rs232.DataParity.Parity_None
.DataBit = 8
.StopBit = Rs232.DataStopBit.StopBit_1
End With

oCP.Open()

oCP.Write(Encoding.ASCII.GetBytes("strs" & Chr(13)))

' As long as there is information, read one byte at a time and output it.
Try
While (oCP.Read(1) <> -1) 'get byte frm port

WriteMessage(oCP.InputStreamString, False)

End While
Catch exc As Exception
End Try

Dim myStat() As String
myStat = TextBox1.Lines(TextBox1.Lines.Length - 3).Split(" "c)

For o = LBound(myStat) To UBound(myStat) - 1
ListBox2.Items.Add(myStat(o))


Select Case myStat(o)
Case "607", "401", "402"
count += 1
idletime = count
Case "70E", "405", "40A"
count2 += 1
tempsetup = count2
Case "716", "507"
count3 += 1
lotsetuploss = count3
Case "301", "304", "306", "700", "701", "702", "703", "704", "70B", "70D", "710"
count4 += 1
unscheduledDTime = count4
Case "30A", "715"
count5 += 1
speedloss = count5
Case Else
'ignore
End Select

Next

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
oCP.Write(Encoding.ASCII.GetBytes("temp" & Chr(13)))
Try
While (oCP.Read(1) <> -1) 'get byte frm port

WriteMessage2(oCP.InputStreamString, False)

End While
Catch exc As Exception
End Try

actualTemp = TextBox3.Lines(TextBox3.Lines.Length - 8).Split("="c, "€"c) 'split each code in line m
setTemp = TextBox3.Lines(TextBox3.Lines.Length - 7).Split("="c, "€"c)

If actualTemp(1) = 25.0 Then
'if temperature out of band
If actualTemp(1) < (setTemp(1) - 5) Or actualTemp(1) > (setTemp(1) + 5) Then
countTemp += 1

End If
Else
'if temperature out of band
If actualTemp(1) < (setTemp(1) - 3) Or actualTemp(1) > (setTemp(1) + 3) Then
countTemp += 1
End If

End If
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Connection Error")

End Try
'----------------------------
If oCP.IsOpen Then oCP.Close()
Next

m_bCheckForKeyPress = False
Loop

''''''''''''''''''''''''''''''''''''

workbook.Save("c:\test.xls")
Process.Start("c:\test.xls")
End Sub
Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress

If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Enter) Then

Else

End If

End Sub

Private Sub Form1_KeyDown(ByVal KeyCode As Integer, ByVal Shift As Integer)
If m_bCheckForKeyPress = True Then
m_bKeyPressed = True
KeyCode = 0
Else
m_bKeyPressed = False
End If

End Sub


Private Sub Form1_Unload(ByVal Cancel As Integer)

'Shut down loop and unload

Me.Close()

End Sub
 
Last edited:
Back
Top