We help IT Professionals succeed at work.

Trying to sum a group of Textboxes in VB.Net

dicecomputers
dicecomputers used Ask the Experts™
on
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

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
HainKurtSr. System Analyst

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

where did you define LaborSelection?
HainKurtSr. System Analyst
Commented:
maybe you should use

var1 = Convert.ToInt16(TextBox1.Text)
-->
var1 = iif(TextBox1.Text="", "0", Convert.ToInt16(TextBox1.Text))

also I guess right syntax foir

TextBox1.Text = Nothing

is

TextBox1.Text is Nothing

or

TextBox1.Text = ""

Commented:
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
HainKurtSr. System Analyst

Commented:
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

Author

Commented:
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

Commented:
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.
High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009
Commented:
Try something like this:
Public Class Labor

    Private Sub Labor_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        TextBox15.Text = LaborSelection.ToString
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim val As Integer
        Dim sum As Integer
        Dim count As Integer = 0
        Dim ctls() As Control
        Dim tb As TextBox
        For i As Integer = 1 To 14
            ctls = Me.Controls.Find("TextBox" & i, True)
            If ctls.Length > 0 AndAlso TypeOf ctls(0) Is TextBox Then
                tb = DirectCast(ctls(0), TextBox)
                If tb.Text.Trim <> "" Then
                    If Integer.TryParse(tb.Text.Trim, val) Then
                        sum = sum + val
                        count = count + 1
                    Else
                        MessageBox.Show(tb.Name & ": " & tb.Text, "Invalid Entry")
                        tb.Focus()
                        tb.SelectAll()
                        Exit Sub
                    End If
                End If
            End If
        Next
        If count > 0 Then
            LaborSelection = LaborSelection + sum
            TextBox15.Text = LaborSelection.ToString
            Me.Close() ' <-- They won't see the new total before the form closes! *(but it will be there when they open it again)
        Else
            MessageBox.Show("You must enter labor hours for at least 1 employee.")
        End If
    End Sub

End Class

Open in new window

HainKurtSr. System Analyst

Commented:
did you try my suggestions @ 36312664

Author

Commented:
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 TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

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

Author

Commented:
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!!