Visual Basic.NET - Agregar solo 2 números despues del punto vb. net

 
Vista:
sin imagen de perfil

Agregar solo 2 números despues del punto vb. net

Publicado por Alonso (8 intervenciones) el 23/04/2015 02:48:25
Quisiera que me ayudaran con otro problema igual como hicieron con el anterior.

Lo que quiero hacer en una caja de texto es que cuando yo escriba punto solo me permita escribir solo dos números.

Ejemplo.
Si yo escribo 12. Que solo me permita escribir 2 números más Ejemplo 12.45
Y que no ya no puedo agregar más números despues del punto.
Gracias.
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
sin imagen de perfil

Agregar solo 2 números despues del punto vb. net

Publicado por Guillermo (42 intervenciones) el 23/04/2015 10:55:18
Pero bueno, eso no es para escribir moneda con 2 decimales? si es así, porqué no utilizas una máscara de entrada y te olvidas de tanto problema!
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
sin imagen de perfil

Agregar solo 2 números despues del punto vb. net

Publicado por Javier (3 intervenciones) el 30/04/2015 19:52:43
txtBox.Text = String.Format("{0:n2}", txtBox.Text);
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
sin imagen de perfil
Val: 23
Ha aumentado su posición en 3 puestos en Visual Basic.NET (en relación al último mes)
Gráfica de Visual Basic.NET

Agregar solo 2 números despues del punto vb. net

Publicado por Pedro Lopez (14 intervenciones) el 10/05/2015 03:44:18
Te envio el codigo de un modulo que cree hoy. Transforma un texbox para que solo admita números, signo menos, y separador de decimales. Se puede elegir la cantidad de decimales que uno quiera.
En el comentario al inicio va el código a poner en el texbox para llamar la función.
No lo tengo lo suficientemente probado así que espero ayuda.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
Module NUMERICO
    Public SEPARADOR As Char
    Public CODIGOSEPARADOR As Integer
    Public Function NUMERO(TEXTO As String, CODIGO As Integer, ByRef COD As Integer, DECIMALES As Integer) As Boolean
 
REM COMENTARIO DE COMO LLAMAR LA FUNCIÓN
        'se llama asi:
        '  Private Sub TextBox_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox7.KeyPress
        'Dim r As Integer
        'If NUMERO(TextBox7.Text, AscW(e.KeyChar), r, 2) = False Then
        REM el ejemplo es para 2 decimales
        'e.Handled = True
        'Exit Function
        'End If
        'e.KeyChar = ChrW(r)
        'End Function
REM FIN DEL COMENTARIO
 
        If CODIGOSEPARADOR = 0 Then SeparadorDecimales()
        COD = CODIGO
        If CODIGO = 46 Or CODIGO = 44 Then
            COD = CODIGOSEPARADOR
            CODIGO = COD
        End If
        If CODIGO = 45 And Len(TEXTO) = 0 Then
            COD = 45
            Return True
            Exit Function
        End If
        If CODIGO = CODIGOSEPARADOR And Len(TEXTO) < 1 Then
            COD = 48
            Return True
            Exit Function
        End If
        If CODIGO <> 8 Then
            For n = 1 To Len(TEXTO)
                If Mid(TEXTO, n, 1) = "," Then
                    If Len(TEXTO) - n + 1 > DECIMALES Then
                        Return False
                        Exit Function
                    End If
                End If
            Next
        End If
        If CODIGO = CODIGOSEPARADOR Then
            For n = 1 To Len(TEXTO)
                If Mid(TEXTO, n, 1) = "," Then
                    Return False
                    Exit Function
                End If
            Next
        End If
 
        If CODIGO = 8 Then
            NUMERO = True
            Return True
            Exit Function
        End If
        If CODIGO = CODIGOSEPARADOR And DECIMALES > 0 Then
            NUMERO = True
            Return True
            Exit Function
        End If
        If CODIGO >= 48 And CODIGO <= 57 Then
            Return True
            Exit Function
        End If
        Return False
    End Function
 
    Public Sub SeparadorDecimales()
        SEPARADOR = System.Globalization.CultureInfo.CurrentCulture.NumberFormat.CurrencyDecimalSeparator
        CODIGOSEPARADOR = AscW(SEPARADOR)
    End Sub
 
End Module

Espero te sirva
Saludos
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar