What do you use for inputting decimals since text boxes cause errors?

Sample code in a Windows form:

Public Class EnterPayroll

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim Tb1 As Decimal = TextBox1.Text
        Dim Tb2 As Decimal = TextBox2.Text

        If (Tb1 >= 400.0) And (Tb1 <= 425.0) Then
            Tb2 = 0.33 * Tb1
        End If
      
    End Sub

      I am testing some code for payroll calculations and apparently string will not convert to decimal in a text box.
jampostGeneral MaanagerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
This does not include error checking, but you should be able to use:

Dim Tb1 As Decimal = Convert.ToDecimal(TextBox1.Text)
Dim Tb2 As Decimal = Convert.ToDecimal(TextBox2.Text)

Open in new window

0
jampostGeneral MaanagerAuthor Commented:
Kaufmed,

I tried that and received an error.

Input string was not in a correct format.
I inputted 412 and tried 412.00  I don't know what format is needed.                   


The following was in the immediate window after the error:      
A first chance exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll
A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
0
käµfm³d 👽Commented:
Interesting. If you are indeed putting in valid decimal values, then you shouldn't receive that exception. Are you certain these are the lines causing the exceptions.

As a side note, you can wrap those calls in a try/catch for FormatException; it won't specifically help you with this issue, but it will keep your code from bombing.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

käµfm³d 👽Commented:
Actually, in reviewing your post, how exactly do you receive the error? Generally, "first-chance" exceptions aren't ones you would worry about.
0
jampostGeneral MaanagerAuthor Commented:


Format exception was unhandled   (dialog box)    with the error message --   This was pointing to...

Dim Tb2 As Decimal = Convert.ToDecimal(TextBox2.Text)


~~~~~~~~~~~~
   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim Tb1 As Decimal = Convert.ToDecimal(TextBox1.Text)
        Dim Tb2 As Decimal = Convert.ToDecimal(TextBox2.Text)

        If (Tb1 >= 400.0) And (Tb1 <= 425.0) Then
            Tb2 = 0.33 * Tb1
        End If

    End Sub

~~~~~~~~~~~~~~~~~
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
Why are you parsing TextBox2 into Tb2 but then you never use it?

You are simply overwriting Tb2 without ever using the original value...

At any rate, see if the TryParse() methods help out...it's a similar approach to the try/catch proposed by kaufmed:
Dim TB1 As Double
If Double.TryParse(TextBox1.Text, TB1) Then
    Dim TB2 As Double
    If Double.TryParse(TextBox2.Text, TB2) Then

        ' ... do something with TB1 and TB2 in here ...

    Else
        MessageBox.Show("Invalid Value in TextBox2")
    End If
Else
    MessageBox.Show("Invalid Value in TextBox1")
End If

Open in new window

0
käµfm³d 👽Commented:
You will get that exception if you don't enter anything into TextBox2, since you are trying to convert the text from it as well.
0
jampostGeneral MaanagerAuthor Commented:
Idle Mind

I got all of the bugs except one  out after entering the new code. I have a syntax error now. I put *** around it.



  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim TB1 As Double
       
        If Double.TryParse(TextBox1.Text, TB1) Then
            Dim TB2 As Double

            If Double.TryParse(TextBox2.Text, TB2) Then


           ***(TB1 >= 400.0)*** And (TB1 <= 425.0) Then
                TB2 = 0.33 * TB1

            Else
                MessageBox.Show("Invalid Value in TextBox2")
            End If
        Else
            MessageBox.Show("Invalid Value in TextBox1")
        End If


    End Sub
0
jampostGeneral MaanagerAuthor Commented:
Kaufmed,

   The result is suppose to appear in Textbox2.  
0
Mike TomlinsonMiddle School Assistant TeacherCommented:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim TB1 As Double        
    If Double.TryParse(TextBox1.Text, TB1) Then
        If (TB1 >= 400.0) And (TB1 <= 425.0) Then
            Dim TB2 As Double = 0.33 * TB1
            TextBox2.Text = TB2.ToString() ' <-- pass in a format to ToString() if desired
        Else
            MessageBox.Show("Value must be >= 400.0 and <= 425.0")
        End If
    Else
        MessageBox.Show("Invalid Value in TextBox1")
    End If
End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jampostGeneral MaanagerAuthor Commented:
I made a few more changes  --  now error free during run time  --  but nothing happens in TB2.


Public Class EnterPayroll

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim TB1 As Double
       
        If Double.TryParse(TextBox1.Text, TB1) Then
            Dim TB2 As Double

            If Double.TryParse(TextBox2.Text, TB2) Then
            End If


            If (TB1 >= 400.0) And (TB1 <= 425.0) Then
                TB2 = 0.33 * TB1



            Else
                MessageBox.Show("Invalid Value in TextBox2")
            End If
        Else
            MessageBox.Show("Invalid Value in TextBox1")
        End If


    End Sub
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
See my last comment...
0
jampostGeneral MaanagerAuthor Commented:
This solution worked. What can I say? It totaly resolve everything.    Thank you all for your efforts.  Time to hit the sack.
0
käµfm³d 👽Commented:
The result is suppose to appear in Textbox2.  
Understood. But understand that you are attempting to convert the value of an empty TextBox with the line:

Dim Tb2 As Decimal = Convert.ToDecimal(TextBox2.Text)

Open in new window


I only modified your original code based on what you posted--and it appeared that you wanted the value of TextBox2 converted.

I realize you already accpeted Idle_Mind's answer, and that is fine, but I just want to make sure you understand why the error is occurring. If you try to convert an empty string (which TextBox2 is since it's your result box), then you will get that error.
0
jampostGeneral MaanagerAuthor Commented:
Kaufmed,

  Thank you for that explanation. I thought you misunderstood my algorythmm. I am not advanced enough to pick up on things like that immediately. It looks like Idlle Mind's 12:01 AM post made changes  that might have included what you are saying. If I would have known that at the time I would have split the points. I try my best to make the best decision on the points because I understand the importance of getting that part right.   Thank again  --  Joe
0
käµfm³d 👽Commented:
Don't sweat it. The important thing is you got your answer (and hopefully learned something new)     : )
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.