|
-
Dec 20th, 2005, 02:54 PM
#1
Thread Starter
New Member
Help!! TextBox input value Changes
I have a formatted textbox that numeric values are entered into. After clicking away from the textbox and then click back into the textbox, the input value changes. ex: 23,000.00 changes to 23.00
Sample of code:
Private Sub TextBox1_leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Leave
Dim value1 As Decimal = Val(TextBox1.Text)
TextBox1.Text = Format(value1, "##,##0.00")
display_results()
End Sub
By removing the format, this behavior does not happen.
What am I doing wrong?
All help is appreciated!
-
Dec 20th, 2005, 03:05 PM
#2
New Member
Re: Help!! TextBox input value Changes
You will need the format there to format the value. You can also use the Round function, which will do the same thing. There is no default option for decimal places. They all have to be manipulated as you've done above. Hope this helps
-
Dec 20th, 2005, 03:05 PM
#3
Fanatic Member
Re: Help!! TextBox input value Changes
Try doing this instead for your format functionality:
VB Code:
Private Function NumberPad(ByVal Str As String) As String
Dim MyPos As Double = Double.Parse(Str)
Return MyPos.ToString("#,###,###,##0.00;")
End Function
In life you can be sure of only two things... death and taxes. I'll take death.
-
Dec 20th, 2005, 03:07 PM
#4
Re: Help!! TextBox input value Changes
Works for me using this code, instead of val...
VB Code:
Dim value1 As Decimal = Convert.ToDecimal(TextBox1.Text)
TextBox1.Text = Format(value1, "##,##0.00")
If it still does that afterwards, then look in your display results code..
of course, this would raise an error of the text inside of the textbox is not numeric, so you may want to add code to use .IsNumeric to check if it is a numerical value, or some other sort of verification..
Last edited by gigemboy; Dec 20th, 2005 at 03:11 PM.
-
Dec 20th, 2005, 06:33 PM
#5
Re: Help!! TextBox input value Changes
How about:
VB Code:
Me.TextBox1.Text = Convert.ToDecimal(Me.TextBox1.Text).ToString("n2")
Of course, this does not make any guarantees about what the user has entered into the TextBox in the first place. Val() will not throw an exception if the user enters invalid data but it is not necessarily the way to validate as the user may not notice that they have entered an invalid character and carry on while Val has caused the value in the TextBox to be made zero. You should really perform proper validation on the TextBox by either preventing improper input in the first place or else testing for a valid value before formatting. You could do something like this:
VB Code:
Private Sub TextBox1_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
Dim value As Double
If Double.TryParse(Me.TextBox1.Text, Globalization.NumberStyles.Number, Nothing, value) Then
Me.TextBox1.Text = value.ToString("n2")
Else
e.Cancel = True
MessageBox.Show("Please enter a numerical value.")
End If
End Sub
-
Dec 21st, 2005, 09:29 AM
#6
Thread Starter
New Member
Re: Help!! TextBox input value Changes
I want to thank you all for your instructive inputs. My problem is resolved.
The problem was not stripping commas from the input to the format statement and therefore the result returned from val(TextBox1.text) was truncated to the first comma position. Example: 123,456,00 became 123.00
The response from this forum is awesome and an invaluable resource for ALL developers...from the novice to the expert.
Again, Thanks for your help!.
-
Dec 21st, 2005, 08:29 PM
#7
Re: Help!! TextBox input value Changes
Cool. Don't forget to resolve your thread from the Thread Tools menu.
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
|