Private Function TrapKey(ByVal KCode As String) As Boolean
If (KCode >= 48 And KCode <= 57) Or KCode = 8 Then
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
Alternate you can write common function like
Public Sub NumbericText(ByVal sender As System.Object, ByVal e As KeyPressEventArgs)
Dim ch As Integer = Asc(e.KeyChar)
If (ch >= 48 AndAlso ch <= 57) Or ch = 8 Then
e.Handled = False
Else
e.Handled = True
End If
End Sub
then add eventhandler for text box,
suppose ,in textbox txtId you just want to allow numeric value then you can do
AddHandler txtId.KeyPress, AddressOf NumbericText
No comments:
Post a Comment