Ir para conteúdo
Fórum Script Brasil
  • 0

Formatar Textbox "Valor" ao digitar


Juliano Kurschbauer

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,00

15,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,40

15.482,45

Alguém tem alguma dica de como reproduzir esse exemplo?

Editado por Juliano Kurschbauer
Link para o comentário
Compartilhar em outros sites

2 respostass a esta questão

Posts Recomendados

  • 0

Consegui 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 Sub

 

Private Sub TxtCartao_GotFocus(sender As Object, e As EventArgs) Handles TxtCartao.GotFocus
        TxtCartao.SelectAll()
        TeclaPress2 = ""
    End Sub

 

Private 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 Sub

 

 Private Sub TxtCartao_Leave(sender As Object, e As EventArgs) Handles TxtCartao.Leave
        TotalTxt()
        BtnSalvar.Enabled = True
    End Sub

 

Private 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, alguém tem alguma sugestão?

Link para o comentário
Compartilhar em outros sites

  • 0

Ajustando melhor ficou assim:
 

    Public TeclaPress As String
    Public TeclaPress2 As String

 

  Private Sub txtEnviado_Click(sender As Object, e As EventArgs) Handles txtEnviado.Click
        TeclaPress2 = ""
    End Sub

    Private Sub txtEnviado_DoubleClick(sender As Object, e As EventArgs) Handles txtEnviado.DoubleClick
        sender.SelectAll()
    End Sub

    Private Sub txtEnviado_GotFocus(sender As Object, e As EventArgs) Handles txtEnviado.GotFocus
        sender.SelectAll()
        TeclaPress2 = ""
    End Sub

    Private Sub txtEnviado_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtEnviado.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 = sender.SelectionStart

        If e.KeyChar = "," Then
            TeclaPress = "2"
            TeclaPress2 = "2"
            sender.SelectionStart = sender.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 Sub

    Private Sub txtEnviado_TextChanged(sender As Object, e As EventArgs) Handles txtEnviado.TextChanged

        Dim Posicao As Long = sender.SelectionStart
        Dim PontosInicial As String
        Dim PontosFinal As String

        Posicao = Replace(sender.SelectionStart, ".", "")
        PontosInicial = ContaCaracteresNaString(sender.Text, ".")

        Dim valor As Double
        Try
            valor = Double.Parse(sender.Text)
            sender.Text = valor.ToString("#,#.00;(#,#.00)")
            PontosFinal = ContaCaracteresNaString(sender.Text, ".")

            If TeclaPress = "2" Then
                sender.SelectionStart = sender.Text.Length - TeclaPress
            ElseIf TeclaPress = "1" Then
                sender.SelectionStart = sender.Text.Length - TeclaPress

            ElseIf TeclaPress = "0" Then
                sender.SelectionStart = sender.Text.Length
            ElseIf TeclaPress = "3" Then

                If PontosInicial <> PontosFinal Then
                    Posicao += 1
                End If

                sender.SelectionStart = Posicao
                TeclaPress2 = ""

            End If

        Catch ex As Exception

        End Try

    End Sub


Tinha esquecido da Função:

 Public Function ContaCaracteresNaString(ByVal texto As String, ByVal caracter As String) As Long
        Dim x As Object
        x = Split(texto, caracter)
        ContaCaracteresNaString = UBound(x)
    End Function

Link para o comentário
Compartilhar em outros sites

Participe da discussão

Você pode postar agora e se registrar depois. Se você já tem uma conta, acesse agora para postar com sua conta.

Visitante
Responder esta pergunta...

×   Você colou conteúdo com formatação.   Remover formatação

  Apenas 75 emoticons são permitidos.

×   Seu link foi incorporado automaticamente.   Exibir como um link em vez disso

×   Seu conteúdo anterior foi restaurado.   Limpar Editor

×   Você não pode colar imagens diretamente. Carregar ou inserir imagens do URL.



  • Estatísticas dos Fóruns

    • Tópicos
      152k
    • Posts
      651,7k
×
×
  • Criar Novo...