Results 1 to 7 of 7

Thread: Help!! TextBox input value Changes

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2005
    Posts
    5

    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!

  2. #2
    New Member
    Join Date
    Dec 2005
    Posts
    5

    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

  3. #3
    Fanatic Member cpatzer's Avatar
    Join Date
    Sep 2004
    Posts
    537

    Re: Help!! TextBox input value Changes

    Try doing this instead for your format functionality:

    VB Code:
    1. Private Function NumberPad(ByVal Str As String) As String
    2.         Dim MyPos As Double = Double.Parse(Str)
    3.         Return MyPos.ToString("#,###,###,##0.00;")
    4.     End Function
    In life you can be sure of only two things... death and taxes. I'll take death.

  4. #4
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: Help!! TextBox input value Changes

    Works for me using this code, instead of val...
    VB Code:
    1. Dim value1 As Decimal = Convert.ToDecimal(TextBox1.Text)
    2. 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.

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

    Re: Help!! TextBox input value Changes

    How about:
    VB Code:
    1. 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:
    1. Private Sub TextBox1_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
    2.         Dim value As Double
    3.  
    4.         If Double.TryParse(Me.TextBox1.Text, Globalization.NumberStyles.Number, Nothing, value) Then
    5.             Me.TextBox1.Text = value.ToString("n2")
    6.         Else
    7.             e.Cancel = True
    8.             MessageBox.Show("Please enter a numerical value.")
    9.         End If
    10.     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

  6. #6

    Thread Starter
    New Member
    Join Date
    Nov 2005
    Posts
    5

    Thumbs up 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!.

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

    Re: Help!! TextBox input value Changes

    Cool. Don't forget to resolve your thread from the Thread Tools menu.
    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