Click here to Skip to main content
15,884,821 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi, Firstly, I am fairly new to VB.NET and still in the learning curve.

I am working on an application for work and we have 10 text boxes, 5 that are for Hours and 5 for Indirect Hours, I can enter text in to them all fine and they update label text no problem. The problem is when I am typing in the text boxes, the 'Net Hour' text box is always 1 number behind, for example, if I type 1, then 4, it will then show 1 in the Net Hour text box when I type 4. I am getting really confused by this.

Here is my code:
VB
Private Sub txtClockHour_TextChanged(sender As Object, e As EventArgs) Handles txtClockHour.TextChanged, txtIndirectHour.TextChanged

    '   This changes the Clocked Hours text box to white when text changed, for errors / missing information.

    Dim TotNum1 As Integer
    Dim TotNum2 As Integer
    Dim TotNum3 As Integer
    Dim TotNum4 As Integer
    Dim TotNum5 As Integer

    TotNum1 = Integer.Parse(lblTot1.Text)
    TotNum2 = Integer.Parse(lblTot2.Text)
    TotNum3 = Integer.Parse(lblTot3.Text)
    TotNum4 = Integer.Parse(lblTot4.Text)
    TotNum5 = Integer.Parse(lblTot5.Text)

    txtClockHour.BackColor = Color.White

    '   This below calculates the Clocked Hours multiplied by the Indirect Hours text boxes, if no Indirect Hours, it makes the Net Hour box equal the same as Clocked Hours.

    If txtClockHour.Text > "" And txtIndirectHour.Text > "" Then
        lblTot1.Text = (CDec(txtClockHour.Text)) - (CDec(txtIndirectHour.Text))
        txtNetHour.Text = (CInt(TotNum1)) + (CInt(TotNum2)) + (CInt(TotNum3)) + (CInt(TotNum4)) + (CInt(TotNum5))
    Else
        txtNetHour.Text = (CInt(TotNum1)) + (CInt(TotNum2)) + (CInt(TotNum3)) + (CInt(TotNum4)) + (CInt(TotNum5))
    End If

End Sub


Please help. Thanks.
Posted
Comments
frostcox 14-Mar-13 18:59pm    
Hey you need to change the condition in your if statement to If txtClockHour.Text.Trim() <> String.Empty And txtClockHour.Text.Trim() <> String.Empty Then .... If you need any more help I'm happy to help.

You can't do that in this way:
VB
If txtClockHour.Text > "" And txtIndirectHour.Text > "" Then

Read more about: Operators and Expressions in Visual Basic[^]

Check the length of string, using String.Length property[^]:
VB
If txtClockHour.Text.Length > 0 And txtIndirectHour.Text.Length > 0 Then


I suggest you to create class. How to: create class in VB.NET[^].
 
Share this answer
 
Hi,

Thank you both for you help, I managed to do this using MacieJ Los answer by creating a 'sub' instead of a class and calling the sub, like below. Thank you.

Sub
VB
Public Sub TotNum1()

    Dim TotNum1 As Integer
    Dim TotNum2 As Integer
    Dim TotNum3 As Integer
    Dim TotNum4 As Integer
    Dim TotNum5 As Integer

    TotNum1 = Integer.Parse(lblTot1.Text)
    TotNum2 = Integer.Parse(lblTot2.Text)
    TotNum3 = Integer.Parse(lblTot3.Text)
    TotNum4 = Integer.Parse(lblTot4.Text)
    TotNum5 = Integer.Parse(lblTot5.Text)

    txtNetHour.Text = (CInt(TotNum1)) + (CInt(TotNum2)) + (CInt(TotNum3)) + (CInt(TotNum4)) + (CInt(TotNum5))

End Sub


TextChanged event
VB
Private Sub txtClockHour_TextChanged(sender As Object, e As EventArgs) Handles txtClockHour.TextChanged, txtIndirectHour.TextChanged

    '   This changes the Clocked Hours text box to white when text changed, for errors / missing information.

    txtClockHour.BackColor = Color.White

    '   This below calculates the Clocked Hours multiplied by the Indirect Hours text boxes, if no Indirect Hours, it makes the Net Hour box equal the same as Clocked Hours.

    If txtClockHour.Text.Length > 0 And txtIndirectHour.Text.Length > 0 Then
        lblTot1.Text = (CDec(txtClockHour.Text)) - (CDec(txtIndirectHour.Text))
        Call TotNum1()
    Else
        Call TotNum1()
    End If

End Sub
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900