Need Help with Formatting Currency in Textbox


  • I searched and searched and yet i can't find anything to really explain to me what to do and how to fix my problem. What i'm trying to do is the basic formatting a textbox as currency and every different way i have tried keeps messing up... ways i've tried is setting the textbox.tag to $#,##0.00 and also $#,###.## and also i have tried actually reformating the textbox's text with this coding...

    textbox1.Text = Microsoft.VisualBasic.Format(CSng(textbox1.Text), "$#,###,##0.00")

    textbox1.Text = Microsoft.VisualBasic.Format(CSng(textbox1.Text), "$#,###,###.##")

    also tried using this i found on a thread, which doesn't work i've tried it and tried it and i can't put anything for DT cause i have NO IDEA what "dt" is suppose to mean or stand for...

    Dim WithEvents bindobject As Binding
    in you load statement

    bindobject = New Binding("text", dt, "CostField")

    dim price as new textboxt 'create the text boxt (or do it from designor)


    ' now create the sub for that binding change

    Private Sub day1bind_Format(ByVal sender As Object, ByVal e As System.Windows.Forms.ConvertEventArgs) Handles bindobject.Format
    If e.Value Is DBNull.Value Then

    e.Value = Format(e.Value, "c")
    End If
    End Sub

    Everytime i tried using the codes listed above it just keeps messing up... it keeps adding 0's where they aren't suppose to be... can anyone help please?
    Tuesday, June 05, 2007 2:47 PM


All replies

  • Have you tried this?


    Code Snippet

    TextBox1.Text = String.Format("{0:c}", myVal)


    Tuesday, June 05, 2007 2:53 PM
  • yea i just tried that and it doesn't work.... also could u specific what u was meaning by myVal?
    Tuesday, June 05, 2007 3:07 PM
  • myVal denotes the decimal value that you want to assign to your textbox.


    For example:


    Code Snippet

    dim myVal as decimal = 43.45

    TextBox1.Text = String.Format("{0:c}", myVal)


    Tuesday, June 05, 2007 3:11 PM
  • no i can't use that because the coding has to be able to format the textbox "On the Fly" as they type in the numbers it has to format the numbers as they type then in. I appologize if i had forgotten that this had to be done. I can't use that coding because i can't set myVal = textbox1.text.. it doesn't format it every time they enter a new number... even if i do put it in keypress/keydown/keyup/textchanged ... does nothing
    Tuesday, June 05, 2007 3:27 PM
  • In that case, you could  used the MaskedTextBox control (.Net 2.0) You can set the mask property of the control to define the input you require.




    From the above reference :




    "$999,999.00"  - A currency value in the range of 0 to 999999. The currency, thousandth, and decimal characters will be replaced at run time with their culture-specific equivalents."










    Tuesday, June 05, 2007 3:46 PM
  • I'm still having trouble with this and hope someone can help.

    I need to enter different numbers in multiple textboxes.   The number are dollar currency and lets say they can be up to $999,999,999.99.  I've tried the masked textbox with mask $999,999,999.00 but the still can't get it to work right.

    When the TextMaskFormat property is set to IncludeLiterals then when trying to use the number from the textbox in a math statement I get an unhandled exception in the code even though I use a Convert.ToDouble(textbox.text).  If I change the TextMaskFormat to ExcludePromptandLiterals then the numbers show up in the textbox in a very strange unacceptable way: Like the default 0.00 shows up in the textbox as $0.00,____,____.__


    Any other suggestions of how to diplay curency in a masked textbox but also use these number in mathematical equations?



    Friday, June 27, 2008 2:38 AM