Mascara Numérica Textbox (colaboración)
Publicado por Haf (178 intervenciones) el 03/04/2008 11:19:16
Ayer en la mañana me dediqué a escribir este código el, cual permite ir dando formato de tipo numerico e inclusive colocar el signo negativo.
Tiene una falla que se puede mejorar y es que al inicio si se pulsa directamente un punto decimal (como para colocar 0.45) se queda en la parte decimal, Ya hoy no tengo más tiempo de terminarlo, también se podría cambiar la cantidad de decimales que se desean manejar. Si alguien lo termina o lo mejora, por favor me lo envía a mi correo [email protected]
Saludos
Public Class Form1
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
e.Handled = solonumeros(System.Convert.ToInt16(Asc(e.KeyChar)))
End Sub
Private Sub TextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
Me.TextBox1.Text = Mascara(TextBox1, e.KeyValue)
End Sub
Public Shared Function Mascara(ByVal txt As TextBox, ByVal tecla As Integer, Optional ByVal decimales As Integer = 2) As String
Dim SeparadorDecimal As String = My.Application.Culture.NumberFormat.NumberDecimalSeparator
Dim SeparadorMillar As String = My.Application.Culture.NumberFormat.NumberGroupSeparator
Dim ASCII As String = ""
txt.TextAlign = HorizontalAlignment.Right
If tecla >= 48 And tecla <= 57 Then
ASCII = CStr(tecla - 48)
End If
Dim Posicion_Cursor As Integer = txt.SelectionStart
If Trim(txt.Text.Length) <= 0 Then
Return Nothing
End If
If txt.Text.Length >= 3 Then
If Mid(txt.Text, Len(txt.Text) - 1) <> "00" Then
If tecla >= 48 And tecla <= 57 Then
If ASCII.Length > 1 Then
ASCII = Mid(ASCII, 2, 1)
End If
txt.Text = Mid(txt.Text, 1, Len(txt.Text) - 1) + ASCII
End If
End If
End If
If tecla = 109 Then 'signo menos
If txt.Text.Length >= 3 Then
If Mid(txt.Text, 1, 1) <> "-" Then
txt.Text = "-" + txt.Text
Else
txt.Text = txt.Text.Replace("-", "")
End If
SendKeys.Send("{END}")
End If
End If
If tecla = 190 Or tecla = 188 Then 'punto decimal (punto - coma)
If txt.Text.Length = 1 Then
txt.Text = "0" + SeparadorDecimal + "00"
txt.SelectionStart = InStr(txt.Text, SeparadorDecimal) '",")
Else
If SeparadorDecimal = "," Then
txt.Text = txt.Text.Replace(".,", SeparadorDecimal)
Else
txt.Text = txt.Text.Replace("..", SeparadorDecimal)
End If
txt.SelectionStart = InStr(txt.Text, SeparadorDecimal) ' ",")
End If
Else
If tecla <> 39 And tecla <> 37 And tecla <> 46 Then 'der,izq,del
txt.Text = FormatNumber(txt.Text, 2)
txt.SelectionStart = InStr(txt.Text, SeparadorDecimal) - 1
End If
If tecla = 46 Then 'delete
If Posicion_Cursor = Trim(txt.Text).Length - 2 Then 'borra el caracter de la coma decimal
txt.Text = Mid(txt.Text, 1, Posicion_Cursor) + SeparadorDecimal + Mid(txt.Text, Posicion_Cursor + 1, 2)
End If
If Mid(txt.Text, 1, 1) = SeparadorMillar Then 'borra el punto de unidades de mil
txt.Text = Mid(txt.Text, 2, Len(txt.Text))
End If
SendKeys.Send("{DOWN}")
End If
End If
Return txt.Text
End Function
Public Shared Function solonumeros(ByVal Kcode As Int16) As Boolean
If (Kcode >= 48 And Kcode <= 57) Or Kcode = 8 Or Kcode = 46 Or Kcode = 13 Or Kcode = 39 Or Kcode = 37 Or Kcode = 190 Or Kcode = 109 Then
Return False
Else
Return True
End If
End Function
End Class
Tiene una falla que se puede mejorar y es que al inicio si se pulsa directamente un punto decimal (como para colocar 0.45) se queda en la parte decimal, Ya hoy no tengo más tiempo de terminarlo, también se podría cambiar la cantidad de decimales que se desean manejar. Si alguien lo termina o lo mejora, por favor me lo envía a mi correo [email protected]
Saludos
Public Class Form1
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
e.Handled = solonumeros(System.Convert.ToInt16(Asc(e.KeyChar)))
End Sub
Private Sub TextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
Me.TextBox1.Text = Mascara(TextBox1, e.KeyValue)
End Sub
Public Shared Function Mascara(ByVal txt As TextBox, ByVal tecla As Integer, Optional ByVal decimales As Integer = 2) As String
Dim SeparadorDecimal As String = My.Application.Culture.NumberFormat.NumberDecimalSeparator
Dim SeparadorMillar As String = My.Application.Culture.NumberFormat.NumberGroupSeparator
Dim ASCII As String = ""
txt.TextAlign = HorizontalAlignment.Right
If tecla >= 48 And tecla <= 57 Then
ASCII = CStr(tecla - 48)
End If
Dim Posicion_Cursor As Integer = txt.SelectionStart
If Trim(txt.Text.Length) <= 0 Then
Return Nothing
End If
If txt.Text.Length >= 3 Then
If Mid(txt.Text, Len(txt.Text) - 1) <> "00" Then
If tecla >= 48 And tecla <= 57 Then
If ASCII.Length > 1 Then
ASCII = Mid(ASCII, 2, 1)
End If
txt.Text = Mid(txt.Text, 1, Len(txt.Text) - 1) + ASCII
End If
End If
End If
If tecla = 109 Then 'signo menos
If txt.Text.Length >= 3 Then
If Mid(txt.Text, 1, 1) <> "-" Then
txt.Text = "-" + txt.Text
Else
txt.Text = txt.Text.Replace("-", "")
End If
SendKeys.Send("{END}")
End If
End If
If tecla = 190 Or tecla = 188 Then 'punto decimal (punto - coma)
If txt.Text.Length = 1 Then
txt.Text = "0" + SeparadorDecimal + "00"
txt.SelectionStart = InStr(txt.Text, SeparadorDecimal) '",")
Else
If SeparadorDecimal = "," Then
txt.Text = txt.Text.Replace(".,", SeparadorDecimal)
Else
txt.Text = txt.Text.Replace("..", SeparadorDecimal)
End If
txt.SelectionStart = InStr(txt.Text, SeparadorDecimal) ' ",")
End If
Else
If tecla <> 39 And tecla <> 37 And tecla <> 46 Then 'der,izq,del
txt.Text = FormatNumber(txt.Text, 2)
txt.SelectionStart = InStr(txt.Text, SeparadorDecimal) - 1
End If
If tecla = 46 Then 'delete
If Posicion_Cursor = Trim(txt.Text).Length - 2 Then 'borra el caracter de la coma decimal
txt.Text = Mid(txt.Text, 1, Posicion_Cursor) + SeparadorDecimal + Mid(txt.Text, Posicion_Cursor + 1, 2)
End If
If Mid(txt.Text, 1, 1) = SeparadorMillar Then 'borra el punto de unidades de mil
txt.Text = Mid(txt.Text, 2, Len(txt.Text))
End If
SendKeys.Send("{DOWN}")
End If
End If
Return txt.Text
End Function
Public Shared Function solonumeros(ByVal Kcode As Int16) As Boolean
If (Kcode >= 48 And Kcode <= 57) Or Kcode = 8 Or Kcode = 46 Or Kcode = 13 Or Kcode = 39 Or Kcode = 37 Or Kcode = 190 Or Kcode = 109 Then
Return False
Else
Return True
End If
End Function
End Class
Valora esta pregunta


0