Numbers Only Textbox

  • 14 years ago

    I am trying to write a piece of code in VB.NET 2003 that will only allow NUMBERS and PERIODS but what I am using currently doesn't work. I am trying to keep this as simple as I can to understand it. This is what I have so far:

    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress

    If e.KeyChar <= Microsoft.VisualBasic.ChrW(7) Then

    TextBox1.Text = ""

    ElseIf e.KeyChar >= Microsoft.VisualBasic.ChrW(9) Then

    TextBox1.Text = ""

    ElseIf e.KeyChar <= Microsoft.VisualBasic.ChrW(45) Then

    TextBox1.Text = ""

    ElseIf e.KeyChar = Microsoft.VisualBasic.ChrW(47) Then

    TextBox1.Text = ""

    ElseIf e.KeyChar >= Microsoft.VisualBasic.ChrW(58) Then

    TextBox1.Text = ""

    End If

    End Sub

    If you hit a wrong key, erase the textbox .... But this doesn't work. The key that is not allowed still gets printed in the textbox anyway.

    Before I wrote out this If statement, I previously wrote out a Select Case statement with the values I have posted above and a MsgBox would go off saying I hit the wrong key and did not report a MsgBox if I did hit numbers or the period, but after I clicked ok to the MsgBox the wrong letter that is supposed to be not allowed would appear in the textbox anyway.

    What can I do to make the keys I dont want in the textbox not get written to the textbox?

  • 14 years ago
    Hi ,
    I think the problem is we didnt tell the system that the keypress is handled that y the problem occur..
    for example when u press some character as per ur condition it will clear the text box and then the system process the key press and put it in the text box....ok use
    e.Handled = True

    in this code i think u miss logic...1st he enters correct character then he enters a worng character it will earse the entire textbox....anyway the previously entered value is correct right..make sure of it according to ur spec..

    -Amjath







  • 14 years ago

    Amjath got it on the money so I won't re-explain it.  Basically if you don't want the textbox to accept a keypress then you need to set e.handled = true.  So what you need to do is check if the key is something you don't want to except and if so ignore it.  This code should do what your looking for although you can tweak it if it's not exactly right.

        Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
            'If key isn't a number, control key (you want del/backspace I assume),
            'or a period then set e.handled to True (tells texbox not to worry about it)
            If Not (Char.IsNumber(e.KeyChar) _
            OrElse Char.IsControl(e.KeyChar) _
            OrElse e.KeyChar = "."c) Then e.Handled = True
        End Sub
  • 14 years ago

    Thank you both for helping me! I understand now how to control the keyboard :)

    One question to TwoFaced ......

    on one line you wrote "OrElse e.KeyChar = "."c) Then e.Handled = True"

    What does c do? or was that a typo?

  • 14 years ago

    Nope, it wasn't a typo.  e.KeyChar returns a type char.  The 'c' after the string means that the letter I typed is of type char not string.  If you have Option Strict On something like the following won't work.

            Dim letter As Char
            letter = "t" 'This won't compile because implicit conversions aren't allowed
            letter = "t"c 'This compiles fine because I used the char literal
    However, it turns out even with option strict on something like e.KeyChar = "." works just fine. So I guess I didn't need the char literal in this case, but that's the reason behind it.

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away.” - Antoine de Saint Exupéry