Foro de elhacker.net

Programación => Programación Visual Basic => Mensaje iniciado por: Red Mx en 4 Marzo 2006, 23:59 pm



Título: Ayuda como forzar a un textbox para que solo acepte numeros
Publicado por: Red Mx en 4 Marzo 2006, 23:59 pm
Hola bueno ise una aplicacion donde ocupo que un text box solo admita numeros por que si no truena tengo este codigo:

Sub Text2_Keypress(KeyAscii As Integer)
    If KeyAscii <> Asc("9") Then
    If KeyAscii <> 8 Then
    KeyAscii = 0
    End If
    End If
End Sub

pero solo deja poner el 9 y no los demas numeros no se donde la estoy regando no se si tu me puedas ayudar.


De antemano gracias


Título: Re: Ayuda como forzar a un textbox para que solo acepte numeros
Publicado por: Red Mx en 5 Marzo 2006, 00:10 am
Bueno gracias ya tengo el codigo

era este:

Private Sub Text1_KeyPress(KeyAscii As Integer)

    If InStr("0123456789", Chr(KeyAscii)) = 0 And KeyAscii <> 8 Then
        KeyAscii = 0
    End If

End Sub



Título: Re: Ayuda como forzar a un textbox para que solo acepte numeros
Publicado por: Hwagm en 5 Marzo 2006, 00:18 am
O asi:

Código:
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii < 47 Or KeyAscii > 57 Then
If KeyAscii <> 8 Then KeyAscii = 0
End If
End Sub


Título: Re: Ayuda como forzar a un textbox para que solo acepte numeros
Publicado por: .Slasher-K. en 5 Marzo 2006, 01:34 am
Código:
Private Sub txtData_LostFocus()
  If Not IsNumeric(txtData) Then
    Call MsgBox("Tenés que ingresar un valor numérico", vbExclamation)
    txtData = vbNullString
  End If
End Sub

o

Código:
Private Sub txtNum_Validate(Cancel As Boolean)

  If Not IsNumeric(txtData) Then
    Call MsgBox("Tenés que ingresar un valor numérico", vbExclamation)
    txtData = vbNullString
    Cancel = True
  End If
End Sub


Título: Re: Ayuda como forzar a un textbox para que solo acepte numeros
Publicado por: Red Mx en 5 Marzo 2006, 18:28 pm
Slasher-K y
Hwagm

Gracias.


Título: Re: Ayuda como forzar a un textbox para que solo acepte numeros
Publicado por: erick185 en 10 Marzo 2006, 04:53 am
Hola, prueba con esto:

Private Sub Text1_KeyPress(KeyAscii As Integer)   
If KeyAscii = 13 Then       
KeyAscii = 0        ' Para que no "pite"     
SendKeys "{tab}"    ' Envía una pulsación TAB   
ElseIf KeyAscii <> 8 Then    ' El 8 es la tecla de borrar (backspace)   
' Si después de añadirle la tecla actual no es un número...       
'If Not IsNumeric("0" & Text1.Text & Chr(KeyAscii)) Then   
If Not IsNumeric(Chr(KeyAscii)) Then        ' ... se desecha esa tecla y se avisa de que no es correcta            Beep           
KeyAscii = 0       
End If    End If
End Sub

Salu2