Link to home
Create AccountLog in
Avatar of dicecomputers

asked on

Trying to sum a group of Textboxes in VB.Net

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?
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

            '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")
            'Return from wence you came
        End If

    End Sub

End Class

Open in new window

Avatar of HainKurt
Flag of Canada image

remove "try ... catch" and see the error, or debug it :) what is the issue?

where did you define LaborSelection?
Avatar of HainKurt
Flag of Canada image

Link to home
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
How and where do you define LaborSelection?
1. Inside the Labor class?
This may be causing the issue
2. Inside the parent form?
Make sure it is a WithEvents
maybe you can use private variable

Public Class Labor
public LaborSelection as int = 0

then when you create the class, set LaborSelection

dim c as new Labor
Avatar of dicecomputers


OK, if I take out the try/catch, the textboxes that I enter numbers into are OK, but the first one that is left empty says:

Input string was not in a correct format

Also, the variable is declared in a seperate module (included below). Should it be in a class instead of a module? I don't really understand the difference.
Module PublicVariables

#Region "Invoice Panel"

    'Client pop-up
    Public ClientNameSelection As String
    Public ClientAddressSelection As String
    Public ClientPhoneSelection As String

    'Status pop-up
    Public StatusSelection As String

    'Tax Exempt pop-up
    Public TaxExemptSelection As String

    'Labor pop-up
    Public LaborSelection As Integer

#End Region

End Module

Open in new window

Don't you need a default value for LaborSelection? Set it to zero in the declaration and then search your code for LaborSelection to find out where else you are setting its value.
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
did you try my suggestions @ 36312664
Thanks to both of you. Hain, your solution did work, but Idle minds solution reduced the amount of code needed to accomplish the same result. Unfortunately, some parts of Idle Minds solution were beyond my knowledge so I am doing some research to find out what it all does. I like the fact that it worked and was neat but i'm one of those guys who can't just accept that something works, I need to know HOW it works.
Feel free to ask any questions about the code I posted...  =)
I appreciate the offer, but it comes down to educating myself, it's not just simple questions. I don't know what "DirectCast" is, and I don't understand what "TryParse" is. I am sure those will require more than a simple answer. I have used for a few projects so I am familiar with the basics, but this project is the most involved one I have tried yet. I have a database that I use for my invoices, that I created in Filemaker Pro, and it works great, but I felt like I could do a lot more with it if I re-wrote it in SQL with a VB GUI. It is proving to be very tricky and I am learning a lot more VB as I go, but I love programming so it's fun. I am sure I will be picking your brain some more before it's all done.