Results 1 to 5 of 5

Thread: [2008] Error conversion

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2001
    Posts
    134

    [2008] Error conversion

    Code:
            txtbox.Text = txtbox.Text.ToString("C")
    Am I doing something wrong?

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: [2008] Error conversion

    What do you expect that to do?
    My usual boring signature: Nothing

  3. #3
    Frenzied Member Icyculyr's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    1,934

    Re: [2008] Error conversion

    Yes you are doing something wrong, but I don't know the solution:P

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Dec 2001
    Posts
    134

    Re: [2008] Error conversion

    sorry guys... itwasn't very clear...
    What I want to do is a textbox that is formated to currency... as soon as the user input numbers, it will show them like currency...

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] Error conversion

    The Text property of a TextBox is type String. There is no overload of the String.ToString method that takes a format string as a parameter. Only numerical types can do that. You format a NUMBER as currency, not a string. What would that code do if the TextBox contained "Hello World"? How would that get formatted as currency.

    What you should be doing is handling the Validating event. Try to parse the Text to a number. If it succeeds then format that number as a currency string, otherwise tell the user to enter a valid value, e.g.
    vb.net Code:
    1. Private Sub TextBox1_Validating(ByVal sender As Object, _
    2.                                 ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
    3.     Dim currency As Decimal
    4.  
    5.     If Decimal.TryParse(Me.TextBox1.Text, currency) Then
    6.         'The user has entered a valid value so format it.
    7.         Me.TextBox1.Text = currency.ToString("c")
    8.     Else
    9.         Me.TextBox1.HideSelection = False
    10.         Me.TextBox1.SelectAll()
    11.  
    12.         MessageBox.Show("Please enter a valid currency value.", _
    13.                         "Invalid Data", _
    14.                         MessageBoxButtons.OK, _
    15.                         MessageBoxIcon.Error)
    16.  
    17.         Me.TextBox1.HideSelection = True
    18.  
    19.         'Don't let the control lose focus.
    20.         e.Cancel = True
    21.     End If
    22. End Sub
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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