Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
 
TitleMake a numeric field using the KeyPress event handler
Keywordsnumeric field, KeyPress, locked TextBox
CategoriesControls
 
By Ferdie.

I have been using a function for some time now, that I think does the same trick, without so much code:

 
Public Function check_key_press(v1)
    ' check for valid characters
    ' from a "KeyPress" event. (chr(8)=backspace)
    ' where v1 is the keyascii from a keypress event
    If InStr(1, "1234567890-+." & Chr(8), Chr(v1)) Then
        check_key_press = v1
    Else
        check_key_press = 0
    End If
End Function
 
I would use the call as such:
 
Private Sub text1_keypress(KeyAscii As Integer)
    KeyAscii = check_key_press(KeyAscii)
End Sub
 
[Note that my book Custom Controls Library shows how to make field controls that do this sort of thing, though not this way.]
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated