How to edit timespan values with an UpDown control using VB.NET? - 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

Related

Count down clock - getting the difference between two times

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

Passing and Viewing Combobox Text in VB

I have a program with two forms: Form1 and FocusPRG. On the FocusPRG form I have two comboboxes set to List, one that let's you pick a Start Time and one that let's you pick a Stop Time for a process. Right now all I want to do is have Form1 look at the selected times and write them to a log file but the log file just shows blank lines and not any of the actual text. Here is the code on Form1 I am using:
Dim StartTime As String
Dim StopTime As String
StartTime = FocusPRG.StartTimePicker.SelectedText
StopTime = FocusPRG.StopTimePicker.SelectedText
WriteLog(StartTime)
WriteLog(StopTime)
What am I missing?
The SelectedText property does not do what you think it does. It's not the text of the selected value. Just like in a TextBox, it's the text that the user has highlighted using either the mouse or keyboard. If you want the text displayed in the control then you want the Text property, again, just like a TextBox.
Use this:
Dim StartTime As String
Dim StopTime As String
StartTime = FocusPRG.StartTimePicker.SelectedItem.ToString()
StopTime = FocusPRG.StopTimePicker.SelectedItem.ToString()
WriteLog(StartTime)
WriteLog(StopTime)
That is to say, use the SelectedItem property of the ComboBox instead of the SelectedText property.
If you read the documentation of the two at MSDN, you can easily see the difference:
Definition of SelectedText says:
Gets or sets the text that is selected in the editable portion of a ComboBox.
Definition of SelectedItem says:
Gets or sets currently selected item in the ComboBox.
FocusPRG must be an instance of the form, not just a class name. When creating FocusPRG, do something like:
dim FocusPRG1 as new FocusPRG
FocusPRG1.Show()
Also use Text, not SelectedText to get the user's input.
StartTime = FocusPRG1.StartTimePicker.Text
StopTime = FocusPRG1.StopTimePicker.Text
Also, the question is a little confusing because you say the controls are combo boxes but they are call xxxTimePicker, which suggests a different type of control.

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.

How do I set a Calendar Object in VB.NET so that it has no date selected?

By default, the Calendar object in a VB.NET form will have 'todays date' selected.
How do I set it so that by default, it does not have a date selected whatsoever, until the user performs a click action?
Thanks,
C.
The usual method for clearing the date in a date control is to either set the value of the control to DateTime.MinValue.
Some third party controls store the value in an object and let you set the value to DBNull.Value or Nothing to indicate that no selection has been made.

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.