Limit the # of digits after a decimal in a KeyPress Event Procedure

  • 13 years ago
    Greetings Everyone!
    I'm currently just starting to learn VB.net on my own. I have a simple question in what is the correct way to restrict the number of digits after the decimal point? As shown with my code below I was able to create a textbox in which when a user enters value such as 1232.23232 is possible...however I want to make the textbox more user friendly I restricting digits after the decimal to 2. So it would look more like this 1232.23. Thank you for all your time..any guidance and help would be greatly appreciated.

    Public Class Form1

    Private Sub txtPayment_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtPayment.KeyPress
    Const x As Integer = 2
    If e.KeyChar = Chr(8) Then Exit Sub

    Select Case e.KeyChar
    Case "0" To "9"
    'ok
    Case "."
    If (InStr(txtPayment.Text, ".")) <> x Then
    e.Handled = True
    Beep()
    End If
    Case Else
    e.Handled = True
    Beep()
    End Select


    End Sub
    End Class























  • 13 years ago

    hi friend,

    pls change the code of marked in red

     

    Public Class Form1

    Private Sub txtPayment_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtPayment.KeyPress
    Const x As Integer = 2
    If e.KeyChar = Chr(8) Then Exit Sub

    Select Case e.KeyChar
    Case "0" To "9"
    'ok
    Case "."
    If  Len(Mid(txtPayment.Text, InStr(txtPayment.Text, "."))) <> x Then
    e.Handled = True
    Beep()
    End If
    Case Else
    e.Handled = True
    Beep()
    End Select


    End Sub
    End Class




















  • 13 years ago

    Greetings Tsalim! Thank you for your reply.

    However I tried replacing my code with your code in red. But it didn't work. Do you know of any problems there may be?

  • 13 years ago

    The case the red code is in, is for the "." being pressed.  For that case you should be limiting the user to a single ".".  What you should do there is test that a "." doesn't already exist in the textbox because a number can't have more then one ".".  You can do this easily using the contains method.  Something like

    If TextBox1.Text.Contains(".") then
    e.handled = true
    beep
    end if
    To limit the number of characters after the decimal is a little tricky. This is because a user editing the textbox isn't necessarily making a changes from left to right. They may go back to the 2nd position (which may be before the decimal) to enter a number they missed. One way to handle this would be to create the final string and then make sure you don't have to many digits after the decimal.  Try adding this code in case "0" to "9".
            Const MAX As Integer = 2
                'Create the Final string if we allow this character
                Dim temp As String = TextBox1.Text.Insert(TextBox1.SelectionStart, e.KeyChar)
                'Where is the "."
                Dim DecLoc As Integer = temp.IndexOf(".")
                If DecLoc >= 0 AndAlso temp.Length - DecLoc - 1 > MAX Then
                    'To many digits after the decimal place...Don't allow digit
                    e.Handled = True
                    Beep()
                End If
    
    You'll have to change a few variable names but that code should work for you.

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Anyone who considers arithmetic methods of producing random digits is, of course, in a state of sin.” - John von Neumann