Question how to avoid typing letters into textbox

ip2op01

Member
Joined
Jun 10, 2009
Messages
6
Programming Experience
Beginner
i want to create such a textbox in which only the numbers and one period should be allowed to type.

Letters and characters should be disabled.

please give the coding for Visual Basic 2005 only.

Thank you.
 
Not everyone likes the NumericUpDown, put this into your KeyPress event:
VB.NET:
[FONT="Courier New"]
Select Case Asc(e.KeyChar)
    Case 48 To 57 'the number keys
    Case 46 'the decimal key
         If txtNumberKeyPad.Text.Contains(".") Then
            e.Handled = True
         End If
    Case Keys.Back
    Case Else
         e.Handled = True 'this blocks all other key inputs
End Select[/FONT]
 
And if you want to add a zero if it is the first key then use:
VB.NET:
[FONT="Courier New"]Select Case Asc(e.KeyChar)
   Case 48 To 57 'the number keys
   Case 46 'the decimal key
        If Not txtNumberKeyPad.Text.Contains(".") Then
           If txtNumberKeyPad.Text.Length = 0 Then
              e.Handled = True
              txtNumberKeyPad.Text = "0."
              txtNumberKeyPad.SelectionStart = txtNumberKeyPad.Text.Length
           End If
        Else
           e.Handled = True
        End If
    Case Keys.Back
    Case Else
        e.Handled = True 'this blocks all other key inputs[/FONT]
End Select
 
And if you want to add a zero if it is the first key then use:
VB.NET:
[FONT="Courier New"]Select Case Asc(e.KeyChar)
   Case 48 To 57 'the number keys
   Case 46 'the decimal key
        If Not txtNumberKeyPad.Text.Contains(".") Then
           If txtNumberKeyPad.Text.Length = 0 Then
              e.Handled = True
              txtNumberKeyPad.Text = "0."
              txtNumberKeyPad.SelectionStart = txtNumberKeyPad.Text.Length
           End If
        Else
           e.Handled = True
        End If
    Case Keys.Back
    Case Else
        e.Handled = True 'this blocks all other key inputs[/FONT]
End Select

Thanks dude. :)
You are great.
you are genius.
i searched everywhere on the internet but i didn't found the solution for my problem. yes i found some code but it works only for vb 6.0, and also there are some usercontrols on internet which are heavy and not so good.
but your code works fine for my vb2005 project. you solved my problem. :)

I just replaced txtNumberKeyPad with TextBox1 and it worked.

:) Thanks again.
 
Last edited:
Best code for input of floating point numbers in a textbox:

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If Not Char.IsDigit(e.KeyChar) Then e.Handled = True 'allow only numerals
If e.KeyChar = Chr(8) Then e.Handled = False 'allow Backspace
If e.KeyChar = "-" And TextBox1.SelectionStart = 0 Then e.Handled = False 'allow negative numbers
If e.KeyChar = "." And TextBox1.Text.IndexOf(".") = -1 Then e.Handled = False 'allow only one decimal point
End Sub
 
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If Not Char.IsDigit(e.KeyChar) Then e.Handled = True 'allow only numerals
If e.KeyChar = Chr(8) Then e.Handled = False 'allow Backspace
If e.KeyChar = "-" And TextBox1.SelectionStart = 0 Then e.Handled = False 'allow negative numbers
If e.KeyChar = "." And TextBox1.Text.IndexOf(".") = -1 Then e.Handled = False 'allow only one decimal point
End Sub


Thanks Solitaire :)
 
Back
Top