Jump to content

Casas decimais


Recommended Posts


Bom dia a todos
Preciso da  ajuda do pessoal do forum
Gostaria de permitir apenas duas casas decimais.
Eu poderia definir a caixa de texto apenas numérica, mas não sei como limitar 2 pontos decimais.
ex: 000,00

Private Sub txtPrice_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtPrice.KeyPress
           Dim keyChar = e.KeyChar
          If Char.IsControl(keyChar) Then
'Allow all control characters./ 'Permitir todos os caracteres de controle.

        ElseIf Char.IsDigit(keyChar) OrElse keyChar = "."c Then
           Dim text = Me.txtPrice.Text
           Dim selectionStart = Me.txtPrice.SelectionStart
           Dim selectionLength = Me.txtPrice.SelectionLength

          text = text.Substring(0, selectionStart) & keyChar & text.Substring(selectionStart + selectionLength)

           If Integer.TryParse(text, New Integer) AndAlso text.Length > 16 Then

 'Reject an integer that is longer than 16 digits.

                e.Handled = True
                ElseIf Double.TryParse(text, New Double) AndAlso text.IndexOf("."c) < text.Length - 3 Then

'Reject a real number with two many decimal places.
                e.Handled = False

            End If
'Reject all other characters.
             e.Handled = True

        End If
      End Sub
End Class

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.