preventing users from entering characters in a textbox allowing only numbers

heres a class that only allows numeric input

VB.NET:
Public Class TextBoxEx
    Inherits System.Windows.Forms.TextBox

    Private _allowNegative As Boolean
    Private _enterTabbing As Boolean
    Private _previousText As String = ""

    Public Property allowNegative() As Boolean
        Get
            allowNegative = _allowNegative
        End Get
        Set(ByVal value As Boolean)
            _allowNegative = value
        End Set
    End Property

    Public Property enterTabbing() As Boolean
        Get
            enterTabbing = _enterTabbing
        End Get
        Set(ByVal value As Boolean)
            _enterTabbing = value
        End Set
    End Property

    Private ReadOnly Property previousText() As String
        Get
            previousText = _previousText
        End Get
    End Property

    Protected Overrides Sub OnKeyPress(ByVal e As System.Windows.Forms.KeyPressEventArgs)
        MyBase.OnKeyPress(e)

        If enterTabbing Then
            If Asc(e.KeyChar) = 13 And Me.SelectionStart = Me.Text.Length Or Asc(e.KeyChar) = 13 And Me.SelectionLength = Me.Text.Length Then
                Me.Parent.SelectNextControl(Me, True, True, False, True)
                Exit Sub
            End If
        End If

        If allowNegative Then
            If Not IsNumeric(e.KeyChar) Then
                If Not e.KeyChar = "." And Not e.KeyChar = "-" And Asc(e.KeyChar) <> 8 Or Me.Text.Contains(".") = True And Asc(e.KeyChar) <> 8 Or Not e.KeyChar = "." And Asc(e.KeyChar) <> 8 And Me.SelectionStart > 0 And e.KeyChar = "-" Then
                    e.KeyChar = Nothing
                End If
            End If
        Else
            If Not IsNumeric(e.KeyChar) Then
                If Not e.KeyChar = "." And Asc(e.KeyChar) <> 8 Or Me.Text.Contains(".") = True And Asc(e.KeyChar) <> 8 Then
                    e.KeyChar = Nothing
                End If
            End If
        End If

    End Sub

    Protected Overrides Sub OnTextChanged(ByVal e As System.EventArgs)
        MyBase.OnTextChanged(e)

        If allowNegative Then
            If Me.Text = "." Or Me.Text = "-" Or Me.Text = "-." Or Me.Text = "" Or IsNumeric(Me.Text) Then
                _previousText = Me.Text
            Else
                Me.Text = previousText
            End If
            Me.SelectionStart = Me.Text.Length
        Else
            If Me.Text = "." Or Me.Text = "" Or IsNumeric(Me.Text) Then
                _previousText = Me.Text
            Else
                Me.Text = previousText
            End If
            Me.SelectionStart = Me.Text.Length
        End If
    End Sub

End Class
 
never mind I got it
heres the code:

For only Numeric Input
If Char.IsNumber(e.KeyChar) = False Then
If e.KeyChar = CChar(ChrW(Keys.Back)) or e.KeyChar = CChar(ChrW(
Keys.Space)) Then
e.Handled = False
Else
e.Handled = True
End If
End If
It works great for me hope it works with everyone!

For only Alphabetical Input

If Char.IsLetter(e.KeyChar) = False Then
If e.KeyChar = CChar(ChrW(Keys.Back)) or e.KeyChar = CChar(ChrW(
Keys.Space)) Then
e.Handled = False
Else
e.Handled = True
End If
End If

For only Alphanumeric Input

If Char.IsLetterOrDigit(e.KeyChar) = False Then
If e.KeyChar = CChar(ChrW(Keys.Back)) or e.KeyChar = CChar(ChrW(
Keys.Space)) Then
e.Handled = False
Else
e.Handled = True
End If
End If

For only Decimal Input

If Char.IsNumber(e.KeyChar) = False Then
If e.KeyChar = CChar(ChrW(Keys.Back)) Or e.KeyChar =
CChar(".") or e.KeyChar = CChar(ChrW(Keys.Space)) Then
e.Handled = False
Else
e.Handled = True
End If
End If
 
Use a NumericUpDown control or a MaskedTextBox control.
 
a simple way to do it is

If IsNumeric(TextBox1.Text) Then
Else
TextBox1.ResetText()
End If

however if you need to allow decimal points then one of the above mentioned solutions is needed.
 
Personally, I use the NumericUpDown control for this instead of a TextBox, the NumericUpDown control allows you to specify a minimum number, maximum number, how many decimal places are allowed, etc...

Then you simply use the .Value() property to grab whatever the user typed in
 
Back
Top