Discussion:
Aceptar sólo un punto decimal en TextBox
(demasiado antiguo para responder)
Elena
hace 18 años
Permalink
Hola a todos.

Estoy usando TextBox el cual solo recibe números, pero si tecleo dos veces
el punto, lo acepta.

En ese cuadro debe aceptar sólo números, sean enteros o con decimales, pero
por supuesto, con un sólo punto decimal.

¿Podría alguien ayudarme, por favor?

De antemano, muchas gracias

Elena

P.D. Actualmente tengo este código para los números:

If ((KeyAscii < 48 Or KeyAscii > 57) And (KeyAscii <> 46 And KeyAscii <>
44)) Then
KeyAscii = 0
End If

--
Héctor Miguel
hace 18 años
Permalink
hola, Elena !
Estoy usando TextBox el cual solo recibe numeros, pero si tecleo dos veces el punto, lo acepta.
En ese cuadro debe aceptar solo numeros, sean enteros o con decimales, pero por supuesto, con un solo punto decimal.
If ((KeyAscii < 48 Or KeyAscii > 57) And (KeyAscii <> 46 And KeyAscii <> 44)) Then
KeyAscii = 0
End If
suponiendo que tambien quieres limitar el uso de comas a una sola [el KeyAscii 44]...
prueba cambiando a lo siguiente... OJO: no se requiere poner un -> End If
[solo modifica de ser necesario el nombre del TextBox1] ;)

If (KeyAscii < 48 Or KeyAscii > 57) _
And (KeyAscii <> 46 And KeyAscii <> 44) _
Or (KeyAscii = 46 And TextBox1 Like "*.*") _
Or (KeyAscii = 44 And TextBox1 Like "*,*") _
Then KeyAscii = 0

si cualquier duda [o informacion adicional]... comentas ?
saludos,
hector.
m***@gmail.com
hace 18 años
Permalink
Yo le agregaria el siguiente código al evento Change del textbox en
cuestión

Private Sub TextBox1_Change()

On Error Resume Next

If TextBox1.Text <> "" Then
If IsNumeric(TextBox1.Text) Then
If Not IsNumeric(Right(TextBox1.Text, 1)) And _
Right(TextBox1.Text, 1) <> "." Then
TextBox1.Text = Mid(TextBox1.Text, 1, Len(TextBox1.Text) - 1)
End If
ElseIf Not IsNumeric(TextBox1.Text) Then
TextBox1.Text = Mid(TextBox1.Text, 1, Len(TextBox1.Text) - 1)
End If
End If

End Sub

Marco Barboza - San Jose, Costa Rica
...
Elena
hace 18 años
Permalink
Héctor y Marco.

Muchas gracias por su ayuda, me fue de mucha utilidad.

Siendo sincera, sólo usé el código que envió Héctor, ya que sólo era añadir
algunas líneas a lo que ya tenía.

De cualquier manera, gracias a los dos

Saludos
--
<***@gmail.com> escribi� en el mensaje news:***@l77g2000hsb.googlegroups.com...
Yo le agregaria el siguiente código al evento Change del textbox en
cuestión

Private Sub TextBox1_Change()

On Error Resume Next

If TextBox1.Text <> "" Then
If IsNumeric(TextBox1.Text) Then
If Not IsNumeric(Right(TextBox1.Text, 1)) And _
Right(TextBox1.Text, 1) <> "." Then
TextBox1.Text = Mid(TextBox1.Text, 1, Len(TextBox1.Text) - 1)
End If
ElseIf Not IsNumeric(TextBox1.Text) Then
TextBox1.Text = Mid(TextBox1.Text, 1, Len(TextBox1.Text) - 1)
End If
End If

End Sub

Marco Barboza - San Jose, Costa Rica
...
Continúe leyendo en narkive:
Loading...