Mostrando entradas con la etiqueta Hacer que un textbox acepte solo numeros. Mostrar todas las entradas
Mostrando entradas con la etiqueta Hacer que un textbox acepte solo numeros. Mostrar todas las entradas

lunes, 5 de octubre de 2009

Hacer que un textbox acepte solo numeros

iniciar un proyecto winform y añadir un textbox en el form:

Declarar la variable KeyAscii
Dim KeyAscii As Short

Crear esta función:
Function Numeros(ByVal Keyascii As Short) As Short
If InStr("1234567890", Chr(Keyascii)) = 0 Then
Numeros = 0
Else
Numeros = Keyascii
End If
Select Case Keyascii
Case 8
Numeros = Keyascii
Case 13
Numeros = Keyascii
End Select
End Function

Después en el evento keypress del textbox1,poner este codigo:

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
Dim KeyAscii As Short = CShort(Asc(e.KeyChar))
KeyAscii = CShort(Numeros(KeyAscii))
If KeyAscii = 0 Then
e.Handled = True
End If
End Sub