Custom Search
Your Ad Here
Latest News

This following code just allowed you to entered strings / alphabetics only (no numbers or any special characters):




  1. Private Sub TextBox2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress
  2. If (Microsoft.VisualBasic.Asc(e.KeyChar) < 65) _
  3. Or (Microsoft.VisualBasic.Asc(e.KeyChar) > 90) _
  4. And (Microsoft.VisualBasic.Asc(e.KeyChar) < 97) _
  5. Or (Microsoft.VisualBasic.Asc(e.KeyChar) > 122) Then
  6. 'Allowed space
  7. If (Microsoft.VisualBasic.Asc(e.KeyChar) <> 32) Then
  8. e.Handled = True
  9. End If
  10. End If
  11. ' Allowed backspace
  12. If (Microsoft.VisualBasic.Asc(e.KeyChar) = 8) Then
  13. e.Handled = False
  14. End If
  15. End Sub

This following code just allowed you to entered numbers only (No alphabetics or any special characters




  1. Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
  2. If (Microsoft.VisualBasic.Asc(e.KeyChar) < 48) _
  3. Or (Microsoft.VisualBasic.Asc(e.KeyChar) > 57) Then
  4. e.Handled = True
  5. End If
  6. If (Microsoft.VisualBasic.Asc(e.KeyChar) = 8) Then
  7. e.Handled = False
  8. End If
  9. End Sub

To make text box to accept only numbers, in key press event of that textbox u can code like this




If Char.IsDigit(e.KeyChar) = False And Char.IsControl(e.KeyChar) = False Then
e.Handled = True
MsgBox("Please enter valid number ")
End If