hie
how can i restrict the user to enter only numbers in a textbox

If you are looking for an easier way(hack), try my code.

Dim cache As String = ""
    Private Sub Textbox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Textbox1.TextChanged
        If IsNumeric(Textbox1.Text) = True Then
            cache = Int(Textbox1.Text)
        Else
            Textbox1.text = cache
        End If
    End Sub

This code is a bad practice(hack), so you shouldn't really do this unless you are having a hard time on the true solution

Handle KeyPress Event of TextBox control.

Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        If Not Char.IsDigit(e.KeyChar) Then
            e.Handled = True
        End If
 End Sub

It depends on the type of numbers you are inputting. If whole numbers, I would suggest using the numericupdown control such as suggested above. Other options such as suggested above will be triggered with each key stroke, just of the textbox's validating event might be better since it would not trigger as much and wait for the full input before checking. Also keep in mind if your values contain a decimal point, neither the isnumeric of char.isdigit methods will work.

I think this code will allow to enter numbers and decimal points But it will allow ' , " ; etc
i am still looking for another shortcut way

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        If (Char.IsLetter(e.KeyChar) = True) Then
            e.Handled = True

        End If
    End Sub

Char.IsDigit will return false for decimal points. Especially with using the keypress event where it is not comparing the whole input only the individual character. Example. (Textbox1.text = 1.3) would return 3 results; true-false-true. However this may not be a concern since the original poster hasnt mentioned if it will be whole numbers or not.

I know this way which let you choose the char you want to allow to be written in the textbox

'Textbox KeyPress
' Allow number 0-9 plus backspace, Del + Home + End will be accepted also
Dim ValidInputChar = "0123456789." + vbBack
If not ValidInputChar.Contains(e.KeyChar) then
    e.KeyChar=Nothing           
End If

'Textbox_Lostfocus
Dim TextboxToNumeric as Double = Val(TextBox4.Text)
TextBox4.Text = TextboxToNumeric.ToString("#,###.00")

Yes that's a good idea ,
If it works i can restrict some special numbers and characters ...
Yes Thanks mannn .
Nice Job..

you just to handle keychar, but you must know the ascii of letters. this following code will not allowed you to input other character except letters or strings. so u cannot input number or other special character.
try this following code :

Private Sub textDate_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles textDate.KeyPress
        If (Microsoft.VisualBasic.Asc(e.KeyChar) < 65) _
             Or (Microsoft.VisualBasic.Asc(e.KeyChar) > 90) _
             And (Microsoft.VisualBasic.Asc(e.KeyChar) < 97) _
             Or (Microsoft.VisualBasic.Asc(e.KeyChar) > 122) Then
            'space accepted
            If (Microsoft.VisualBasic.Asc(e.KeyChar) <> 32) Then
                e.Handled = True
            End If
        End If
        If (Microsoft.VisualBasic.Asc(e.KeyChar) = 8) Then
            
            e.Handled = False
        End If
    End Sub

or more simply use a masked text box from the toolbox rather then a regular textbox & set it to accept only numbers.

regards.

thank you..................

Select Your textbox and then goto properties then select Event then in Event double click on keypress and add the follwing code

If Not Char.IsDigit
(e.KeyChar) Then
e.Handled
= True
End If

now run you program

There's a piece of code that disables all texts including special characters, it allows number only. I've been using it.

commented: Contributes nothing +0

There's a piece of code that disables all texts including special characters, it allows number only. I've been using it.

That's of absolutely no help unless you are willing to post the code or a link to it. Also, to everyone who has posted here in the last few days - this thread is three years old. Please do not revive old threads.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.