Am I doing something wrong?Code:txtbox.Text = txtbox.Text.ToString("C")
Printable View
Am I doing something wrong?Code:txtbox.Text = txtbox.Text.ToString("C")
What do you expect that to do?
Yes you are doing something wrong, but I don't know the solution:P
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...
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