Blog

Numeric Text box

By Ankur Gupta Jun 02 2010
How can we put only numbers in Text box control?
    • Like
    • Love It
    • Awesome
    • Interesting
    • It's Okay
    • Thumbs Down
  • 5.5k
  • 0

 

Put the following code in Textbox Ker Press event.

This code will allow only Numbers in the Textbox

Dim isBackSpace As Boolean = False

        Dim isKey As Boolean = [Char].IsDigit(e.KeyChar)

        If Asc(e.KeyChar) = 8 Then

            isBackSpace = True

        End If

        If Not isKey AndAlso Not isBackSpace Then

            e.Handled = True

        End If

If you want to put decimal also in the text box the use the following code.

Dim txtbox As TextBox

            txtbox = sender

            Dim isBackSpace As Boolean = False

            Dim isKey As Boolean = [Char].IsDigit(e.KeyChar)

            If Asc(e.KeyChar) = 8 Then

                isBackSpace = True

            End If

            If Not isKey AndAlso Not isBackSpace AndAlso txtbox.Text.Contains(".") Then

                e.Handled = True

            End If