Textbox numerico

Imagen de lsubiabre

Codigo que muestra como crear un textbox que permita el ingreso de números solamente.

  1. Function SoloNumeros(ByVal Keyascii As Short) As Short
  2. If InStr("1234567890", Chr(Keyascii)) = 0 Then
  3. SoloNumeros = 0
  4. Else
  5. SoloNumeros = Keyascii
  6. End If
  7. Select Case Keyascii
  8. Case 8
  9. SoloNumeros = Keyascii
  10. Case 13
  11. SoloNumeros = Keyascii
  12. End Select
  13. End Function
  14. ------------------------------------------------------------------------------------------------
  15. Ahora valla al Formulario y haga doble click sobre el:
  16. Después de la línea:
  17. Inherits System.Windows.Forms.Form
  18. Copie y pegue esta Declaración de Variable
  19. Public KeyAscii As Short
  20. Ahora en el evento KeyPress del textBox1 copie y pegue este código
  21. Dim KeyAscii As Short = CShort(Asc(e.KeyChar))
  22. keyascii = CShort(SoloNumeros(keyascii))
  23. If keyascii = 0 Then
  24. e.Handled = True
  25. End If
  26. Quedaría de la siguiente forma:
  27. Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
  28. Dim KeyAscii As Short = CShort(Asc(e.KeyChar))
  29. keyascii = CShort(SoloNumeros(keyascii))
  30. If keyascii = 0 Then
  31. e.Handled = True
  32. End If
  33. End Sub