CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1

    Limit decimals in text input box

    Hello,

    Can someone give me an idea on how to limit the number of decimals in a text box??

    I have a simple example app I am working on that has some user input. The input is supposed to be dollar amounts, which may have a decimal added...

    What I want to try to do is limit the amount of decimal places that users can input, IF they input them (decimals are not required).

    For example, users should be able to enter 100 or 100.00, but not 100.0002.

    any ideas? I can limit decimals shown in output, either by rounding, or formatting the output, but I cant limit them in input...

    suggestions??


    Thanks
    jeff

  2. #2
    Join Date
    Jun 2004
    Posts
    7

    Re: Limit decimals in text input box

    Hi.. Use this


    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
    TextBox1.Text = Format(TextBox1.Text, "Standard")
    End Sub


    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
    If Asc(e.KeyChar) < 48 Or Asc(e.KeyChar) > 57 Then
    e.Handled = True
    End If
    End Sub

  3. #3

    Re: Limit decimals in text input box

    thanks, but that doesn't quite do it...

    limiting the keypress event is fine, I already have a sub to do that that works well... and I tried formatting the textbox.text element as you suggested, but that formats as soon as any text changes at all...

    for instance, lets say that textbox.text is blank. I, the user, then want to enter 100.

    As soon as I press 1, the format command formats the new text string as 1.00 and places the cursor in front of the 1. SO... if I wanted to type in 156.65, it would actually end up like this:

    56.651.00, or at least this: 5665100 instead of 156.65.

    but thanks for trying though. I had actually thought of using format, and that still may be the correct path.. perhaps a keyup event that would place the cursor behind the last entered char??

    Oh well, its an educational experience.

    thanks
    Jeff

  4. #4
    Join Date
    Jul 2003
    Posts
    135

    Re: Limit decimals in text input box

    Woudl this solve your problem?:
    Code:
        Private Sub TextBox1_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.LostFocus
            TextBox1.Text = Format(Math.Round(CDec(TextBox1.Text), 2), "Currency")
        End Sub
    It would round to two decimal places and format it as currency when the user moved to another box. This way it doesnt matter if they type in the decimal or not.

    Allan.

  5. #5
    Join Date
    Jul 2003
    Posts
    135

    Re: Limit decimals in text input box

    Actually you may want to throw in the VAL() in case they do enter text by accident it would give them a casting from string to decimal error:

    Format(Math.Round(CDec(Val(TextBox1.Text)), 2), "Currency")

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

Featured