Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am a beginner in programming.
Can someone help me with my problem?

I have 3 labels:
1st shows the downtime data (ex: 01:17:05)
2nd shows the idle time data (ex:00:05:00)
3rd is the TotalTime data (ex: 01:23:05)

Can someone help me with what program I can use to save it during the program close and still remain after I open it? Then continue the time count if I send a signal on it.

I made a program that actually saves it and shows after opening the program. However, once I continue to count, it starts again from zero.

What I have tried:

VB
Private Sub SaveTimeValue()
    Dim timeValue As String = Label1.Text
    Using writer As New StreamWriter(filePath)
        writer.Write(timeValue)
    End Using
End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) _
    Handles MyBase.Load
    If File.Exists(filePath) Then
        Using reader As New StreamReader(filePath)
            Dim timeValue As String = reader.ReadToEnd()
            Label1.Text = timeValue
        End Using
    End If
End Sub

Private Sub Form1_FormClosing(sender As Object, _
    e As FormClosingEventArgs) Handles MyBase.FormClosing
    SaveTimeValue()
End Sub
Posted
Updated 13-Sep-23 9:41am
v3

We can't really help you - we can't see your code other than the brief fragment you show us, and that would appear to do what you are asking for, though there are better ways to do that. For example, your SaveTimeValue method could be replaced with
VB
File.WriteAllText(filePath, Label1.Text)
And the load event handler with
VB
If File.Exists(filePath) Then
    File.ReadAllText(filePath, Label1.Text)
End If
Or better, use the Settings file instead: Manage application settings (.NET) - Visual Studio (Windows) | Microsoft Learn[^] - That will allow you to manage a TimeSpan object directly, instead of having to convert it from a string each time you want to update it.

Create a class level TimeSpan value and add or remove time from that, then update the Label.Text field from that value, and save your settings value when you exit, reload it when you load the form. You can have multiple settings values a lot easier that way, so you can also have class level TimeSpan variables for your other labels which are also preserved in the same way.

If you'd rather try to fix the existing code - and I wouldn't - then I'd start by using the debugger to check what is going on by putting a breakpoint on the first line of the Load handler and watching what path it took through the code. Probably, you'll find that the filePath isn't set or isn't set correctly and it's not loading because of that, or that something else in your code (that we can't see) is resetting the Label value, maybe when you "restart the count".

Sorry, but we can't fix what we can't see!
 
Share this answer
 
Comments
Marlon Rivera 27-Aug-23 3:10am    
Yes sir it is working, my problem only after I start again the timer the count start from zero not from the last value that it save (Ex: I close it from 00:05:07, after I open the program the label shows 00:05:07 but when I start again the time again it count from 00:00:01 not from 00:05:08)
OriginalGriff 27-Aug-23 3:18am    
So look at the code that you use for restarting it ...
Your timer starts at zero because you are only saving the value of 'Label1.Text' which represents 'downtime', and not considering the 'idle time' or 'total time'. Additionally, you are only saving 'downtime' but not the other time values. You need to save all three time values and their respective labels -
VB
Private Sub SaveTimeValue()
    Dim downtimeValue As String = Label1.Text
    Dim idleTimeValue As String = Label2.Text
    Dim totalTimeValue As String = Label3.Text

    Using writer As New StreamWriter(filePath)
        writer.WriteLine(downtimeValue)
        writer.WriteLine(idleTimeValue)
        writer.WriteLine(totalTimeValue)
    End Using
End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    If File.Exists(filePath) Then
        Using reader As New StreamReader(filePath)
            Dim downtimeValue As String = reader.ReadLine()
            Dim idleTimeValue As String = reader.ReadLine()
            Dim totalTimeValue As String = reader.ReadLine()

            Label1.Text = downtimeValue
            Label2.Text = idleTimeValue
            Label3.Text = totalTimeValue
        End Using
    End If
End Sub

Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
    SaveTimeValue()
End Sub
 
Share this answer
 
1st: you can save your values at Project Settings.
2nd: Look to this Example:

General Declaration Variables at Class:
VB
Dim Time As New DateTime
  Dim Value As Long
  Dim Expr As String

Add a timer at the Form and use this statement:
Statements at Button to start the Timer:
VB
Me.Timer1.Enabled = Not Me.Timer1.Enabled
If Me.Timer1.Enabled = True Then
   Value = My.Settings.Value
Else
   My.Settings.Value = Value
   My.Settings.Save()
End If

Statement at Tick event of Timer:
VB
Value += 1
With Time.AddSeconds(Value)
 Expr = String.Format("{0:00}:{1:00}:{2:00}", .Hour, .Minute, .Second)
 End With
Debug.Print(Expr)

Each time you clikc on the Button, Time started to count and Continue thet.... (at Debug window)
 
Share this answer
 
v3

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