Many new programmer face a problem to checking whether textbox input is numeric or not. So you can easily implement this code on your windows forrm. Call this function on textbox1_Keypress event and it will prevent to enter non-numeric character in textbox.
<code>Public Function Check_number(ByVal ch As Char) As Boolean
On Error GoTo handle
If Asc(ch) = 8 Then
Check_number = False
Exit Function
End If
If IsNumeric(ch) = False Then
Check_number = True
Exit Function
End If
Check_number = False
Exit Function
handle:
LoadError("Check_number")
End Function</code>
Call this function as follows
<code>Private Sub txtbox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtbox1.KeyPress
e.Handled = Check_number(e.KeyChar)
End Sub</code>
<code>Public Function Check_number(ByVal ch As Char) As Boolean
On Error GoTo handle
If Asc(ch) = 8 Then
Check_number = False
Exit Function
End If
If IsNumeric(ch) = False Then
Check_number = True
Exit Function
End If
Check_number = False
Exit Function
handle:
LoadError("Check_number")
End Function</code>
Call this function as follows
<code>Private Sub txtbox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtbox1.KeyPress
e.Handled = Check_number(e.KeyChar)
End Sub</code>
Comments
Post a Comment