Avatar of dicecomputers
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?
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

Open in new window

Visual Basic.NET

Avatar of undefined
Last Comment
dicecomputers

8/22/2022 - Mon
HainKurt

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

where did you define LaborSelection?
SOLUTION
HainKurt

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
nepaluz

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
HainKurt

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
c.LaborSelection=LaborSelection
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
dicecomputers

ASKER
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

nepaluz

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.
ASKER CERTIFIED SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
HainKurt

did you try my suggestions @ 36312664
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
dicecomputers

ASKER
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.
THANKS AGAIN!!!!!
Mike Tomlinson

Feel free to ask any questions about the code I posted...  =)
dicecomputers

ASKER
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 VB.net 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.
THANKS AGAIN!!
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes