Hi all! I am making a TimeCard application and I got stucked.. Anyway, the program looks like this: the user inserts date,start hour and finnish hour into 3 textboxes. On the fourth textbox it will show how many hours he has spent. I am using StreamWriter to save the data to a .txt file. My plan is to make this program as simple as possible (no database, access etc.). So, my biggest problem now is how to add all data from textbox4 (the amount of worked hours). I was thinking, to save into a separate txt file the data from textbox4 and then with StreamReader to get it back into a listbox.. but then.. how to add all of them? Im out of ideas.. Help!

Visual Basic 2008

6 Years
Discussion Span
Last Post by codeorder

' Why don't you declare a new variable for the total of all the 4th textbox totals

Dim totalhoursInteger As Integer

totalhoursInteger += the value on 4th textbox

Is this something closer to what you may be trying to accomplish?


If you Append text to a File for each week worked, and save each day's date, hour logged in, hour logged out, and the total hours worked for the day in one line, you should easily access the information and get a sum total for the week. Here is an example on how to combine and split lines:

Public Class Form1

    Private userFile As String = "C:\!vb.net\userFile.txt"

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        TextBox1.Text = Date.Today : Button1.Text = "Save Hours" : Button2.Text = "Total Hours"
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        TextBox4.Text = CInt(TextBox3.Text) - CInt(TextBox2.Text) '// assuming hours are between 1 and 12.
        Dim w As New IO.StreamWriter(userFile, True) '// setting it True will Append text to the file.
        '// write the textbox's content and separate each textbox's value by a character, in this case "~".
        w.WriteLine(TextBox1.Text & "~" & TextBox2.Text & "~" & TextBox3.Text & "~" & TextBox4.Text)
        w.Close() : w.Dispose()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim lines() As String = IO.File.ReadAllLines(userFile)
        Dim totalHours As Integer = 0
        For Each line As String In lines
            Dim array() As String = line.Split("~") '// split the line.
            totalHours += array(3) '// get the 4th item, index of 3.
        MsgBox("Total Hours Worked: " & totalHours) '// show total hours.
    End Sub

End Class
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.