typing too fast for KeyUp event???

XIXl

New member
Joined
Oct 9, 2004
Messages
3
Programming Experience
10+
I have a textbox that I have written a KeyUp event for, and it looks at the contents of the text box for validation. The problem is that when I type the number too fast, the previous event evaluates to true and fires heavy database routines multiple times. I can't quite figure this out, any ideas or workarounds or just stuff that I am not doing? The TextBox is filling up before the last event fires somehow. It does not happen when the number is typed slowly. here is some code:

VB.NET:
Private Sub InputField_KeyUp(ByVal sender As Object, ByVal e As _ System.Windows.Forms.KeyEventArgs) Handles InputField.KeyUp
 
If ValidInputText(Me.InputField.Text) Then 
'see if the contents are in the right format
 
If Exists(Me.InputField.Text) Then
'checks correct format for existence in the SQLServer
 
GatherAll()
'create the worker threads to make objects from the SQLServer
End If
End If
End Sub
Muchos Gracias!
 
Last edited by a moderator:
Here is the complete listing for this function, thanks for taking a look at it:


VB.NET:
Private Function ValidInputText(ByVal strIn As String) As Boolean
Dim strTemp As String
strTemp = Trim(strIn)
If strTemp.Length() = 7 And (strTemp.LastIndexOf("-") = 1) Then 
'enable the Next Button
btnNext.Enabled = True
'set any state variables
t_InputType = InputType.WMTR
'select the appropriate radio button
rdoWMTR.Checked = True
Return True
ElseIf strTemp.Length() = 6 And (strTemp.LastIndexOf("-") = -1) Then 
'enable the Next Button
btnNext.Enabled = True
'set any state variables
t_InputType = InputType.TESTLOG
'select the appropriate radio button
rdoTestLog.Checked = True
Return True
Else
'if there is not presently any valid string formats in the text box 
' then disable the Next Button
btnNext.Enabled = False
t_InputType = Nothing
Return False
End If
End Function
 
Last edited by a moderator:
I have solved this by moving the routine into the TextChanged event. What was happening was I was typing and overlapping the keys. for example, the last 2 numbers being typed were 6 and then 1. I was hitting the 6 key and before the key was released, I was typing the 1 key. Therefore, when the 6 key fired its KeyUp, the textbox was already holding the finished (and valid) contents, so it falsely reported that it passed the validation, and queried the SQL Server. Then the KeyUp was fired for the 1 key, yadda yadda.
Hope this helps someone else.

Thanks all!
XIXl
 
yea, i jes read through it all fer ya and saw that event fireing sequence. the textchanged event is indeed the one ya want (or the keypress event wit an extra check)
 
I was just having a similiar problem and I found this thread. Here is the code I eventually came up with and it seems to work.

VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] KeyIsDown [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Boolean[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Private [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] TextBox_KeyDown([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2]System.Windows.Forms.KeyEventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] TextBox.KeyDown[/SIZE]
[SIZE=2]  <<Code to handle this event here>>[/SIZE]
[SIZE=2]  KeyIsDown = [/SIZE][SIZE=2][COLOR=#0000ff]True[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
 
[SIZE=2][COLOR=#0000ff]Private [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] TextBox_KeyUp([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Windows.Forms.KeyEventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] TextBox.KeyUp[/SIZE]
[SIZE=2][COLOR=#0000ff]  If[/COLOR][/SIZE][SIZE=2] KeyIsDown [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE]
[SIZE=2]          <<Code to handle this event here>>[/SIZE]
[SIZE=2]          KeyIsDown = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]  End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
 
Never use rapid/often firing events such as keypresses or mousemoves to fire off heavy, long running ops

Also be aware that keydown fires repeatedly when you hold a key, but keyup only fires once..
 
I was just having a similiar problem and I found this thread. Here is the code I eventually came up with and it seems to work.

VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] KeyIsDown [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Boolean[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Private [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] TextBox_KeyDown([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2]System.Windows.Forms.KeyEventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] TextBox.KeyDown[/SIZE]
[SIZE=2]  <<Code to handle this event here>>[/SIZE]
[SIZE=2]  KeyIsDown = [/SIZE][SIZE=2][COLOR=#0000ff]True[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
 
[SIZE=2][COLOR=#0000ff]Private [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] TextBox_KeyUp([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Windows.Forms.KeyEventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] TextBox.KeyUp[/SIZE]
[SIZE=2][COLOR=#0000ff]  If[/COLOR][/SIZE][SIZE=2] KeyIsDown [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE]
[SIZE=2]          <<Code to handle this event here>>[/SIZE]
[SIZE=2]          KeyIsDown = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]  End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]

What kind of operations are you using with this? this type of code could cause problems if a lot of stuff is being done with each keypress
 
@cjard: I don't know why, but my KeyUp event was sometimes firing twice if I held any key down a bit longer. (Maybe my code was somehow causing this.) I wanted to put code in the KeyUp event so that I could process the text in the textbox after the most recently typed character showed up in the textbox.

@JuggaloBrotha: No heavy operations, my problem was similiar in that the key was firing twice when I didn't want it to. I'm making a text-editor that makes use of shorthand notation. Typing an abbreviation will instantly replace the text with something else.
 
Back
Top