Results 1 to 13 of 13

Thread: [RESOLVED] Limit TextBox's decimal places

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2008
    Posts
    255

    Resolved [RESOLVED] Limit TextBox's decimal places

    Hi all,

    I have some textboxes in my program. How to limit the textbox to the decimal places? For example, in the amount textbox, i would limit the user to input two decimal places only while the exchange rate textbox would limit the user to input 7 decimal places.

    Thank you

  2. #2
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,123

    Re: Limit TextBox's decimal places

    This is from MartinLiss, have a look!
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  3. #3
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: Limit TextBox's decimal places

    Or perhaps MaskedEdit Control.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Sep 2008
    Posts
    255

    Re: Limit TextBox's decimal places

    thank for your solution.

    Can I just add some codes to the existing textbox instead of using the above two solutions? because the solutions above require the use of external library.

    What I actually want is just adding some codes in my VB program to achieve this requirement.

    Anyway, Many Thanks for your help

  5. #5
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657

    Re: Limit TextBox's decimal places

    Try something like this for your KeyPress event (in the statement "If Len(Mid$(txtRate.Text, InStr(txtRate.Text, ".") + 1)) >= 2 Then", substitute whatever limit you want for the "2").
    Code:
    Private Sub txtRate_KeyPress(KeyAscii As Integer)
    
        If KeyAscii < 32 Then
            ' let it go, it is a control char like backspace
            Exit Sub
        End If
    
        If InStr("0123456789.", Chr$(KeyAscii)) > 0 Then
            If InStr(txtRate.Text, ".") > 0 Then
                If Chr$(KeyAscii) = "." Then
                    ' do not allow more than one decimal point
                    KeyAscii = 0
                Else
                    If Len(Mid$(txtRate.Text, InStr(txtRate.Text, ".") + 1)) >= 2 Then
                        ' do not allow more than 2 digits past decimal point
                        KeyAscii = 0
                    End If
                End If
            End If
        Else
            KeyAscii = 0
        End If
        
    End Sub
    "It's cold gin time again ..."

    Check out my website here.

  6. #6
    Frenzied Member Jim Davis's Avatar
    Join Date
    Mar 2001
    Location
    Mars base one Username: Jim Davis Password: yCrm33
    Posts
    1,284

    Re: Limit TextBox's decimal places

    In some regions (just like here in hungary) the Dots are represent group of numbers, but not decimal places.

    1.511.211,30

  7. #7
    Frenzied Member Jim Davis's Avatar
    Join Date
    Mar 2001
    Location
    Mars base one Username: Jim Davis Password: yCrm33
    Posts
    1,284

    Re: Limit TextBox's decimal places

    This is i wrote a sec ago, will limit decimal places, also it support the region settings (will display , or . its can be aset on the regional settings in windows)

    vb Code:
    1. Private Sub Text1_Change()
    2. Static Before As String, sDot As String
    3. Const DecimalPlaces = 3
    4. Dim SStart As Long
    5.  
    6.   If InStr(Format("1,1", "#.0"), ".") > 0 Then sDot = "," Else sDot = "."
    7.  
    8.   If InStr(Text1.Text, sDot) > 0 Then
    9.     Text1.Text = Before
    10.     If Len(Text1.Text) < DecimalPlaces Then Exit Sub
    11.     Text1.SelStart = Len(Text1.Text) - DecimalPlaces
    12.     Exit Sub
    13.   End If
    14.  
    15.   SStart = Text1.SelStart
    16.   Text1.Text = Format(Text1.Text, "###0." + String(DecimalPlaces, "0"))
    17.  
    18.   If SStart > Len(Text1.Text) Then SStart = Len(Text1.Text)
    19.   Text1.SelStart = SStart
    20.  
    21.   Before = Text1.Text
    22. End Sub

    Its quite unfinished But you can start here.
    Last edited by Jim Davis; Dec 10th, 2008 at 12:24 PM.

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Sep 2008
    Posts
    255

    Re: Limit TextBox's decimal places

    Quote Originally Posted by BruceG
    Try something like this for your KeyPress event (in the statement "If Len(Mid$(txtRate.Text, InStr(txtRate.Text, ".") + 1)) >= 2 Then", substitute whatever limit you want for the "2").
    Code:
    Private Sub txtRate_KeyPress(KeyAscii As Integer)
    
        If KeyAscii < 32 Then
            ' let it go, it is a control char like backspace
            Exit Sub
        End If
    
        If InStr("0123456789.", Chr$(KeyAscii)) > 0 Then
            If InStr(txtRate.Text, ".") > 0 Then
                If Chr$(KeyAscii) = "." Then
                    ' do not allow more than one decimal point
                    KeyAscii = 0
                Else
                    If Len(Mid$(txtRate.Text, InStr(txtRate.Text, ".") + 1)) >= 2 Then
                        ' do not allow more than 2 digits past decimal point
                        KeyAscii = 0
                    End If
                End If
            End If
        Else
            KeyAscii = 0
        End If
        
    End Sub
    god, you have given me a 100 marks solution
    just a little comment: In case the user has already entered 2 decimal places and then he want to change the value, the rountine that you wrote will quit his input.

    for example, if a user has entered 100.12
    and he want to change to 101.12
    the sub rountine makes him cannot do that

  9. #9
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: Limit TextBox's decimal places

    "just a little comment: In case the user has already entered 2 decimal places and then he want to change the value, the rountine that you wrote will quit his input.

    for example, if a user has entered 100.12 and he want to change to 101.12
    The sub rountine makes him cannot do that."
    -------------------
    That means you have to write a little code to fix that. BruceG has given you the building blocks. Now, add a little straw and mortar.
    Doctor Ed

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Sep 2008
    Posts
    255

    Re: Limit TextBox's decimal places

    Quote Originally Posted by Code Doc
    "just a little comment: In case the user has already entered 2 decimal places and then he want to change the value, the rountine that you wrote will quit his input.

    for example, if a user has entered 100.12 and he want to change to 101.12
    The sub rountine makes him cannot do that."
    -------------------
    That means you have to write a little code to fix that. BruceG has given you the building blocks. Now, add a little straw and mortar.
    yeah, just add this code:

    If myTxtEdit.SelStart < InStr(1, myTxtEdit.Text, ".") Then
    Exit Sub
    End If

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Sep 2008
    Posts
    255

    Re: Limit TextBox's decimal places

    Quote Originally Posted by Jim Davis
    This is i wrote a sec ago, will limit decimal places, also it support the region settings (will display , or . its can be aset on the regional settings in windows)

    vb Code:
    1. Private Sub Text1_Change()
    2. Static Before As String, sDot As String
    3. Const DecimalPlaces = 3
    4. Dim SStart As Long
    5.  
    6.   If InStr(Format("1,1", "#.0"), ".") > 0 Then sDot = "," Else sDot = "."
    7.  
    8.   If InStr(Text1.Text, sDot) > 0 Then
    9.     Text1.Text = Before
    10.     If Len(Text1.Text) < DecimalPlaces Then Exit Sub
    11.     Text1.SelStart = Len(Text1.Text) - DecimalPlaces
    12.     Exit Sub
    13.   End If
    14.  
    15.   SStart = Text1.SelStart
    16.   Text1.Text = Format(Text1.Text, "###0." + String(DecimalPlaces, "0"))
    17.  
    18.   If SStart > Len(Text1.Text) Then SStart = Len(Text1.Text)
    19.   Text1.SelStart = SStart
    20.  
    21.   Before = Text1.Text
    22. End Sub

    Its quite unfinished But you can start here.
    thank you, i will debug it also. really thank you a lot

  12. #12

  13. #13
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657

    Re: Limit TextBox's decimal places

    Hey lok1234, glad I was able to help. I do see now the little limit my code had, and I am glad you were able to solve it.
    "It's cold gin time again ..."

    Check out my website here.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width