Results 1 to 4 of 4

Thread: trap non numeric entry

  1. #1

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

    trap non numeric entry

    I’m trying to trap non-numeric entry in a regular text box including decimal point trapping

    I put this code on the Keypress Event of the TextBox1 but it doesn’t work!

    e.KeyChar = IIf((e.KeyChar >= Chr(48) And e.KeyChar <= Chr(57)) Or e.KeyChar = Chr(8) Or e.KeyChar = Chr(13) Or (e.KeyChar = Chr(46) And InStr(TextBox1.Text, ".") = Chr(0)), e.KeyChar, Chr(0))

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Try using the different IsMethods of KeyChar to figure out what it is.

    If e.KeyChar.IsNumber then

  3. #3
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Dublin (Ireland)
    Posts
    304
    No better to add a handler.

    In fact I created my own custom controls, for numeric only, decimal only and date inheriting from the text box control and adding a handler to check characters enter and disregard any that didn't meet the criteria.

    Not near my system at the moment but if you want a code exampple I'll post it here.

  4. #4
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Edneeis's way is perfect but is this what you are looking for :
    VB Code:
    1. Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
    2.         Dim KeyAscii As Integer
    3.         KeyAscii = Asc(e.KeyChar)
    4.         'only allow numbers, a single decimal point, backspace  or enter        
    5.         Select Case KeyAscii
    6.             Case Asc("0") To Asc("9"), Asc(ControlChars.Back)
    7.                 'acceptable keystrokes
    8.                 e.Handled = False
    9.             Case Asc(".")
    10.                 e.Handled = False
    11.             Case Else
    12.                 e.Handled = True
    13.         End Select
    14.  
    15.     End Sub

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