Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27355

VS 2010 [RESOLVED] Error checking empty textbox / basic math problem

$
0
0
Hey guys, I've read up on a ton of posts here and finally decided to register. So, this is my first post.

I have just started learning Visual Basic in an online class and am using Visual Basic 2010 Express.

I am NOT simply looking for someone to complete homework assignments for me; I will post my code and simply seek help in solving the errors.

That said, here's my current issue:

When the user clicks the button, I have multiple "If Then" statements to verify that the textboxes are not empty. If they are, prompt the user which box is blank and return to that box. This works just fine EXCEPT for the textboxes that are converted to Integers. Here is the code in question:

Code:

        Dim description As String = txtDescription.Text
        Dim purchased As String = txtPurchased.Text
        Dim cost As Integer = CInt(txtCost.Text)
        Dim life As Integer = CInt(txtLife.Text)
        Dim straightdep As Integer

        'my calculation
        straightdep = CInt((cost - 0) / life)

        'error checking
        If txtDescription.Text = "" Then
            MessageBox.Show("Description missing!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            txtDescription.Focus()
        ElseIf txtPurchased.Text = "" Then
            MessageBox.Show("Year purchased is missing!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            txtPurchased.Focus()
        ElseIf CInt(txtCost.Text) < 0 Then
            MessageBox.Show("Cost is missing!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            txtCost.Focus()
        ElseIf CInt(txtLife.Text) < 0 Then
            MessageBox.Show("Estimated Life is missing!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            txtLife.Focus()
        Else
       
        'populate listbox
        LISTBOX CODE PURPOSELY NOT SHOWN, BUT IT WORKS FINE

When the code is run, and either the "txtCost.Text" or "txtLife.Text" boxes are empty, it crashes and returns this error:
Code:

Conversion from string "" to type 'Integer' is not valid.
I'm sure this is going to be a SIMPLE fix for any of you VB gurus I've seen around here. But, go easy on me, I am learning by lots of trial and error and am just looking for some constructive feedback on what I should be doing different with my code. Thanks in advance to anyone that chimes in!

Viewing all articles
Browse latest Browse all 27355

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>