How can I use numeric values only but also allow the Backspace?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Alkis Arxontas
    New Member
    • Mar 2011
    • 10

    How can I use numeric values only but also allow the Backspace?

    I am using Visual Basic Express 2008.
    I have added the following code to my text box:

    Private Sub TextBox1_KeyPre ss(ByVal sender As Object, ByVal e As System.Windows. Forms.KeyPressE ventArgs) Handles TextBox1.KeyPre ss
    If Not IsNumeric(e.Key Char) Then
    MessageBox.Show ("Please insert numbers only.", "Error!", MessageBoxButto ns.OK, MessageBoxIcon. Exclamation)
    e.KeyChar = Nothing
    End If
    End Sub

    The problem is that if I click Backspace it shows the same error and I can't change a type error. Is there a way to get it working?
  • VijaySofist
    New Member
    • Jun 2007
    • 107

    #2
    Hi,

    You can try the following Code

    Code:
    'Add This Function to your Code
    Public Shared Sub ValidateKeys(ByVal strContent As String, ByVal e As System.Windows.Forms.KeyPressEventArgs, ByVal blnAlpha As Boolean, ByVal blnNumeric As Boolean, ByVal blnSpecial As Boolean, Optional ByVal strAllowableChars As String = "", Optional ByVal strDisAllowableChars As String = "", Optional ByVal blnToUpperCase As Boolean = True)
            Dim KeyAscii As Integer
            Dim intCounter As Integer
            Dim EnteredKeyAscii As Integer
            Dim mValidCharacters As String = ""
            Try
                If e.KeyChar = "'" Then
                    e.KeyChar = CChar("".ToString)
                    Exit Sub
                End If
    
                KeyAscii = Asc(e.KeyChar)
                EnteredKeyAscii = Asc(e.KeyChar)
    
                If blnNumeric = True And blnAlpha = False And blnSpecial = True And strAllowableChars = "." And strDisAllowableChars = "" Then
                    If e.KeyChar = "." Then
                        If strContent.IndexOf(".") >= 0 Or strContent.Length.Equals(0) Then 'if period already exists or if the user has entered period as the first entry
                            e.KeyChar = CChar("".ToString)
                            Exit Sub
                        End If
                    End If
                End If
    
                If blnToUpperCase = True Then KeyAscii = KeyAscii - CInt(IIf(KeyAscii >= 97 And KeyAscii <= 122, 32, 0))
    
                If blnAlpha = True Then
                    For intCounter = Asc("A") To Asc("Z")
                        mValidCharacters = mValidCharacters & UCase(Chr(intCounter))
                    Next
                End If
    
                If blnNumeric = True Then
                    For intCounter = 0 To 9
                        mValidCharacters = mValidCharacters & intCounter
                    Next
                End If
    
                If blnSpecial = True Then
                    If strAllowableChars <> "" And strDisAllowableChars <> "" Then
                        mValidCharacters = mValidCharacters & strAllowableChars
                    ElseIf strAllowableChars <> "" And strDisAllowableChars = "" Then
                        mValidCharacters = mValidCharacters & strAllowableChars
                    ElseIf strAllowableChars = "" And strDisAllowableChars <> "" Then
                        For intCounter = 1 To 255
                            If Not InStr(1, strDisAllowableChars, Chr(intCounter), vbTextCompare) > 0 Then
                                mValidCharacters = mValidCharacters & Chr(intCounter)
                            End If
                        Next
                    ElseIf strAllowableChars = "" And strDisAllowableChars = "" Then
                        For intCounter = 1 To 255
                            mValidCharacters = mValidCharacters & Chr(intCounter)
                        Next
                    End If
                End If
    
                If Not (InStr(1, mValidCharacters, Chr(KeyAscii).ToString, CompareMethod.Text)) = 0 Or KeyAscii = 8 Then
                    e.KeyChar = Chr(KeyAscii)
                Else
                    e.KeyChar = CChar("".ToString)
                End If
            Catch excp As Exception
                MsgBox(excp.Message)
            End Try
        End Sub
    
    'How to use - Example
    ValidateKeys(txtEmpID.Text, e, False, True, False)
    Regards
    Vijay.R

    Comment

    • Alkis Arxontas
      New Member
      • Mar 2011
      • 10

      #3
      Wow :) Actually thanks a lot for the code!! But the only things I'd like to be available to insert should be 0123456789 Backspace Delete
      Could you help me on how to do that?

      Comment

      Working...