troubleshooting Question

Trying to sum a group of Textboxes in VB.Net

Avatar of dicecomputers
dicecomputers asked on
Visual Basic.NET
11 Comments2 Solutions387 ViewsLast Modified:
I know this is so simple you will laugh, but for some reason I can't get it to work. I have a form with 14 textboxes to allow the user to input labor hours and a 15th textbox to display the total. I am storing the total in a global variable so when I close the form (but not the program) and then re-open the form, it should display the total of the hours but it just shows 0 every time. What am I doing wrong here?
THANKS!!
Public Class Labor

    Private Sub Labor_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        'Fill in the total hours when this box is RE-opened
        TextBox15.Text = Convert.ToString(LaborSelection)


    End Sub

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

        Try
            'Create variables for all of the TextBoxes and convert them to integers so they can be summed
            Dim var1, var2, var3, var4, var5, var6, var7, var8, var9, var10, var11, var12, var13, var14 As Integer

            var1 = Convert.ToInt16(TextBox1.Text)
            var2 = Convert.ToInt16(TextBox2.Text)
            var3 = Convert.ToInt16(TextBox3.Text)
            var4 = Convert.ToInt16(TextBox4.Text)
            var5 = Convert.ToInt16(TextBox5.Text)
            var6 = Convert.ToInt16(TextBox6.Text)
            var7 = Convert.ToInt16(TextBox7.Text)
            var8 = Convert.ToInt16(TextBox8.Text)
            var9 = Convert.ToInt16(TextBox9.Text)
            var10 = Convert.ToInt16(TextBox10.Text)
            var11 = Convert.ToInt16(TextBox11.Text)
            var12 = Convert.ToInt16(TextBox12.Text)
            var13 = Convert.ToInt16(TextBox13.Text)
            var14 = Convert.ToInt16(TextBox14.Text)

            'Add all the variables together and store in global variable
            LaborSelection = Math.Round(var1 + var2 + var3 + var4 + var5 + var6 + var7 + var8 + var9 + var10 + var11 + var12 + var13 + var14)

            'Display the total in the bottom TextBox
            TextBox15.Text = Convert.ToString(LaborSelection)

        Catch ex As Exception

        End Try

        'Check to make sure some labor hours are entered
        If TextBox1.Text = Nothing And _
            TextBox2.Text = Nothing And _
            TextBox3.Text = Nothing And _
            TextBox4.Text = Nothing And _
            TextBox5.Text = Nothing And _
            TextBox6.Text = Nothing And _
            TextBox7.Text = Nothing And _
            TextBox8.Text = Nothing And _
            TextBox9.Text = Nothing And _
            TextBox10.Text = Nothing And _
            TextBox11.Text = Nothing And _
            TextBox12.Text = Nothing And _
            TextBox13.Text = Nothing And _
            TextBox14.Text = Nothing Then
            MessageBox.Show("You must enter labor hours for at least 1 employee")
        Else
            'Return from wence you came
            Me.Close()
        End If

    End Sub

End Class
ASKER CERTIFIED SOLUTION
Mike Tomlinson
High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Join our community to see this answer!
Unlock 2 Answers and 11 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 11 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros