Usuário com melhor resposta
Formatar Textbox "Valor" ao digitar

Pergunta
-
Boa tarde pessoal,
Estou encontrando dificuldade em formatar uma textbox ao digitar, a ideia principal seria a seguinte:
Digamos que eu vá incluir o seguinte valor 15.482,45.
sem digitar nada na textbox gostaria de ter o valor inicial de ",00"
e conforme incluir os valor ficariam assim...
1,0015,00
154,00
1.548,00
15.482,00
e somente após clicar na vírgula ele preencher os valores após a ela...
15.482,4015.482,45
Alguém tem alguma dica de como reproduzir esse exemplo?
Respostas
-
Tudo certo Felipe e vc?
Acredito que seria uma "How to/Customização" não sou um grande conhecedor de dos termos técnicos,
Consegue reproduzir o que queria com os seguintes comandos:
Private Sub TxtCartao_Click(sender As Object, e As EventArgs) Handles TxtCartao.Click
TxtCartao.SelectAll()
TeclaPress2 = ""
End SubPrivate Sub TxtCartao_GotFocus(sender As Object, e As EventArgs) Handles TxtCartao.GotFocus
TxtCartao.SelectAll()
TeclaPress2 = ""
End SubPrivate Sub TxtCartao_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TxtCartao.KeyPress
Dim allowedChars As String = "0123456789"
If allowedChars.IndexOf(e.KeyChar) = -1 And (e.KeyChar <> Chr(8)) Then
e.Handled = True
End If
Dim Posicao As Long = TxtCartao.SelectionStart
If e.KeyChar = "," Then
TeclaPress = "2"
TeclaPress2 = "2"
TxtCartao.SelectionStart = TxtCartao.Text.Length - TeclaPress
Else
If TeclaPress2 = "2" Then
TeclaPress = "1"
TeclaPress2 = "1"
Else
If TeclaPress2 = "1" Then
TeclaPress = "0"
'TeclaPress2 = "0"
Else
TeclaPress = "3"
End If
End If
End If
If e.KeyChar = Chr(13) Then
' troca enter por o tab
SendKeys.Send("{TAB}")
' Tira o som da tecla enter
e.Handled = True
End If
End SubPrivate Sub TxtCartao_Leave(sender As Object, e As EventArgs) Handles TxtCartao.Leave
TotalTxt()
BtnSalvar.Enabled = True
End SubPrivate Sub TxtCartao_TextChanged(sender As Object, e As EventArgs) Handles TxtCartao.TextChanged
Dim Posicao As Long = TxtCartao.SelectionStart
Dim PontosInicial As String
Dim PontosFinal As String
Posicao = Replace(TxtCartao.SelectionStart, ".", "")
PontosInicial = ContaCaracteresNaString(TxtCartao.Text, ".")
Dim valor As Double
Try
valor = Double.Parse(TxtCartao.Text)
TxtCartao.Text = valor.ToString("#,#.00;(#,#.00)")
PontosFinal = ContaCaracteresNaString(TxtCartao.Text, ".")
If TeclaPress = "2" Then
TxtCartao.SelectionStart = TxtCartao.Text.Length - TeclaPress
ElseIf TeclaPress = "1" Then
TxtCartao.SelectionStart = TxtCartao.Text.Length - TeclaPress
ElseIf TeclaPress = "0" Then
TxtCartao.SelectionStart = TxtCartao.Text.Length
ElseIf TeclaPress = "3" Then
If PontosInicial <> PontosFinal Then
Posicao += 1
End If
TxtCartao.SelectionStart = Posicao
TeclaPress2 = ""
End If
Catch ex As Exception
End Try
End SubÉ uma gambiarra, acredito que pode ser aperfeiçoado por alguém com mais conhecimentos.
- Marcado como Resposta Filipe B CastroModerator sexta-feira, 11 de agosto de 2017 20:40
Todas as Respostas
-
Boa tarde, Juliano Bauer. Tudo bem?
Obrigado por usar o fórum MSDN.
Essa seria uma questão de "Break Fix/Erro" ou "How to/Customização"?
Atenciosamente,Filipe B de Castro
Esse conteúdo é fornecido sem garantias de qualquer tipo, seja expressa ou implícita
MSDN Community Support
Por favor, lembre-se de Marcar como Resposta as postagens que resolveram o seu problema. Essa é uma maneira comum de reconhecer aqueles que o ajudaram e fazer com que seja mais fácil para os outros visitantes encontrarem a resolução mais tarde.
-
Tudo certo Felipe e vc?
Acredito que seria uma "How to/Customização" não sou um grande conhecedor de dos termos técnicos,
Consegue reproduzir o que queria com os seguintes comandos:
Private Sub TxtCartao_Click(sender As Object, e As EventArgs) Handles TxtCartao.Click
TxtCartao.SelectAll()
TeclaPress2 = ""
End SubPrivate Sub TxtCartao_GotFocus(sender As Object, e As EventArgs) Handles TxtCartao.GotFocus
TxtCartao.SelectAll()
TeclaPress2 = ""
End SubPrivate Sub TxtCartao_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TxtCartao.KeyPress
Dim allowedChars As String = "0123456789"
If allowedChars.IndexOf(e.KeyChar) = -1 And (e.KeyChar <> Chr(8)) Then
e.Handled = True
End If
Dim Posicao As Long = TxtCartao.SelectionStart
If e.KeyChar = "," Then
TeclaPress = "2"
TeclaPress2 = "2"
TxtCartao.SelectionStart = TxtCartao.Text.Length - TeclaPress
Else
If TeclaPress2 = "2" Then
TeclaPress = "1"
TeclaPress2 = "1"
Else
If TeclaPress2 = "1" Then
TeclaPress = "0"
'TeclaPress2 = "0"
Else
TeclaPress = "3"
End If
End If
End If
If e.KeyChar = Chr(13) Then
' troca enter por o tab
SendKeys.Send("{TAB}")
' Tira o som da tecla enter
e.Handled = True
End If
End SubPrivate Sub TxtCartao_Leave(sender As Object, e As EventArgs) Handles TxtCartao.Leave
TotalTxt()
BtnSalvar.Enabled = True
End SubPrivate Sub TxtCartao_TextChanged(sender As Object, e As EventArgs) Handles TxtCartao.TextChanged
Dim Posicao As Long = TxtCartao.SelectionStart
Dim PontosInicial As String
Dim PontosFinal As String
Posicao = Replace(TxtCartao.SelectionStart, ".", "")
PontosInicial = ContaCaracteresNaString(TxtCartao.Text, ".")
Dim valor As Double
Try
valor = Double.Parse(TxtCartao.Text)
TxtCartao.Text = valor.ToString("#,#.00;(#,#.00)")
PontosFinal = ContaCaracteresNaString(TxtCartao.Text, ".")
If TeclaPress = "2" Then
TxtCartao.SelectionStart = TxtCartao.Text.Length - TeclaPress
ElseIf TeclaPress = "1" Then
TxtCartao.SelectionStart = TxtCartao.Text.Length - TeclaPress
ElseIf TeclaPress = "0" Then
TxtCartao.SelectionStart = TxtCartao.Text.Length
ElseIf TeclaPress = "3" Then
If PontosInicial <> PontosFinal Then
Posicao += 1
End If
TxtCartao.SelectionStart = Posicao
TeclaPress2 = ""
End If
Catch ex As Exception
End Try
End SubÉ uma gambiarra, acredito que pode ser aperfeiçoado por alguém com mais conhecimentos.
- Marcado como Resposta Filipe B CastroModerator sexta-feira, 11 de agosto de 2017 20:40