Results 1 to 10 of 10

Thread: ignore an invalid character in a text box [RESOLVED]

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2003
    Location
    Connecticut
    Posts
    257

    ignore an invalid character in a text box [RESOLVED]

    I am trying to prevent users from entering a character into a text box. I remember doing something like this in a beginning VB .net class a while ago and it was only one line of code in the textchanged (?) event of the textbox. From what I remember, it was a simple command to ignore the key that was pressed if it did not fit a certain criteria. Does anyone know a simple way to do this.
    Last edited by JMM427; Nov 24th, 2003 at 10:53 AM.

  2. #2
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    It is in KeyPress event, and is e.Handled=True
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2003
    Location
    Connecticut
    Posts
    257

    ignore character being entered [RESOLVED]

    Thank you!

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2003
    Location
    Connecticut
    Posts
    257
    I tried the following code and it's not working. Am I missing something, or misunderstanding what the handled event does?

    VB Code:
    1. Private Sub txtMileage_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtMileage.KeyPress
    2.  
    3.         If e.KeyChar.IsDigit("[0-9]") Then
    4.             e.Handled = True
    5.         Else
    6.             e.Handled = False
    7.  
    8.         End If
    9.  
    10.     End Sub

    What I'm trying to do is ignore the key being pressed if it is not a digit?

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2003
    Location
    Connecticut
    Posts
    257
    Sorry, I tried this also:

    VB Code:
    1. Private Sub txtMileage_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtMileage.KeyPress
    2.  
    3.         If e.KeyChar.IsDigit("[0-9]") = True Then
    4.             e.Handled = True
    5.         Else
    6.             e.Handled = False
    7.  
    8.         End If
    9.  
    10.     End Sub

    Still does the same thing.

  6. #6
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    VB Code:
    1. If Not (Char.IsDigit(e.KeyChar) Or Char.IsControl(e.KeyChar)) Then
    2.      e.Handled = True
    3. End If
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2003
    Location
    Connecticut
    Posts
    257
    Finally! Thank you.

    Now what if I also want it to accept a decimal separator "."?

  8. #8
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    Because the decimal point will happen only once then you have to do this:
    VB Code:
    1. If Not (Char.IsDigit(e.KeyChar) Or Char.IsControl(e.KeyChar)) Then
    2.      If Not (e.KeyChar = ".") Or (InStr(DirectCast(sender, TextBox).Text, ".", CompareMethod.Text)) Then
    3.           e.Handled = True
    4.      End If
    5. End If
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2003
    Location
    Connecticut
    Posts
    257
    Thanks, that makes sense. I appreciate all your help.

  10. #10
    Hyperactive Member sw_is_great's Avatar
    Join Date
    Nov 2003
    Posts
    330
    why donnt u use the method IsDigit
    Regards

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