[RESOLVED] Vb6: elapsed time + time in label1.caption
Basically I need this:
on form_load, start label1 counting how long since form1 loaded = this i already did it in the code bellow
and i need 2 buttons, one to save current time running on label1 in textfile
and second to add or load saved textfile into label1 and keep counting from there
i hope you got my point
here my code, plz help me out:
----------------
Private mStartTime As Date
Private Sub Form_Load()
mStartTime = Now
tmrDelta.Interval = 500
End Sub
Private Sub tmrDelta_Timer()
Label1.Caption = "Started: " & GetElapsedTimeString(Now - mStartTime)
End Sub
Function GetElapsedTimeString(ByVal dT As Double) As String
Const sFmt$ = " \d\a\y\s | h \h\o\u\r\s | m \m\i\n"
GetElapsedTimeString = Int(dT) & Format$(dT, sFmt)
End Function
End Sub
-----------
Last edited by geekmaro; Jan 10th, 2021 at 11:45 PM.
So you are asking how to do a simple file read/write?
You can use the basic Open statement. It doesn't matter what you are trying to read or write, text is text so same methods as any simple text file.
So you are asking how to do a simple file read/write?
You can use the basic Open statement. It doesn't matter what you are trying to read or write, text is text so same methods as any simple text file.
no, you didnt understand what im trying to do
I want to count elapsed time from saved textfile
you can read again what i asked for and im sure you will understand
Hi,
i thing i got what are you trying to accomplish ,
try to add the class Stopwatch
'class
Option Explicit
Private Declare Function QueryPerformanceCounter Lib "kernel32" (X As Currency) As Boolean
Private Declare Function QueryPerformanceFrequency Lib "kernel32" (X As Currency) As Boolean
Private m_startTime As Currency
Private m_freq As Currency
Private m_overhead As Currency
Public Sub start()
10 QueryPerformanceCounter m_startTime
End Sub
Public Function ElapsedSeconds() As Double
Dim currentTime As Currency
10 QueryPerformanceCounter currentTime
20 ElapsedSeconds = (currentTime - m_startTime - m_overhead) / m_freq
End Function
Public Function ElapsedMilliseconds() As Double
10 ElapsedMilliseconds = ElapsedSeconds * 1000
End Function
Private Sub Class_Initialize()
10 QueryPerformanceFrequency m_freq
Dim ctr1 As Currency
Dim ctr2 As Currency
20 QueryPerformanceCounter ctr1
30 QueryPerformanceCounter ctr2
40 m_overhead = ctr2 - ctr1
End Sub
then in your form
Dim sw As STOPWATCH
10 Set sw = New STOPWATCH
20 sw.start
'do some stuff here to let the time moves...
100 msgbox sw.ElapsedMilliseconds
there you have the time elapsed ...
Private mStartTime As Date
Private Sub Command1_Click()
Open App.Path & "\time.data" For Binary As #1
Put #1, , mStartTime
Close #1
End Sub
Private Sub Command2_Click()
Open App.Path & "\time.data" For Binary As #1
Get #1, , mStartTime
Close #1
End Sub
Private Sub Form_Load()
mStartTime = Now
tmrDelta.Interval = 500
End Sub
Private Sub tmrDelta_Timer()
Label1.Caption = "Started: " & GetElapsedTimeString(Now - mStartTime)
End Sub
Function GetElapsedTimeString(ByVal dT As Double) As String
Const sFmt$ = " \d\a\y\s | h \h\o\u\r\s | m \m\i\n | s \s\e\c\o\n\d\s"
GetElapsedTimeString = Int(dT) & Format$(dT, sFmt)
End Function
when u click Command1 it will save the current time. u can now close the program.
when u open the program again, u will notice it will start over, but if u use Command2 it will take the old starting time and continue from there. (added seconds, just to see the result, dont want to wait minutes for the testing)
Option ExplicitDim inFile As String
Dim outFile As String
Private mStartTime As Date
Private Sub Command1_Click()
outFile = FreeFile
Open "Path and filename here" For Output As #outFile
Print #outFile, Label1.Caption
Close #outFile
End Sub
Private Sub Command2_Click()
Dim myLine As String
inFile = FreeFile
Open "Path and filename here" For Input As #inFile
Line Input #inFile, myLine
Label1.Caption = myLine
mStartTime = Now 'Reset Start Time
Close #inFile
End Sub
Private Sub Form_Load()
mStartTime = Now
tmrDelta.Interval = 500
End Sub
Private Sub tmrDelta_Timer()
Label1.Caption = "Started: " & GetElapsedTimeString(Now - mStartTime)
End Sub
Function GetElapsedTimeString(ByVal dT As Double) As String
Const sFmt$ = " \d\a\y\s | h \h\o\u\r\s | m \m\i\n"
GetElapsedTimeString = Int(dT) & Format$(dT, sFmt)
End Function
Option ExplicitDim inFile As String
Dim outFile As String
Private mStartTime As Date
Private Sub Command1_Click()
outFile = FreeFile
Open "Path and filename here" For Output As #outFile
Print #outFile, Label1.Caption
Close #outFile
End Sub
Private Sub Command2_Click()
Dim myLine As String
inFile = FreeFile
Open "Path and filename here" For Input As #inFile
Line Input #inFile, myLine
Label1.Caption = myLine
mStartTime = Now 'Reset Start Time
Close #inFile
End Sub
Private Sub Form_Load()
mStartTime = Now
tmrDelta.Interval = 500
End Sub
Private Sub tmrDelta_Timer()
Label1.Caption = "Started: " & GetElapsedTimeString(Now - mStartTime)
End Sub
Function GetElapsedTimeString(ByVal dT As Double) As String
Const sFmt$ = " \d\a\y\s | h \h\o\u\r\s | m \m\i\n"
GetElapsedTimeString = Int(dT) & Format$(dT, sFmt)
End Function
i m having a problem with command2, it loads the time from textfile but immediately goes back to Started: 0 days | 0 hours | 0 min
how to fix it ?
Private mStartTime As Date
Private Sub Command1_Click()
Open App.Path & "\time.data" For Binary As #1
Put #1, , mStartTime
Close #1
End Sub
Private Sub Command2_Click()
Open App.Path & "\time.data" For Binary As #1
Get #1, , mStartTime
Close #1
End Sub
Private Sub Form_Load()
mStartTime = Now
tmrDelta.Interval = 500
End Sub
Private Sub tmrDelta_Timer()
Label1.Caption = "Started: " & GetElapsedTimeString(Now - mStartTime)
End Sub
Function GetElapsedTimeString(ByVal dT As Double) As String
Const sFmt$ = " \d\a\y\s | h \h\o\u\r\s | m \m\i\n | s \s\e\c\o\n\d\s"
GetElapsedTimeString = Int(dT) & Format$(dT, sFmt)
End Function
when u click Command1 it will save the current time. u can now close the program.
when u open the program again, u will notice it will start over, but if u use Command2 it will take the old starting time and continue from there. (added seconds, just to see the result, dont want to wait minutes for the testing)
it works but i need to save label1 time with text1.text in textfile and load it and get second line of textfile which is the saved time to label1
Open App.Path & "\1.txt" For Output As #1
Print #1, Text1.Text Put #1, , mStartTime = it says bad file mode
Close #1
plz help
Last edited by geekmaro; Jan 11th, 2021 at 10:40 AM.
so far i was able to save label1 + text1 into textfile using this code:
----
Open App.Path & "\1.txt" For Binary As #1
Put #1, , Text1.Text & vbCrLf
Put #1, , mStartTime
Close #1
--
but now i need to load only second line from textfile into label1
i tried this but not working
----
Dim line1 As String, line2 As String
Open App.Path & "\1.txt" For Binary As #1
Line Input #1, line2
Get #1, , mStartTime = line2
Close #1
should clicking on the save button, save the current time on that text-file, maybe u need to use Append as well?
it seems that u need:
1. click on "save" to store the current time on a text-file, each time you do that it will add a new time on the text-file
2. click on "load" will take the "last" time and continue counting.
should clicking on the save button, save the current time on that text-file, maybe u need to use Append as well?
it seems that u need:
1. click on "save" to store the current time on a text-file, each time you do that it will add a new time on the text-file
2. click on "load" will take the "last" time and continue counting.
that means, u need to use eof(1) and Append.
exactly now you understand what i need, but i have to save text1.text as first line in textfile, and label1.caption as second line in same textfile
then when load, get last time and continue counting from second line in textfile, so you need to skip first line.
hope all clear
plz give me the code
thanx alot for your help
I just hacked this together real quickly...I save THREE things in your text file: Text1.Text, Label1.Caption and Label2.Caption (I put a in invisible label2 on the form and set its value to NOW---you can do the same by using a variable to save "NOW").
Notice I read the THIRD line in the file! IOW, you really don't need the second line (label1.caption) at all...if you opt not to include it in the file, simply delete one of the 'Line Input' calls.
Hope this works for you:
Code:
Option ExplicitDim inFile As String
Dim outFile As String
Dim newTime As Date
Dim bRestart As Boolean
Private mStartTime As Date
Private Sub Command1_Click()
outFile = FreeFile
Open "C:\users\denni\time.txt" For Output As #outFile
Print #outFile, Text1.Text
Print #outFile, Label1.Caption
Print #outFile, Now
Close #outFile
End Sub
Private Sub Command2_Click()
Dim myLine As String
inFile = FreeFile
Open "C:\users\denni\time.txt" For Input As #inFile
Line Input #inFile, myLine
Line Input #inFile, myLine
Line Input #inFile, myLine
Label2.Caption = myLine
newTime = CDate(myLine)
bRestart = True
Close #inFile
End Sub
Private Sub Form_Load()
mStartTime = Now
tmrDelta.Interval = 500
Label2.Caption = CStr(Now)
End Sub
Private Sub tmrDelta_Timer()
If bRestart = False Then
Label1.Caption = "Started: " & GetElapsedTimeString(Now - mStartTime)
Else
Label1.Caption = "Started: " & GetElapsedTimeString(Now - newTime)
End If
End Sub
Function GetElapsedTimeString(ByVal dT As Double) As String
Const sFmt$ = " \d\a\y\s | h \h\o\u\r\s | m \m\i\n"
GetElapsedTimeString = Int(dT) & Format$(dT, sFmt)
Label2.Caption = CStr(Now)
End Function
I just hacked this together real quickly...I save THREE things in your text file: Text1.Text, Label1.Caption and Label2.Caption (I put a in invisible label2 on the form and set its value to NOW---you can do the same by using a variable to save "NOW").
Notice I read the THIRD line in the file! IOW, you really don't need the second line (label1.caption) at all...if you opt not to include it in the file, simply delete one of the 'Line Input' calls.
Hope this works for you:
Code:
Option ExplicitDim inFile As String
Dim outFile As String
Dim newTime As Date
Dim bRestart As Boolean
Private mStartTime As Date
Private Sub Command1_Click()
outFile = FreeFile
Open "C:\users\denni\time.txt" For Output As #outFile
Print #outFile, Text1.Text
Print #outFile, Label1.Caption
Print #outFile, Now
Close #outFile
End Sub
Private Sub Command2_Click()
Dim myLine As String
inFile = FreeFile
Open "C:\users\denni\time.txt" For Input As #inFile
Line Input #inFile, myLine
Line Input #inFile, myLine
Line Input #inFile, myLine
Label2.Caption = myLine
newTime = CDate(myLine)
bRestart = True
Close #inFile
End Sub
Private Sub Form_Load()
mStartTime = Now
tmrDelta.Interval = 500
Label2.Caption = CStr(Now)
End Sub
Private Sub tmrDelta_Timer()
If bRestart = False Then
Label1.Caption = "Started: " & GetElapsedTimeString(Now - mStartTime)
Else
Label1.Caption = "Started: " & GetElapsedTimeString(Now - newTime)
End If
End Sub
Function GetElapsedTimeString(ByVal dT As Double) As String
Const sFmt$ = " \d\a\y\s | h \h\o\u\r\s | m \m\i\n"
GetElapsedTimeString = Int(dT) & Format$(dT, sFmt)
Label2.Caption = CStr(Now)
End Function
this is what i got when i saved textfile:
----
Text1
Started: 0 days | 0 hours | 6 min
1/11/2021 12:00:23 PM
-----
but when i click command2, nothing happened to label1
Really? With that code label1 changes to the time (minute) set in the textfile, and then the counter starts from THAT time and every minute more it will change.
Are you using all this code, and in conjunction with YOUR project? If so, start a NEW project, add a text box, two labels and two command buttons and run it (name your timer as tmrDelta, leave all the rest as default).
Really? With that code label1 changes to the time (minute) set in the textfile, and then the counter starts from THAT time and every minute more it will change.
Are you using all this code, and in conjunction with YOUR project? If so, start a NEW project, add a text box, two labels and two command buttons and run it (name your timer as tmrDelta, leave all the rest as default).
Works fine for me.
i download your project and tested it
when i click command1, i get this inside textfile:
---
SAM
Started: 0 days | 0 hours | 8 min
1/11/2021 12:38:09 PM
----
then when i closed form1 and run it again i got this:
label1 = Started: 0 days | 0 hours | 1 min
it should show: Started: 0 days | 0 hours | 8 min and continue counting from the moment i clicked command2
you got it now ?
Think about it, though. Maybe you can do it yourself now that you know how to save and read files...
The first time your program is run you need to have all zeros in your label, which means you set some variable to NOW.
All future times the program is run, you should read the last saved file and start with that date/time (that's why I used a second label to capture date/time instead of a string).
One way to do it is in the first time the program is run, start your program like you did originally (mStartTime = Now), but after that, your mStartTime should be taken from the file and compared to NOW.
Another way to do this is to use a database table...much easier to understand than reading/writing text files.
If I find time in the morning, I'll crank up something...but I am sure others can assist before then.
Think about it, though. Maybe you can do it yourself now that you know how to save and read files...
The first time your program is run you need to have all zeros in your label, which means you set some variable to NOW.
All future times the program is run, you should read the last saved file and start with that date/time (that's why I used a second label to capture date/time instead of a string).
One way to do it is in the first time the program is run, start your program like you did originally (mStartTime = Now), but after that, your mStartTime should be taken from the file and compared to NOW.
Another way to do this is to use a database table...much easier to understand than reading/writing text files.
If I find time in the morning, I'll crank up something...but I am sure others can assist before then.
I THINK what you are after is a label telling you how many minutes the program has been run, including subsequent runs.
Meaning, at first go it runs, for example, 4 minutes, then the user quits the program. Next time the program is started, you want to have those
4 minutes as the starting point, so that you will know how many minutes the program has been run total time. At least I THINK this is your goal.
If so, I am working up a small example using a database to keep track of total time it is being run.
I THINK what you are after is a label telling you how many minutes the program has been run, including subsequent runs.
Meaning, at first go it runs, for example, 4 minutes, then the user quits the program. Next time the program is started, you want to have those
4 minutes as the starting point, so that you will know how many minutes the program has been run total time. At least I THINK this is your goal.
If so, I am working up a small example using a database to keep track of total time it is being run.
BUT, may not be for a bit...lot to do tomorrow.
now you understand my point
i dont think you need database, i just need to save label1 value into textfile along with text1.text
and load it again
no need to complicate things
and think about saving and loading for text1 as well
i can wait till tomorrow
no prob. thanx
Ok, so this is what I did...one textbox, one label, one "Exit" button (the exit button saves the new runtime to the textfile).
You can experiment by editing the text file...currently it is at:
SAMMI
0
(The zero represent the total runtime--will change if you run the program for at least one minute)
That 0 will change when you exit the program (will increase by the number of minutes the program has been run), and will be loaded next time at form_load). Put in a larger number if you want to test to see # of days and hours and minutes how they change.
If you find an error (or it is not exactly what you want), let me know.
Ok, so this is what I did...one textbox, one label, one "Exit" button (the exit button saves the new runtime to the textfile).
You can experiment by editing the text file...currently it is at:
(The zero represent the total runtime--will change if you run the program for at least one minute)
That 0 will change when you exit the program (will increase by the number of minutes the program has been run), and will be loaded next time at form_load). Put in a larger number if you want to test to see # of days and hours and minutes how they change.
If you find an error (or it is not exactly what you want), let me know.
Sammi
Right here at form load, i need to load text1.text as well, the way you did it getting the 2nd line for label1 i dont like it, i wana something simpler like this:
Dim line1 As String, line2 As String, line3 As String, line4 As String, line5 As String, line6 As String, line7 As String, line8 As String
Open App.Path & "\1.txt" For Input As #1
Line Input #1, line1
Line Input #1, line2
Close #1
Text1.Text = line1
Label1.Caption = line2
could you implement something like this resend me project?
thanks alot
Private mStartTime As Date
Private Sub Command1_Click()
Open App.Path & "\time.txt" For Output As #1
Print #1, Text1.Text
Print #1, mStartTime
Close #1
End Sub
Private Sub Command2_Click()
Dim Data$
Open App.Path & "\time.txt" For Input As #1
Line Input #1, Data
Text1.Text = Data
Line Input #1, Data
mStartTime = Data
Close #1
End Sub
Private Sub Form_Load()
mStartTime = Now
tmrDelta.Interval = 500
End Sub
Private Sub tmrDelta_Timer()
Label1.Caption = "Started: " & GetElapsedTimeString(Now - mStartTime)
End Sub
Function GetElapsedTimeString(ByVal dT As Double) As String
Const sFmt$ = " \d\a\y\s | h \h\o\u\r\s | m \m\i\n | s \s\e\c\o\n\d\s"
GetElapsedTimeString = Int(dT) & Format$(dT, sFmt)
End Function
geez...I give up....it is as simple as it can be....it puts in TWO lines, text1.text and the total runtime.
So, I am done.
Off this thread.
Oh, and 'you're welcome'
plz help me out this code:
Private Sub Form_Load()
tmrDelta.Interval = 1000
Dim myLine As String
inFile = FreeFile
Open App.Path & "\time.txt" For Input As #inFile
Line Input #inFile, myLine
Line Input #inFile, myLine
savedMinutes = CLng(myLine)
convertMinutesToHoursAndDays (savedMinutes)
tmrDelta.Enabled = True
Close #inFile
End Sub
i need to make it simpler
load text1 + time into label1
geez...I give up....it is as simple as it can be....it puts in TWO lines, text1.text and the total runtime.
So, I am done.
Off this thread.
Oh, and 'you're welcome'
I made it work, thanks for your help
Private Sub Form_Load()
Dim line1 As String, line2 As String
tmrDelta.Interval = 1000
inFile = FreeFile
Open App.Path & "\time.txt" For Input As #inFile
Line Input #inFile, line1
Line Input #inFile, line2
Text1.Text = line1
savedMinutes = CLng(line2)
convertMinutesToHoursAndDays (savedMinutes)
tmrDelta.Enabled = True
Close #inFile
End Sub