locked
how to format textbox to be like this format 0.00 RRS feed

  • Question

  • I want a simple textbox that automatically formats itself for money with this format 0.00
    first digit: i can put on it any number of digits like this: 1324654879.00
    second and third digits after the decimal put on them only 2 digit like this: 2136459874.12

    Wednesday, June 1, 2011 3:31 PM

Answers

  • Try using the format code "f2". That's a fixed number set to two decimal places. Also, "n2" will get you commas. There's also one for currency but I don't remember now what it is.

    Oh yes - it's "C" or "c". Have a look at all of them here:

    http://msdn.microsoft.com/en-us/library/y006s0cz(v=VS.71).aspx

    • Edited by Frank L. Smith Wednesday, June 1, 2011 3:39 PM Additional Information...
    • Proposed as answer by Mike Feng Friday, June 3, 2011 3:49 AM
    • Marked as answer by Mike Feng Tuesday, June 21, 2011 9:25 AM
    Wednesday, June 1, 2011 3:37 PM
  • Have a look at this and let me know if you don't understand what I have here or how it works:

     


      Private Sub TextBox1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Leave
    
        ' This is the "leave" event of the textbox. Don't try to figure out what they might
        ' be typing - let them finish typing it, then look to try to format it if it's a
        ' legit number.
    
        Dim userEntry As Decimal = 0
    
        If Not Decimal.TryParse(TextBox1.Text, userEntry) Then
          MessageBox.Show("You must type a number in this textbox.", _
                  "Illegal Characters", _
                  MessageBoxButtons.OK, MessageBoxIcon.Warning)
    
          TextBox1.Focus()
          TextBox1.SelectAll()
        Else
          TextBox1.Text = userEntry.ToString("C")
        End If
    
      End Sub
    


    I hope it helps. :)

    • Proposed as answer by Mike Feng Friday, June 3, 2011 3:50 AM
    • Marked as answer by Mike Feng Tuesday, June 21, 2011 9:26 AM
    Wednesday, June 1, 2011 3:53 PM
  • For that is not the textbox but the maskedtextbox

     


    Success
    Cor
    • Marked as answer by Mike Feng Tuesday, June 21, 2011 9:26 AM
    Wednesday, June 1, 2011 4:50 PM

