TextBox Input Validation

Greetings:

I'm trying to get a textbox to only accept the input of numbers, ".", and the back key. I modeled this on the example in the MSDN help files, which is close to what I want and works just fine, but this tweak doesn't work. No input is accepted. The debugger will display that e.KeyCode is "NumPad5{101}", for example, but the debugger slides right over the first Case where I'd expect it to be accepted. Any other key gets the same result. What's missing here

Private Sub txtExp_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) _

Handles txtExp.KeyDown

Select Case e.KeyCode

Case e.KeyCode >= Keys.NumPad0 And e.KeyCode <= Keys.NumPad9

blnValidCurrency = True

Case e.KeyCode >= Keys.D0 And e.KeyCode <= Keys.D9

blnValidCurrency = True

Case e.KeyCode <> Keys.Back And e.KeyCode <> Keys.OemPeriod

blnValidCurrency = False

End Select

End Sub

Thanks!



Answer this question

TextBox Input Validation

  • Bobby DeRosa

    No, a message isn't necessary. They usually get the idea when a keypress does nothing (that's how it appears to the user - as if pressing the key had no effect).



  • maran.g

    You're on the right track, mate. But yeah, some big issues in your code. Try something like this:

    Firstly, throw this function into your project:

    Private Function ScanText(ByVal _
    MyText
    As String) As String
    Dim X As
    Integer
    Dim IsValid As
    Boolean
    Dim NewString As String = ""
    For X = 1 To Len(MyText)
    IsValid =
    False
    Select Case Asc(Mid(MyText, X, 1))
    Case Is >= 48
    If Asc(Mid(MyText, X, 1)) _
    <= 57 Then IsValid =
    True
    Case 46
    IsValid =
    True
    End
    Select
    If IsValid Then NewString _
    &= Mid(MyText, X, 1)
    Next
    Return NewString
    End Function

    Then, throw this code into the TextChanged() event of your textbox (changing the code as neccessary)

    Private
    Sub TextBox1_TextChanged _
    (ByVal sender As Object, ByVal _
    e As System.EventArgs) Handles _
    TextBox1.TextChanged
    TextBox1.Text = _
    ScanText(TextBox1.Text)
    TextBox1.SelectionStart = _
    Len(TextBox1.Text)
    End Sub

    I hope this helps you out!



  • CumQuaT

    An expression like "e.KeyCode >= Keys.NumPad0 And e.KeyCode <= Keys.NumPad9" evaluates to a boolean result, it is True or False. The Select statement now compares an integer with a boolean. It's rather sad that VB.NET allows this without Option Strict On.

    You can use a Select statement if you write the case statement like this:
    Select Case e.KeyCode
    Case Keys.NumPad0 To Keys.NumPad9
    ...



  • CaoxiCao

    Yes, thank you, I like it. In practice, do you throw in a message box to tell the user that you've messed with what they thought they were inputting
  • mrmckeb

    You can actually test the text character by character and remove any that don't belong:

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

    Dim sb As New System.Text.StringBuilder

    Dim cur As Integer = TextBox1.SelectionStart

    For Each c As Char In TextBox1.Text

    If Char.IsNumber(c) Then

    sb.Append(c)

    End If

    If c = "." Then

    sb.Append(c)

    End If

    Next

    TextBox1.Text = sb.ToString

    TextBox1.SelectionStart = cur

    End Sub

    Hope that helps.



  • Airseir

    Thanks nobugz. I think I understand it now. I certainly hadn't thought to look at it that way.
  • M Anwar

    This code is seriously flawed. To see the problems for yourself, put "Option Strict On" at the top of the source file.



  • JohnHadj

    Thanks nobugz and CumQuaT for your replies! You bet it helps. I appreciate it. I guess I should have said that I was trying to restrict input to the textbox to numbers, dot, and back key, not that I was validating all of the entered text at once. I was planning to add a validation function on the Leave event of the textbox.

    I'm still not getting why my original code is so flawed.

    You're right, option strict didn't like this bit of code, having issues with implicit conversions between type "boolean" and type "keys". Can you help me understand this As I understand it, e.KeyCode isn't boolean, and neither is Keys. Where is the conversion that is problematic

    Here's a snippet of code from the Help files, which work just fine, and Option Strict doesn't have any trouble with: "If e.KeyCode < Keys.D0 OrElse e.KeyCode > Keys.D9 Then" I started with that, and changed it to a Select Case structure to help me undertand it more clearly. I don't get how my code has an implicit conversion and this code doesn't.

    Making the code comply with Option Strict did not fix the problem. The debugger still slides right past the Case where I'd expect it to make a match.

    I appreciate your patience with my questions. I'm pretty new to this and would like to understand better.


  • TextBox Input Validation