Hi Friends

i am using vb.net & i have a textbox on that ,I want only numeric value on that textbox & if i type alfabetical on that textbox so that must give me a message to that type numeric value only

I m not able to doing this plz help me

Private Sub textBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles textBox.KeyPress
        
        If Char.IsNumber(e.KeyChar) Or Asc(e.KeyChar) = 8 Then
            e.Handled = False
        Else
            e.Handled = True
            MsgBox("Numeric Values Only", MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "Input Error")
        End If

End Sub

basicly the IsNumber is numbers 0-9, it also allows the delete button, and the ASCII key 8 is the back space key

This following code just allowed you to entered numbers only (No alphabetics or any special characters) :

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
    If (Microsoft.VisualBasic.Asc(e.KeyChar) < 48) _
              Or (Microsoft.VisualBasic.Asc(e.KeyChar) > 57) Then
            e.Handled = True
    End If
    If (Microsoft.VisualBasic.Asc(e.KeyChar) = 8) Then
            e.Handled = False
    End If
End Sub

well that is what you asked for

use google

If Char.IsNumber(e.KeyChar)Then

        Else
            MsgBox("Numeric Values Only", MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "Input Error")
        End If

try that, the e.Handled = False/True determins wether the charater is to be imputted - so just simply remove it

HI

You can control that text box by this way

Private Sub tbxNumeric_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles tbxNumeric.KeyPress
StringManipulation.SetTextboxAllowedCharacters("0123456789.", e)

End Sub

Sorry that function is like this

Shared Sub SetTextboxAllowedCharacters(ByVal CharactersAllowed As String, ByRef e As System.Windows.Forms.KeyPressEventArgs, Optional ByVal enter As Boolean = True, Optional ByVal BackSpace As Boolean = True)

Dim err As Integer = 0
        If InStr(CharactersAllowed, e.KeyChar.ToString.ToUpper) = 0 Then
            err = 1
            If enter And Asc(e.KeyChar) = Keys.Enter Then err = 0
            If BackSpace And Asc(e.KeyChar) = Keys.Back Then err = 0
            If Asc(e.KeyChar) = 3 Then err = 0
            If Asc(e.KeyChar) = 22 Then err = 0
        End If
        If err = 1 Then
            e.Handled = True
        End If
    End Sub

thanks

you can also use the required field validators in the toolbox

regardless this thread should be marked as solved

This article has been dead for over six months. Start a new discussion instead.