Tag Archives: numbers only

Only allow Numbers in a TextBox Control in VB.NET

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
 

Code snippet to allow only numbers in a textbox.

Put the snippet below in the Textbox_Keypress Event.

        '
        'This will simply check the Keychar that was pressed and will only allow numbers
        'to be typed‘in the TextBox control.
        'Put in the ‘TextBox_KeyPress’ event.
        If Not Char.IsNumber(e.KeyChar) Then

            e.Handled = True

        End If

Allow only Numbers in a Textbox Control using VB.NET and Higher

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
 

This will only allow numbers to be entered into a textbox control. You can allow apply the codes to a combobox controls edit box. This works with VB.NET and VB 2005, VB 2008, and Visual Basic 2010.

        'Allow only numbers when someone types in a textbox control
        '
        'Add a textbox to the form and name is: txt . Then put the following 
        '
        'Code needs to be in the Textbox_KeyPress event of the textbox control.
        If e.KeyChar.IsNumber(e.KeyChar) = False Then

            e.Handled = True

        End If