Results 1 to 6 of 6

Thread: [RESOLVED] Format as currency

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2009
    Location
    Townsville, Qld, Australia
    Posts
    135

    Resolved [RESOLVED] Format as currency

    Please excuse a beginner in VB Net with what is probably a really dumb question.

    I am trying to write some code that will format the text in a text-box as currency, so that if the user enters "6", the text-box will show "$6.00" after the user exits the text-box. (I want to allow for all the variations of users entering 6 or 6.00 or $6 or $6.00.)

    I've got:
    HTML Code:
       Private Sub txt_nett_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt_nett.TextChanged
            txt_nett = FormatCurrency(txt_nett)
        End Sub
    and an error message that says: "Value of type 'String' cannot be converted to 'System.Windows.Forms.TextBox'.

    The frustrating thing is that I could do this in a trice in VBA (with which I am much more familiar)!

    Thank you

  2. #2
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Format as currency

    txt_nett is a textbox. How can you format a textbox? Perhaps you need the text in this textbox... if so, then use its txt_nett.Text property.

    VBA and VB6 permit such sloppiness and try to 'think for/instead of the coder' VB.Net is more strict and does not take actions on its own. The compiler does EXACTLY what's been told in the code. And you're trying to use an object that points to a usercontrol like plain text.

  3. #3
    Hyperactive Member
    Join Date
    Mar 2012
    Posts
    311

    Re: Format as currency

    Your error doesn't have to do with the FormatCurrency function. The problem is something that a lot of VBA / VB 6.0 crossovers fall into... In VB.Net, the Text property of controls is no longer the default property. So you have to address this property specifically. Namely:

    Code:
       Private Sub txt_nett_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt_nett.TextChanged
            txt_nett.Text = FormatCurrency(txt_nett.Text)
        End Sub

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jun 2009
    Location
    Townsville, Qld, Australia
    Posts
    135

    Re: Format as currency

    Thank you both. I'll need to fiddle a bit more, as the TextChanged event (which is the default VB.Net provides) isn't quite what I want. I need something like AfterUpdate or Exit.

  5. #5
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: Format as currency

    I'm curious to why he's using the FormatCurrency() function in the first place.

    If you have a numeric variable (he would need to parse the current text in the TB to a numeric variable & if it passes convert it to currency) you can display it as currency using .ToString("c")

    Here's an example:
    vb Code:
    1. Private Sub txt_nett_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt_nett.TextChanged
    2.     Dim value As Decimal
    3.     If Decimal.TryParse(txt_nett.Text.Trim(), value) Then
    4.         txt_nett.Text = value.ToString("c")
    5.     End If
    6. End Sub
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jun 2009
    Location
    Townsville, Qld, Australia
    Posts
    135

    Re: Format as currency

    The "Leave" event does the trick. Thank you all.
    Last edited by Resource Dragon; Apr 16th, 2012 at 08:24 AM. Reason: Changed "both" to "all" as third person replied as I was replying.

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