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
Suscribirse a:
Enviar comentarios (Atom)
Otra forma de que un textbox acepte solo numeros
ResponderEliminarPrivate Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If InStr("0123456789.,-" & Chr(8), e.KeyChar) Then
e.Handled = False
Else
e.Handled = True
End If
End Sub
Private Sub TextBox1_KeyPress(ByVal ......)....
ResponderEliminarif not isnumeric(e.keychar)=true then e.handled=true
End Sub
esto lo harias en el evento keypress del control.