[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2011-10-29
16
Medium Priority
?
269 Views
Last Modified: 2012-05-12
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.
0
Comment
Question by:jampost
  • 7
  • 6
  • 3
16 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37051615
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
 

Author Comment

by:jampost
ID: 37051661
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
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37051677
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 75

Expert Comment

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

Author Comment

by:jampost
ID: 37051696


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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 37051713
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
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37051728
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
 

Author Comment

by:jampost
ID: 37051746
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
 

Author Comment

by:jampost
ID: 37051769
Kaufmed,

   The result is suppose to appear in Textbox2.  
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 37051780

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
 

Author Comment

by:jampost
ID: 37051799
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 37051801
See my last comment...
0
 

Author Closing Comment

by:jampost
ID: 37051827
This solution worked. What can I say? It totaly resolve everything.    Thank you all for your efforts.  Time to hit the sack.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37051988
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
 

Author Comment

by:jampost
ID: 37052792
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
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37053184
Don't sweat it. The important thing is you got your answer (and hopefully learned something new)     : )
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Make the most of your online learning experience.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Progress

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question