Microsoft KB Archive/183473

{|
 * width="100%"|

FIX: KeyPress Event Code Does Not Reset Value of Keystroke

 * }

Q183473

-

The information in this article applies to:


 * Microsoft Windows CE Toolkit for Visual Basic 5.0, version 1.0

-

SYMPTOMS
In Visual Basic, the KeyPress event can be used to validate keystrokes and prevent invalid characters from being entered in controls. The KeyAscii value can be evaluated for valid characters and reset to 0 (zero) to remove invalid characters. This method does not work in the controls for the Windows CE Toolkit for Visual Basic 5.0 (VBCE). Modifying the KeyAscii value has no affect on the character entered.

CAUSE
The KeyAscii value used by the control is not affected by changes made to its value in KeyPress event procedure code.

RESOLUTION
The following are two ways to work around this problem:

  Use a LostFocus event procedure to validate the user-entered value and if necessary set the focus back to the control for correction. For example, the following code allows only digits in the text entered in textbox Text1:

     Private Sub Text1_LostFocus

Dim sText, i, iChar sText = Text1.Text

For i = 1 To Len(sText) iChar = Asc(Mid(sText, i, 1)) If ((iChar < 48) Or (iChar > 57)) Then MsgBox "Only digits allowed!" Text1.SetFocus Exit For End If        Next

End Sub  Use the KeyPress event and check the value of KeyAscii. Check for and ignore KeyAscii values less than 32 to accommodate control characters such as backspaces used to correct data. Display a message box when invalid characters are entered. This method will notify the user only when invalid data is entered; it will not prevent invalid data.

STATUS
Microsoft has confirmed this to be a problem in the Microsoft products that are listed at the beginning of this article.

This problem was corrected in Windows CE Toolkit for Visual Basic 6.00.