how do I set a Textbox to only accept numerical values in vb .net

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sannihith
    New Member
    • Mar 2008
    • 1

    how do I set a Textbox to only accept numerical values in vb .net

    how do I set a Textbox to only accept numerical values in vb .net
  • enggwaqas
    New Member
    • Jun 2007
    • 19

    #2
    Originally posted by sannihith
    how do I set a Textbox to only accept numerical values in vb .net
    You can do this with keyUp event handler and SuppressKeyPres s property of KeyEventArg like this:

    Code:
    Private Sub TextBox1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
            If e.KeyValue < 48 Or e.KeyValue > 57 Then
                e.SuppressKeyPress = True
            End If
        End Sub

    Comment

    • Vishal Rana
      New Member
      • Mar 2008
      • 1

      #3
      If you are using .NET 2.0, try using the MaskedTextBox control.

      MaskedTextBox

      Comment

      • akshaythedeveloper
        New Member
        • Feb 2008
        • 1

        #4
        Hi there,

        If you wanna apply server side validation for not allowing anything else then numbers use the acii code present in second solution but it is advised to use client side validation ie in your vb.net toolbar, use regular expression validator..for that validator in the properties window enter a regular expression.

        Hope this help..

        Regards

        Comment

        Working...