It is a very simple coding for entering numeric value in a text box (VB.NET)
Step1 : Place a TextBox(TextBox1) on your form.
Step2 : Create a function (say TrapKey) for Checking the Values entered.
Step3: Call this function in the KeyPress event of TextBox1.
This has been explained below:
Code:
Private Function TrapKey(ByVal KCode As String) As Boolean
If (KCode >= 48 And KCode <= 57) Or KCode = 8 Then
'The AsCII values for numbers are between 48 to 57 and ASCII value 8 is for BackSpace.
TrapKey = False
Else
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
Bookmarks