Mascara Numérica Textbox (colaboración)
Publicado por Mascara Numérica Textbox (cola (178 intervenciones) el 07/04/2008 17:24:13
La semana pasada hice una rutina para que le diera formato numérico a un textbox y la publiqué en este foro.
Pero he creado una nueva rutina que está mucho mejor, la cual deseo dejársela a todos los amigos del foro.
Para probarla :
Creen un nuevo formulario , borren todo el código y copien este código y simplemente añadan un textbox.
Acepto sus respetuosas sugerencias y mejoras.
Public Class Form1
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 = 37 Or Kcode = 190 Then
Return False
Else
Return True
End If
End Function
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
Mascara(sender, e.KeyCode, 2)
End Sub
Private Sub Mascara(ByVal Txt As TextBox, ByVal valor As Integer, Optional ByVal decimales As Integer = 2)
Txt.TextAlign = HorizontalAlignment.Right
Dim Formato As String = "#,##0." & StrDup(decimales, "0")
Dim Negativo As Boolean = False
If Mid(Txt.Text, 1, 1) = "-" Then
Negativo = True
End If
Txt.Text = Txt.Text.Replace(" ", "")
If valor = 46 Then 'delete()
If Txt.Text.Length = 0 Then ' se ha borrado todo
Txt.Text = "0," & StrDup(decimales, "0") 'Replicate
Txt.Tag = ""
Else
If InStr(Txt.Text, ",") = 0 Then 'SE HA BORRADO EN LA POSCION DEL PUNTO DECIMAL
Txt.Text = Txt.Text.Replace(",", "")
Txt.Text = Txt.Text.Replace(".", "")
Try
Txt.Text = Mid(Txt.Text, 1, Len(Txt.Text) - decimales) + "," + Mid(Txt.Text, Len(Txt.Text) - (decimales - 1))
Catch ex As Exception
If Txt.Text.Length = 1 Then
Txt.Text = Txt.Text + "," + StrDup(decimales, "0")
End If
End Try
Try
Txt.Text = Format(CDbl(Txt.Text), Formato)
Catch ex As Exception ' solo en el caso que sea negativo
Txt.Text = Txt.Text.Replace(" ", "")
Txt.Text = Txt.Text.Replace(".", "")
Txt.Text = Txt.Text.Replace(",", "")
Txt.Text = Txt.Text.Replace("-", "")
Txt.Text = Txt.Text + "," + StrDup(decimales, "0")
If Negativo Then
Txt.Text = "-" + Txt.Text
End If
End Try
Txt.SelectionStart = Len(Txt.Text) - (decimales + 1)
Exit Sub
End If
Try
Txt.Text = Format(CDbl(Txt.Text), Formato)
Catch ex As Exception
Txt.Text = Txt.Text.Replace(".", "")
End Try
Txt.SelectionStart = Len(Txt.Text) - (decimales + 1)
End If
End If
If Trim(Txt.Text.Length) = 0 Or Mid(Txt.Text, 1, 1) = "." Then
Txt.Text = "0," & StrDup(decimales, "0") 'Replicate
Txt.Tag = ""
End If
Try
If (Mid(Txt.Tag, 1, 5) = "PUNTO") Then
If InStr(Txt.Tag, ",") > 0 Then ' HA LLEGADO A LA ÚLTIMA POSICION DECIMAL
If CInt(Mid(Txt.Tag, 7, 1)) >= decimales Then
Txt.Text = Mid(Txt.Text, 1, Len(Txt.Text) - 1)
End If
End If
End If
Txt.Text = Format(CDbl(Txt.Text), Formato)
Catch ex As Exception
Txt.Text = Txt.Text.Replace(" ", "")
Txt.Text = Txt.Text.Replace(",", "")
Txt.Text = Txt.Text.Replace(".", "")
Txt.Text = Mid(Txt.Text, 1, Len(Txt.Text) - decimales) + "," + Mid(Txt.Text, Len(Txt.Text) - decimales + 1)
Txt.Text = Format(CDbl(Txt.Text), Formato)
End Try
If valor <> 37 And valor <> 39 Then 'left rigth
Txt.SelectionStart = Len(Txt.Text) - (decimales + 1)
End If
Dim Posiciones(decimales) As String
If Mid(Txt.Tag, 1, 5) = "PUNTO" Then 'SE HA PULSADO EL PUNTO DECIMAL
Posiciones = Split(Txt.Tag, ",")
If Posiciones.Length = 1 Then
Else
Posiciones = Split(Txt.Tag, ",")
If CInt(Posiciones(1)) + 1 <= decimales Then
Txt.Tag = "PUNTO," + CStr(CInt(Posiciones(1)) + 1)
Txt.SelectionStart = Len(Txt.Text) - decimales + CInt(Posiciones(1))
Else
Txt.Tag = ""
End If
End If
End If
If valor = 190 Then 'punto decimal
Txt.SelectionStart = Len(Txt.Text) - decimales '2
Txt.Tag = "PUNTO,1"
End If
If valor = 109 Then 'menos -
If Txt.Text <> "0," & StrDup(decimales, "0") Then
If Mid(Txt.Text, 1, 1) = "-" Then
Txt.Text = Txt.Text.Replace("-", "")
Else
Txt.Text = "-" & Txt.Text
End If
Txt.SelectionStart = Len(Txt.Text) - (decimales + 1)
End If
End If
End Sub
Public Shared Function SinMascara(ByVal valor As String, Optional ByVal decimales As Integer = 2) As String
' hecho para sumar los campos el cual quita el formato y lo entrega en formato para suma
If valor.Length <= 0 Then
Return Nothing
End If
valor = valor.Replace(" ", "")
valor = valor.Replace(",", "")
valor = valor.Replace(".", "")
valor = Mid(valor, 1, Len(valor) - decimales) + "." + Mid(valor, Len(valor) - decimales + 1)
Return valor
End Function
End Class
Pero he creado una nueva rutina que está mucho mejor, la cual deseo dejársela a todos los amigos del foro.
Para probarla :
Creen un nuevo formulario , borren todo el código y copien este código y simplemente añadan un textbox.
Acepto sus respetuosas sugerencias y mejoras.
Public Class Form1
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 = 37 Or Kcode = 190 Then
Return False
Else
Return True
End If
End Function
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
Mascara(sender, e.KeyCode, 2)
End Sub
Private Sub Mascara(ByVal Txt As TextBox, ByVal valor As Integer, Optional ByVal decimales As Integer = 2)
Txt.TextAlign = HorizontalAlignment.Right
Dim Formato As String = "#,##0." & StrDup(decimales, "0")
Dim Negativo As Boolean = False
If Mid(Txt.Text, 1, 1) = "-" Then
Negativo = True
End If
Txt.Text = Txt.Text.Replace(" ", "")
If valor = 46 Then 'delete()
If Txt.Text.Length = 0 Then ' se ha borrado todo
Txt.Text = "0," & StrDup(decimales, "0") 'Replicate
Txt.Tag = ""
Else
If InStr(Txt.Text, ",") = 0 Then 'SE HA BORRADO EN LA POSCION DEL PUNTO DECIMAL
Txt.Text = Txt.Text.Replace(",", "")
Txt.Text = Txt.Text.Replace(".", "")
Try
Txt.Text = Mid(Txt.Text, 1, Len(Txt.Text) - decimales) + "," + Mid(Txt.Text, Len(Txt.Text) - (decimales - 1))
Catch ex As Exception
If Txt.Text.Length = 1 Then
Txt.Text = Txt.Text + "," + StrDup(decimales, "0")
End If
End Try
Try
Txt.Text = Format(CDbl(Txt.Text), Formato)
Catch ex As Exception ' solo en el caso que sea negativo
Txt.Text = Txt.Text.Replace(" ", "")
Txt.Text = Txt.Text.Replace(".", "")
Txt.Text = Txt.Text.Replace(",", "")
Txt.Text = Txt.Text.Replace("-", "")
Txt.Text = Txt.Text + "," + StrDup(decimales, "0")
If Negativo Then
Txt.Text = "-" + Txt.Text
End If
End Try
Txt.SelectionStart = Len(Txt.Text) - (decimales + 1)
Exit Sub
End If
Try
Txt.Text = Format(CDbl(Txt.Text), Formato)
Catch ex As Exception
Txt.Text = Txt.Text.Replace(".", "")
End Try
Txt.SelectionStart = Len(Txt.Text) - (decimales + 1)
End If
End If
If Trim(Txt.Text.Length) = 0 Or Mid(Txt.Text, 1, 1) = "." Then
Txt.Text = "0," & StrDup(decimales, "0") 'Replicate
Txt.Tag = ""
End If
Try
If (Mid(Txt.Tag, 1, 5) = "PUNTO") Then
If InStr(Txt.Tag, ",") > 0 Then ' HA LLEGADO A LA ÚLTIMA POSICION DECIMAL
If CInt(Mid(Txt.Tag, 7, 1)) >= decimales Then
Txt.Text = Mid(Txt.Text, 1, Len(Txt.Text) - 1)
End If
End If
End If
Txt.Text = Format(CDbl(Txt.Text), Formato)
Catch ex As Exception
Txt.Text = Txt.Text.Replace(" ", "")
Txt.Text = Txt.Text.Replace(",", "")
Txt.Text = Txt.Text.Replace(".", "")
Txt.Text = Mid(Txt.Text, 1, Len(Txt.Text) - decimales) + "," + Mid(Txt.Text, Len(Txt.Text) - decimales + 1)
Txt.Text = Format(CDbl(Txt.Text), Formato)
End Try
If valor <> 37 And valor <> 39 Then 'left rigth
Txt.SelectionStart = Len(Txt.Text) - (decimales + 1)
End If
Dim Posiciones(decimales) As String
If Mid(Txt.Tag, 1, 5) = "PUNTO" Then 'SE HA PULSADO EL PUNTO DECIMAL
Posiciones = Split(Txt.Tag, ",")
If Posiciones.Length = 1 Then
Else
Posiciones = Split(Txt.Tag, ",")
If CInt(Posiciones(1)) + 1 <= decimales Then
Txt.Tag = "PUNTO," + CStr(CInt(Posiciones(1)) + 1)
Txt.SelectionStart = Len(Txt.Text) - decimales + CInt(Posiciones(1))
Else
Txt.Tag = ""
End If
End If
End If
If valor = 190 Then 'punto decimal
Txt.SelectionStart = Len(Txt.Text) - decimales '2
Txt.Tag = "PUNTO,1"
End If
If valor = 109 Then 'menos -
If Txt.Text <> "0," & StrDup(decimales, "0") Then
If Mid(Txt.Text, 1, 1) = "-" Then
Txt.Text = Txt.Text.Replace("-", "")
Else
Txt.Text = "-" & Txt.Text
End If
Txt.SelectionStart = Len(Txt.Text) - (decimales + 1)
End If
End If
End Sub
Public Shared Function SinMascara(ByVal valor As String, Optional ByVal decimales As Integer = 2) As String
' hecho para sumar los campos el cual quita el formato y lo entrega en formato para suma
If valor.Length <= 0 Then
Return Nothing
End If
valor = valor.Replace(" ", "")
valor = valor.Replace(",", "")
valor = valor.Replace(".", "")
valor = Mid(valor, 1, Len(valor) - decimales) + "." + Mid(valor, Len(valor) - decimales + 1)
Return valor
End Function
End Class
Valora esta pregunta


1