Numbers Only Textbox

Dear Programmers

I want to make a routine named “NumberOnly”

Private Sub TextBox4_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox4.KeyPress

‘Here I want to call the function as 
End sub

All textboxes must accept numbers data on which I call this routine

Supose I call this routine on keypress event of textbox4 then textbox4 must accept only numbers only.

I got these codes from somewhere but it has paremeter error

Private Sub TextBox3_Change() 


End Sub 


Private Sub OnlyNumbers()

    If TypeName(Me.ActiveControl) = "TextBox" Then

        With Me.ActiveControl

            If Not IsNumeric(.Value) And .Value <> vbNullString Then

                MsgBox "Sorry, only numbers allowed"

                .Value = vbNullString

            End If

        End With

    End If


End Sub

Please help

Add KeyPress event for your textbox and use this code:

If Asc(e.KeyChar) <> 13 AndAlso Asc(e.KeyChar) <> 8 _
        AndAlso Not IsNumeric(e.KeyChar) Then
            e.Handled = True
        End If


Try this

Private Function TrapKey(ByVal KCode As String) As Boolean
   If (KCode >= 48 And KCode <= 57) Or KCode = 8 Then
       TrapKey = False
       TrapKey = True
    End If
End Function
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        e.Handled = TrapKey(Asc(e.KeyChar))
End Sub

No need to code to keep validating texbox values. Just use a NumericUpDown control. It is essientially a textbox control that only allows numbers. Also you then wont need to keep converting your textbox values back & forth to a numeric datatype.