Annoying Time format issue
Hi all,
I'm working on a program right now that restarts the computer at a set time, an "automatic shutdown" sort of tool. HERE and HERE are my previous threads on my issues with it, for more info.
http://arbital24.dyndns.org/Dev/VB/REQs/ST/NewProg.PNG
My newest issue is this:
It's currently set so that when I use the left hand portion of the program (for shuttting down with a countdown timer) it will display the time currently, and how long until shutdown (a countdown).
When You use the right hand side of the form, it shows the time currently and at what time the specified time will be reached.
What I'm trying to do now is make the left hand side part display not only how long left until shut-down, counting down, but also the TIME at which it will shut down. For the right hand side, I want it to display how much time is left until shut-down. Basically I want to add the alternate features to either side.
For the left hand side, I tried to add "Time -" to the code, like so:
ORIGINAL CODE:
Code:
newTime = Time
diff = DateDiff("s", newTime, OldTime)
TTSD.Caption = (diff \ 60) & ":" & _
Format((diff - ((diff \ 60) * 60)), "00")
NEW CODE:
Code:
newTime = Time
diff = DateDiff("s", newTime, OldTime)
TTSD.Caption = (diff \ 60) & ":" & _
Format((diff - ((diff \ 60) * 60)), "00")
TSD.Caption = (Time - (diff \ 60) & ":") & _
Format((diff - ((diff \ 60) * 60)), "00")
After I tried this, this was the result that I got (for a clock set to 1 minute in the future):
http://arbital24.dyndns.org/Dev/VB/REQs/ST/1899.PNG
1899? :S
I cannot simply copy the code that I am using from the right-hand side to display the time it will shut down, since all that does it print what's in those boxes on the right, which would be wrong since it's taking the time from the section on the left.
For the right hand side, I tried simple using code from the left to display the correct time:
Code:
newTime = Time
diff = DateDiff("s", newTime, OldTime)
TTSD.Caption = (diff \ 60) & ":" & _
Format((diff - ((diff \ 60) * 60)), "00")
However, when I activated the clock and timer, it came up showing this:
http://arbital24.dyndns.org/Dev/VB/REQs/ST/476.PNG
As you can see, It's not so easy to just copy the code across on this program. What I'm basically asking is two things:
1. How to take a time, and a countdown time in seconds, and add the countdown onto the "time" to produce an end-time
(This is so that for the left hand section, I can display not just how long LEFT until shutdown, but also at what TIME it will shutdown. Just like the section on the right)
2. What's the format for displaying one time minus another?
(This is so that for the right hand section, I can show not just WHEN it'll shut down, but also how long LEFT until shutdown. A shutdown timer, just like on the left)
Latest ZIP of my complete program:
Linky Mc Link
Thank you :)
Re: Annoying Time format issue
Hi,
For a count down timer you can try using a timer whose interval is set to 1000. Then decrease the set time by 1 in every execution.
For example...
VB Code:
Private Sub Form_Load()
Timer1.interval = 1000
Timer1.enabled = true
RefreshInt = Val(TxtInterval.txt) * 60 ' Take the interval time from a text box
End Sub
Private Sub Timer1_Timer(ByVal RefreshInt as Integer )
RefreshInt = RefreshInt - 1
Lbl.Caption = RefreshInt & "seconds" ' For displaying the countdown in a label
If RefreshInt = 0 Then
'## Execute Main Function of scanning and cheking
' Run the function what ever you wish too when the countdown
DoFunction
End If
End Sub
For displaying the time left for shutdown.. try taking the current system time. Split it into a array and display some information on to a label or a text box after reducing the time shown in seconds in the first label from a the corresponding array index.
Hope it helps ... :)
--Putta
Re: Annoying Time format issue
Quote:
Originally Posted by putta
VB Code:
Private Sub Form_Load()
Timer1.interval = 1000
Timer1.enabled = true
RefreshInt = Val(TxtInterval.txt) * 60 ' Take the interval time from a text box
End Sub
Private Sub Timer1_Timer(ByVal RefreshInt as Integer )
RefreshInt = RefreshInt - 1
Lbl.Caption = RefreshInt & "seconds" ' For displaying the countdown in a label
If RefreshInt = 0 Then
'## Execute Main Function of scanning and cheking
' Run the function what ever you wish too when the countdown
DoFunction
End If
End Sub
The problem here is that, I don't have a text box that i can take an amount of seconds from. I have countdown timers working fine, the program is using it on this format. I'm assuming this is the solution for the right hand (click) side of the form?
Quote:
For displaying the time left for shutdown.. try taking the current system time. Split it into a array and display some information on to a label or a text box after reducing the time shown in seconds in the first label from a the corresponding array index.
Hope it helps ... :)
--Putta
Could you explain to me how to use arrays though, please? I'm really a complete newbie to VB and we weren't taought that in the real basics of our course. Thanks :)
Re: Annoying Time format issue
Take a look at my countdown timer. It keeps perfect time.
Just use your dateadd instead of mine, which adds 300 seconds (5 minutes)
http://vbforums.com/attachment.php?attachmentid=38882
Re: Annoying Time format issue
I used that code for part of my program when you last posted in my thread, Thank you for that. However, I can't get that to work with this, since I'm not using a normal text field that I can get a variable from, but that special "Date" type one (but for time)
Re: Annoying Time format issue
Maybe you should focus on the function Dateadd :)
Re: Annoying Time format issue
Another little problem:
On the lable TSD, I'm trying to display the date and time that it will shut down. making TSD = tdTime (the little "time" box on the right at the bottom) displays both the time of shutdown AND the CURRENT date. Making it show tdTime and tdDate shows the time, and CURRENT date and the date to shut down in succession. I tried disabling it via adding the checkbox property, but when unchecked it just doesn't display at all and when checked, it displays both still.
How do I make tdTime display only the time, and not the date?
http://arbital24.dyndns.org/Dev/VB/REQs/ST/DAT.PNG
(And again, showing the text table maximised to show the cut off data from the first screenshot)
http://arbital24.dyndns.org/Dev/VB/REQs/ST/DAT2.PNG
Code:
TSD = dtDate & " - " & DtTime
CTimer.Enabled = True
TSD = green text lable
dtDate = Blue circled value
dtTime = Red circled value
Latest ZIP of the project (3rd ZIP)
*********************************************************
As for the static "time to shutdown" for the timer section, I got that working fine but putting the code in the set button rather than the timer. Thanks for the advice!
http://arbital24.dyndns.org/Dev/VB/REQs/ST/OK.PNG
Re: Annoying Time format issue
So to recap, all that's left needing sorting with the program now are two issues from the Date side of the form:
1. dtTime is displaying the current date, as well as the projected shutdown time
2. Still can't figure out a way of making a countdown timer that takes the current date and time, and the shutdown date and time, and calculates the seconds left until shutdown, and outputs that to the form (as the red text countdown) (Since i would have to make a variable as current TIME, produce a "diff" of the shutdown time (The main issue here is that it takes in both a Time AND DATE, and I can't simbly perform subtraction on it, as it's not the correct time value) and compare these two (Easy except for the previous mentioned problem)
Re: Annoying Time format issue
Why not let the Task Sceduler handle for you? Just write the shutdown code as a separate program, and then use the AT command. You would have to SHELL it from VB. To get info, type AT /? from a command prompt.
Re: Annoying Time format issue
Whilst that would be possible, there are probably also other programs on the market that can do the same as this program, and others of mine. The point is that I like to make my own programs, and to learn from the coding that I do along the way, since my "Taught" VB is so minimal. From this project that I set myself, i've already learned about the workings of timers, and have started integrating them inot my programs (I wrote something else with a timer the other day).
That kind of progressive learning is what is getting me more able to do more with programs than the very basic first few commands that we learnt at college. It's not in the final result for me, but in the creation. I just host the programs on my site in case anyone might benefit from them. I doubt that my programs are as good as something anyone else could do but hey, i'm still learning :)
The thing is, I'm SO close to finishing this program now. All I have to do is iron out these two bugs and it's complete. Whilst trying to figure out the last couple of bits of code, I've written another two programs. It's not that I'm getting frustrated with this program but It's just that everything is working except these last couple of bits, and I really want to finish it now.
Are the two problems that I described not fixible, then?
Re: Annoying Time format issue
I added this line to compute the shutdown date
VB Code:
TSD = DateAdd("d", DateDiff("d", Date, dtDate), Date)
You can play around with it to add the time, but you'd have to check if the date is greater than today, and if so, add 1 days worth of seconds to each datediff. That way you could use your other code to calculate # of hours.
Re: Annoying Time format issue
*Thinks for a minute*
Ahh, I think that's code to make it display the "Time Of Shutdown" when "Countdown" is used, correct? (This project is starting to confuse me now...)
I already got that bit working (see end of last big post), thank you though :)
I just re-read my last "To recap" post.. It doesn't make much sense, i'll admit.
The problems left in my program are:
* When using the "Clock/Shutdown" section, it displays the current date as well as the projected date of shutdown (You explained about that)
* When using the "Clock/Shutdown" section, I'd like to make a way of utilising the red "Time remaining until shutdown" section, which will count down from that current date/time (the real time when "Set" is pressed) until the one set by the controls in that frame is reached (at which time the computer will restart)
EDIT: Just found another error:
A) In "Countdown", settigng especially large "Hour" figures (Max are 500 in hours, 999 in minutes, 9999 in seconds) make it appear with a date which is completely wrong (like 1900 or 1899). I'm obviously not sure if this is actually the date it'll try and shut down (IOW, it actually breaks the code) or not, but either way it's wrong. This appears to happen after the 10th hour
B) Some values in "Hours", below 500 (Like 80 and 6) make it come up with the "Please enter 500 hours or less" validation which is supposed to only kick in if the value is over 500. I had to remove the code for validating mins and secs since it stopped the code working completely).
Here is the code for that section:
Code:
If UTi = True Then
If Hrs = "" Or Mins = "" Or Secs = "" Then
MsgBox "Please enter a value in all 3 boxes. If you don't want to use a box, enter a '0' in it.", , "Incorrect input"
Exit Sub
End If
If Hrs > "500" Then
MsgBox "Please enter 500 or less for hours.", , "Error in input"
Exit Sub
End If
Thanks :)
Re: Annoying Time format issue
I think you want the countdown for a date to show
Quote:
x Days, x Hours, x Minutes, x Seconds
Set a flag if it is the current is the right day to shutdown, if so, don't print the days. Just hours, min, sec!
It would start out at 1 day, 6 hours... and after 6 hours, change to 24 hours...