Count down clock - getting the difference between two times - vb.net

stuck on a simple one.
I have an project that will count down to a start time, the start time is generated from a datetime picker,("HH:mm"), when a button is clicked the time .now is subtracted and the timespan is passed to a timer for the countdown.
a rough outline of the code is
Aa4 is the output from the DateTime picker
Dim Span3
Dim TimetoStart as date
TimetoStart = TimeOfDay.Date 'get current time
Span3=StartTime.Subtract(Aa4) 'Subtract current time from output from datetime picker
LaBel4.text=span3 'Output to label (will be timer later for count down)
Error generated is system.invalidcastexception 'conversion from type timespan to type string is not valid.
As I said at the start it will be easy, I am not seeing it and its doing my head in, cheers all Daz

Related

DateTimePicker Default Value

I am using VB.Net 2008.
I have two DateTimePicker. The first has a default value 8/24/2017 1:35 PM (as it was created Aug. 24. The second one has the default value of what is the current date and time. I haven't set their default values in Properties.
It is just weird for me that they have different default values. Anyway, my desired value is the current date and time.
I have also experienced confusion on my previous project regarding DateTimePicker where it has always default time 1:32 PM where time should be disregarded. The format is "Short" and the default value is the current date w/o time.
Can anyone explain why it happens?
Thank you in advance.
In my experience, a DateTimePicker contains the current date and time in its Value property unless you set it otherwise. Why that should not be happening for one of your controls, I don't know. In that case though, I'd suggest simply deleting that control and adding a new one. Hopefully that will behave as expected.
There's no such thing as a DateTimePicker that "disregards" time. The Value property of a DateTimePicker is type DateTime and a DateTime ALWAYS has both a date component and a time component. You can set the Format and optionally CustomFormat properties to show only the date, only the time or only some part thereof but that makes exactly zero difference to what's stored in the Value property.
Just like for any other DateTime value, if you want the Value of a DateTimePicker to represent just a date then you should zero the time. If you want to use the current date without a time then use DateTime.Today, where DateTime.Now would get you the current time as well as the current date. If you want to zero the time of any DateTime value, including the Value of a DateTimePicker, then you get its Date property. Note that that does not affect the original value, but creates a new DateTime value with the same date and the time zeroed, e.g.
Dim selectedDate = myDateTimePicker.Value.Date
Note that DateTime.Today simply returns DateTime.Now.Date.

vb Count Up From Time (Last Updated: ** Seconds/Minutes Ago)

Okay, I hate just posting a question with no code, but I literally have no idea how to get started other than to user a Timer. I have a Visual Basics program which works with an SQL Table. I'd like to have a label which shows the user how 'old' their display of the table is.
Each time they edit or refresh the table a label is refreshed with a current time stamp. Formatted as 03:19;27.
However, I'd like to show how many seconds or even just minutes the table was last updated.
For example: 'Last updated: 22 Seconds Ago' OR 'Last Updated: Less Than A Minute Ago'.
How could I do this?
The TimeSpan object provides all of the properties you need to format the message however you want. The formatting options in it's ToString method may also be very helpful to you. To get a TimeSpan object, you can simply subtract one Date from another, like this:
Dim span As TimeSpan = lastQueryDate - Date.Now
Dim message As String = String.Format("Last Updated: {0} Seconds Ago", span.TotalSeconds)
Or, if you want to use a Stopwatch object, you could get the span of time since the stopwatch was started by accessing its Elapsed property, which is also a TimeSpan object.

Media Player - Show custom Time Code

I am trying to make a video player for a project which seems to be quite a challenge when I came to a stage where I need to show a time code (sort of time stamp) in a label next to the video being played.
The function of the program is...The movie file name contains the recording start time of the movie file in the format "17:56:33_Camera01.avi". When I load the movie in the player using the URL and click play the movie plays. I use a timer to get the current position of the playback in seconds,minutes and hours using the below method in the timer tick sub.
Dim PlayHour, PlayMin, PlaySec As Integer
Dim iSecond As Double = AxWindowsMediaPlayer1.Ctlcontrols.currentPosition
Dim iSpan As TimeSpan = TimeSpan.FromSeconds(iSecond)
PlayHour = iSpan.Hours.ToString.PadLeft(2, "0"c)
PlayMin = iSpan.Minutes.ToString.PadLeft(2, "0"c)
PlaySec = iSpan.Seconds.ToString.PadLeft(2, "0"c)
My requirement is to dynamically set a time code in the label by adding the current position hour, minute and second to the recorded time of the movie.
For example the movie file will show the time code as 17:56:33" when loaded, when the playback begins the seconds,minutes and hours (17:56:34, 17:56:35 and so...) should start incrementing in accordance with the actual time of the playback.
Can any one please guide me in the right direction... I am breaking my head on this since quite some time and heading no where...I am quite sure I have to use Timespan for this but don't know how to get this done.
Stick this in the tick event of a timer and set the interval to 1...
Dim controls As WMPLib.IWMPControls3 = axWindowsMediaPlayer1.Ctlcontrols
controls.currentPositionTimecode = "[00000]01:00:30.05"
Label1.Text = controls.currentPositionString
All you'll need to change is "Label1" and "axWindowsMediaPlayer1". You might want to play around with "[00000]01:00:30.05" to get the format you require; this displays mm:ss.
hii add label to media and this code to timer of track bar
` TrackBar2.Maximum = AxWindowsMediaPlayer1.Ctlcontrols.currentItem.duration
Label3.Text = AxWindowsMediaPlayer1.Ctlcontrols.currentPositionString
TrackBar2.Value = AxWindowsMediaPlayer1.Ctlcontrols.currentPosition
Label4.Text = AxWindowsMediaPlayer1.Ctlcontrols.currentItem.durationString `

How to edit timespan values with an UpDown control using VB.NET?

I have a timer with a textbox displaying a Timespan value (duration).
When I stop the timer I want to edit the value of the Timespan with an UpDown control.
I want something like this:
But the above control is a datetimePicker with the Format property set to "Custom", the CustomFormat property set to "HH:mm:ss" and the ShowUpDown property to true.
So it can display only dateTime values.
I want it to display Timespan values (such as "32.23:59:59") and with the UpDown control to edit the seconds or the minutes or the hours or the days depending which is marked from the cursor.
How is that possible?
Do I have to create a custom control from scratch?
Thanks in advance.
Edited: After a lot of thought and experiments I found out that if the DomainUpDown Control had the methods SelectionStart and SelectionLength it would be possible to edit a TimeSpan using the DomainUpDown Control.
I submitted such a proposal to the Visual Studio UserVoice site. Please vote up for such a feature!
Use the DateTimePicker, but set the following Properties
Format = Time
ShowUpDown = True
I think that's what you want.
If you want days in there, just set Format=Custom, and CustomFormat = "dd:HH:mm:ss"
If you're dead set on using this control for a 'time span', AND you can be sure your timespan is under 24 hours,
ReadOnly baseDate As DateTime = New DateTime(2000, 1, 1)
Dim myTimeSpan As New TimeSpan
''// set value
DateTimePicker1.Value = baseDate.Add(myTimeSpan)
''// retrieve value
myTimeSpan = DateTimePicker1.Value.Subtract(baseDate)
but then you have to be able to handle what happens with timespans greater than 24 hours or negative timespans

VB time input, a better way to do it?

Working on a handy program for my boss at work as a summer project in between semesters both to make life easier for her, and to practice and improve my skills. Intent is to allow her to quickly and easily calculate the hours and minutes the person worked. The form itself has a series of text boxes for the Clock in and Clock out time for each day that week. Currently it attempts to convert the txtbox text into a Date variable, then .Subtract()'s the start from the end and stores it in a rolling total variable which is displayed at the bottom of the form. I can't help but think there is a better way of going about doing this, and I'm absolutely certain that having the below block of code 21 times (7 days, 3 shifts) is inefficient.
Dim StartTime As Date
Dim EndTime As Date
Dim Worked As System.TimeSpan
Dim WorkedTotal As System.TimeSpan
If chkFirst.Checked = True Then
StartTime = CDate(txtMonStart.Text)
EndTime = CDate(txtMonEnd.Text)
EndTime = EndTime.AddHours(12)
Worked = EndTime.Subtract(StartTime)
lblMonWork.Text = Worked.ToString()
WorkedTotal += Worked
Currently it works, mostly. The user has to enter the ":" in the time input, and if the total exceeds 24 hours, it displays a day column (40 hour 12 min work week displays as 1.16:12). I'd like to eliminate both of these unplanned features and allow for my input validation to take place when the focus changes to another box. A changing bgcolor would indicate an invalid input.
Any bright ideas?
Instead of using TextBox for the time input, use DateTimePicker, just change the Format property to Time.
Handle the Validating event of the DateTimePicker and if it's invalid just set e.Cancel = False, that way they can't save changes unless it's valid. Or if you want them to be able to leave the DateTimePicker and just change the colour, just handle the ValueChanged event instead.
Regarding your code sample, I haven't really looked at the logic of it, but instead of having the same code 21 times, just move the code into a separate function and then call that function with the start and end times as parameters and it can return the workedtime as it's return value.
I'm not sure what your UI looks like but if you're repeating the start time and end time input control multiple times as well it might be worth looking at creating your own usercontrol that can contain one each of the start and end time controls, and then you could have the validation be inside that user control so you don't have to have lots of event handlers etc.