views:

676

answers:

1

I'm looking for a way to use two DateTimePicker controls to select a range of hours and minutes within one day. That is, to basically select a 'starting time' and 'ending time'.

I started with two DateTimePicker controls with the custom format of h:mm tt. I tried adding (on ValueChanged), a check that made the selected 'starting time' will become the 'ending times' minimum and vice versa, the 'ending time' becoming the 'starting times' maximum (sorry its hard to explain, but thanks for your patience!)

However, that approach didn't seem to work -- I was still able to select 11.00 AM on the 'ending time' and 1.00 PM on the 'starting time'. I have a feeling that the current day of the timestamp has something to do with it (e.g. the starting time is of the previous day, and therefore is less).

I thought about changing to a combo box of preset half hour intervals throughout the day; however the minutes need to be more flexible than that...

Any thoughts?

UPDATE:
Dan's comment below answered this problem... This is the code I ended up with for reference;

Private Sub DateTimePicker1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker1.ValueChanged

    DateTimePicker2.MinDate = Date.Today + DateTimePicker1.Value.TimeOfDay

End Sub

Private Sub DateTimePicker2_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker2.ValueChanged

    DateTimePicker1.MaxDate = Date.Today + DateTimePicker2.Value.TimeOfDay

End Sub

DateTimePIcker1 being the lower bound, DateTImePIcker2 being the upper bound, both using "h:mm tt" as a custom datetime format.

Storing these preferences will now just use the Value.TimeOfDay (and be stored as a timespan), then on retrieval the following load process occurs;

DateTimePicker1.Value = Date.Today + StoredTimeLowerBound
DateTimePicker2.Value = Date.Today + StoredTimeUpperBound

This will automatically call the ValueChanged events handled above and restore the selected values.

Dan, thanks for your help :)

A: 

This will make sure both of your times are from today:

Dim startTime As Date = Date.Today + DateTimePicker1.Value.TimeOfDay
Dim endTime As Date = Date.Today + DateTimePicker2.Value.TimeOfDay

Then you can write If endTime < startTime Then endTime = startTime or whatever you want.

Lastly, just put your modified Date values back into the DateTimePicker controls:

DateTimePicker1.Value = startTime
DateTimePicker2.Value = endTime

EDIT: For a range of hours you want a TimeSpan object, which is automatically the result of addition/subtraction of DateTime objects:

Dim range As TimeSpan - endTime - startTime
Dim numHours As Double = range.TotalHours
Dan Tao
Thanks for your reply. I am not worried about what day the values are from, it was just a thought as to why what I wanted didn't quite work.I guess I could reword to that I need to select a range of hours using start and end time (as h:MM tt) in DateTimePickers with validation logic.
Shadow
@Shadow: See my edit. Is this what you're getting at? Not even close?
Dan Tao
I got access to my code again and tested out the first statements you provided -- they worked like a charm! The Value.TimeOfDay really helped things along :).
Shadow