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

Share this post

Link to post
Share on other sites

mas no meu projeto quando vou as propiedads do textbox nao tem essa opçao

Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • 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.