Tag Archives: Keypress

Allowing/Restricting Certain Characters/Letters/Numbers in a Textbox

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (9 votes, average: 4.00 out of 5)
 

There are times where you may want to limit the characters that a user can put in a textbox control in Visual Basic.NET and Visual Basic 2005 or VB 2008. For example, if the textbox is used for math based calculations, you may only want the user to have the ability to put in numbers or only numbers with a single decimal point. Fortunately it is very easy to do.

In case you might care, some of these codes are taken from my csTextboxX .NET Control located at my www.vbcodesource.com website. All of these codes should be added to the Textbox_Keypress event and work with ALL versions of Visual Basic.NET (Including 2008, 2010, and higher).

Note: This article goes over how to Allow Only Numbers in a Textbox control with a Single Decimal Point (.).  I might add other articles in the future with code snippets on how to restrict other chars(characters) and user input. I do have code snippets on this site on restricting/allowing specific characters as well if you simply/only want the source code and nothing else.


Remember to put these codes in the Textbox controls _KeyPress event.

<

pre class=””>

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

'There are a couple scenerios this code is looking for. One is checking for the Decimal period "."
'and whether it exists in the textbox already. The other is seeing if the keypress was a Number and
'Control based key.
  If e.KeyChar = "." Then

'If a value higher than -1 is returned, it means there IS a existing decimal point’
    If TextBox1.Text.IndexOf(".") > -1 Then

'This says that I already dealt with the _KeyPress event so do not do anything else with this event.
        e.Handled = True

    End If
'
'Remember you want numbers to get through and the Control keys like “Backspace”
  ElseIf Char.IsNumber(e.KeyChar) = False AndAlso Char.IsControl(e.KeyChar) = False Then

'This says that I already dealt with the _KeyPress event so do not do anything else with this event.
    e.Handled = True

  End If

End Sub

Now if you run those codes, the textbox “should” only allow Number based characters with a Single decimal point char.

While the article was made to be very basic, hopefully it will give you a simple start on restricting textbox input to certain/specific characters. The basics from the article should help with that. Anyways, Have Fun!

Jason

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