All replies

  • Try using the format code "f2". That's a fixed number set to two decimal places. Also, "n2" will get you commas. There's also one for currency but I don't remember now what it is.

    Oh yes - it's "C" or "c". Have a look at all of them here:

    http://msdn.microsoft.com/en-us/library/y006s0cz(v=VS.71).aspx

    • Edited by Frank L. Smith Wednesday, June 1, 2011 3:39 PM Additional Information...
    • Proposed as answer by Mike Feng Friday, June 3, 2011 3:49 AM
    • Marked as answer by Mike Feng Tuesday, June 21, 2011 9:25 AM
    Wednesday, June 1, 2011 3:37 PM
  • thanks but i need code
    Wednesday, June 1, 2011 3:44 PM
  • thanks but i need code

    Ok, but let me ask you this first: Have you considered what might happen if someone types in, say, the word "six"? I don't mind setting it up for you but I'd like to see where you are in your thinking.
    Wednesday, June 1, 2011 3:46 PM
  • Have a look at this and let me know if you don't understand what I have here or how it works:

     


      Private Sub TextBox1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Leave
    
        ' This is the "leave" event of the textbox. Don't try to figure out what they might
        ' be typing - let them finish typing it, then look to try to format it if it's a
        ' legit number.
    
        Dim userEntry As Decimal = 0
    
        If Not Decimal.TryParse(TextBox1.Text, userEntry) Then
          MessageBox.Show("You must type a number in this textbox.", _
                  "Illegal Characters", _
                  MessageBoxButtons.OK, MessageBoxIcon.Warning)
    
          TextBox1.Focus()
          TextBox1.SelectAll()
        Else
          TextBox1.Text = userEntry.ToString("C")
        End If
    
      End Sub
    


    I hope it helps. :)

    • Proposed as answer by Mike Feng Friday, June 3, 2011 3:50 AM
    • Marked as answer by Mike Feng Tuesday, June 21, 2011 9:26 AM
    Wednesday, June 1, 2011 3:53 PM
  • I took this in my mind by lock the textbox that it accepts only numbers

    Wednesday, June 1, 2011 3:55 PM
  • I took this in my mind by lock the textbox that it accepts only numbers


    I don't know what you mean by "locking" the textbox unless maybe you're talking about a third-party textbox control? Dev Express's allows you to set it up that way, but either way, my thinking is that if the idiot is dumb enough to type text into it, I let them.

    I don't let them get away with it, but I let them type it if they want to! :-o

    Wednesday, June 1, 2011 4:00 PM
  • i try your code but i change from "c" to "e"

    to be : 0.000

    to write any number in the first digit but i need to edit in the 3 digit after decimal

    and i need to prevent the user to delete or back space the digit 0.000 i need it fixed and user put values not clear digit and write the value

     

    Wednesday, June 1, 2011 4:19 PM
  • i try your code but i change from "c" to "e"

    to be : 0.000

    to write any number in the first digit but i need to edit in the 3 digit after decimal

    and i need to prevent the user to delete or back space the digit 0.000 i need it fixed and user put values not clear digit and write the value

     


    If you want three digits use either "f3" for fixed, or "n3" to get commas.

    As far as controlling the keys, yes there is a way to do it - but I don't agree with the idea of it, so I've never set it up, but there's examples around the forum for that sort of thing.

    Like I said, I let them do whatever they want to do, then I "validate" it once they leave the textbox. For example, let's say they did delete everything and I want to set it to show 0.000:

     


      Private Sub TextBox1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Leave
    
        ' This is the "leave" event of the textbox. Don't try to figure out what they might
        ' be typing - let them finish typing it, then look to try to format it if it's a
        ' legit number.
    
        Dim userEntry As Decimal = 0
    
        If TextBox1.Text = "" Then
          userEntry = 0
          TextBox1.Text = userEntry.ToString("f3")
        Else
          If Not Decimal.TryParse(TextBox1.Text, userEntry) Then
            MessageBox.Show("You must type a number in this textbox.", _
                    "Illegal Characters", _
                    MessageBoxButtons.OK, MessageBoxIcon.Warning)
    
            TextBox1.Focus()
            TextBox1.SelectAll()
          Else
            TextBox1.Text = userEntry.ToString("f3")
          End If
        End If
    
      End Sub
    


    So after they leave the textbox, I look to see if's string.empty and if it is, I force it to be 0 shown as 0.000.

    Wednesday, June 1, 2011 4:26 PM
  • thanks but i need it fixed

    ex: textbox format like this 0.00

    i set value 12500.00

    when i set the value the format be clear i don't need to be clear i need it fixed 0.00

    i put value  :  1254    this part .00  fixed not clear but can edit

    Wednesday, June 1, 2011 4:45 PM
  • thanks but i need it fixed

    ex: textbox format like this 0.00

    i set value 12500.00

    when i set the value the format be clear i don't need to be clear i need it fixed 0.00

    i put value  :  1254    this part .00  fixed not clear but can edit


    Well, sorry I could't help then. Maybe someone else here will jump in and answer it.

    Good luck with your project. :)

    Wednesday, June 1, 2011 4:47 PM
  • For that is not the textbox but the maskedtextbox

     


    Success
    Cor
    • Marked as answer by Mike Feng Tuesday, June 21, 2011 9:26 AM
    Wednesday, June 1, 2011 4:50 PM
  • Try following line:

    TextBox.Text = Format(Val(TextBox.Text),".00")

    May be this is your answer.

     


    RJ
    Wednesday, June 1, 2011 5:48 PM
  • How do you want the user to enter the data? How should they edit the number while they're inputting it? Should anything be displayed before a number is entered? If they type 0.149, should it be rounded to 0.15 or just ignore any input after two decimal places have been entered? What if they paste a number in?

    It would be simpler to let the user enter anything and validate/format it when the TextBox loses focus (as Frank indicated).

    --
    Andrew

    Wednesday, June 1, 2011 7:06 PM
  • How do you want the user to enter the data? How should they edit the number while they're inputting it? Should anything be displayed before a number is entered? If they type 0.149, should it be rounded to 0.15 or just ignore any input after two decimal places have been entered? What if they paste a number in?

    It would be simpler to let the user enter anything and validate/format it when the TextBox loses focus (as Frank indicated).

    --
    Andrew


    Hi Andrew - it's been a while since I've been around. I hope you are well. :)

    I agree that it's easier and, in my opinion, more flexible to let the user type whatever they want, then when the move focus, write code to do whatever you want it to do. Validate that it's a number for instance, or if it's negative change the format to be bold and red or ... the list goes on.

    I honestly never did fully understand what the OP wanted. It seemed to change over time. :-o

    I hope you have better success. :)

     


    @Raj -->

    You might want to try this for your own edification. I'm of the belief to never use "Val" because it doesn't work quite the way you might think it does. Let me suggest this just so you can see what I mean:

    Set up a textbox like you suggested. Now type in the following:

    123 1st Street

    Did that work like you wanted it to? Next try typing in this:

    Main Street

    See what I mean? Just my opinion of course, but I think Val is something that shouldn't even be considered.

    Wednesday, June 1, 2011 7:29 PM
  • Hi Frank, I can't understand why are you using strings in place of numbers. The "Format" function that I have suggested previously works perfectly on any number if you put the same line in the "LostFocus" Property of the text box.

    If you doubt that a user may enter a string then what would happen, then the answer that comes is "0.00", OR what you told 123 1st Street, then the answer will be 1231.00.

    Another thing is that if someone has provided the text box to be used for numeric values only then the program should have the check on the text box that any alphabet in the text box will be an invalid input by the user and it should intimate the same to user using some message box.

     


    RJ
    Thursday, June 2, 2011 11:25 AM
  • Hi Frank, I can't understand why are you using strings in place of numbers. The "Format" function that I have suggested previously works perfectly on any number if you put the same line in the "LostFocus" Property of the text box.

    If you doubt that a user may enter a string then what would happen, then the answer that comes is "0.00", OR what you told 123 1st Street, then the answer will be 1231.00.

    Another thing is that if someone has provided the text box to be used for numeric values only then the program should have the check on the text box that any alphabet in the text box will be an invalid input by the user and it should intimate the same to user using some message box.

     


    RJ


    For all the reasons you just stated - that's why I think it's a bad idea.

    To each their own. :)

    Thursday, June 2, 2011 2:01 PM