Results 1 to 2 of 2

Thread: Keypress

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2002
    Location
    Pilipinas
    Posts
    441

    Keypress

    I have problem with this code;

    If (e.KeyChar < Chr(48) Or e.KeyChar > Chr(57)) And (e.KeyChar <> Chr(8)) Then
    e.Handle = True

    End If

    I need a code that trap non numeric key and allow backspace and period!

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    I always use this as needed .
    VB Code:
    1. Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
    2.         'Numeric keys start with 48 through 52
    3.         'backspace key has the ascii code 8
    4.         'period key has the ascii code 46
    5.  
    6.         Dim KeyAscii As Integer
    7.         KeyAscii = Asc(e.KeyChar)
    8.  
    9.         'only disallow numeric values
    10.         Select Case KeyAscii
    11.             Case Asc("0") To Asc("9")
    12.                 'acceptable keystrokes
    13.                 e.Handled = True
    14.                 '   Case Asc(".") if you want to disable
    15.                                   'other keys
    16.  
    17.  
    18.                     'e.Handled = True
    19.             Case Else
    20.                 e.Handled = False
    21.         End Select
    22.     End Sub
    have fun!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width