|
-
Dec 15th, 2007, 01:00 PM
#1
Thread Starter
Addicted Member
[2008] Error conversion
Code:
txtbox.Text = txtbox.Text.ToString("C")
Am I doing something wrong?
-
Dec 15th, 2007, 01:11 PM
#2
Re: [2008] Error conversion
What do you expect that to do?
My usual boring signature: Nothing
 
-
Dec 15th, 2007, 04:29 PM
#3
Frenzied Member
Re: [2008] Error conversion
Yes you are doing something wrong, but I don't know the solution:P
-
Dec 15th, 2007, 05:48 PM
#4
Thread Starter
Addicted Member
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...
-
Dec 15th, 2007, 10:01 PM
#5
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:
Private Sub TextBox1_Validating(ByVal sender As Object, _
ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
Dim currency As Decimal
If Decimal.TryParse(Me.TextBox1.Text, currency) Then
'The user has entered a valid value so format it.
Me.TextBox1.Text = currency.ToString("c")
Else
Me.TextBox1.HideSelection = False
Me.TextBox1.SelectAll()
MessageBox.Show("Please enter a valid currency value.", _
"Invalid Data", _
MessageBoxButtons.OK, _
MessageBoxIcon.Error)
Me.TextBox1.HideSelection = True
'Don't let the control lose focus.
e.Cancel = True
End If
End Sub
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|