I am creating a custom control that contains a list view with dates and times. I need to make sure that times do not overlap. I would like the control to determine if the overlap is occuring with the starting time or with the ending time and offer the user an adjusted time entry. Below is the code I'm using currently to check the date and time. How would I modify it to detect where the overlap is occuring.
Public Sub Add(ByVal StartTime As DateTime, ByVal EndTime As DateTime)
If Me.CheckTimes(StartTime, EndTime) = -1 Then
MsgBox("The starting time cannot occur before the ending time!")
Exit Sub
ElseIf Me.CheckTimes(StartTime, EndTime) = 0 Then
StartTime = Me.LatestTime(StartTime, EndTime)
If MsgBox("You are attempting to insert an overlapping time." & vbCrLf & " The starting time has been adjusted to " & StartTime & vbCrLf & "Continue inserting this time?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
Me.Add(StartTime, EndTime)
End If
Exit Sub
End If
Dim myItem As New ListViewItem
myItem.Text = StartTime
myItem.SubItems.Add(EndTime)
myItem.SubItems.Add(FormatNumber(DateDiff(DateInte rval.Minute, StartTime, EndTime) / 60, 2))
Me.Times.Items.Add(myItem)
TotalHours.Text = FormatNumber(Me.CalculateHours) & " hrs."
End Sub
Private Function CheckTimes(ByVal StartTime as datetime, ByVal EndTime as datetime) As Integer
If StartTime > EndTime Then
Return -1
End If
For Each item As ListViewItem In Me.Times.Items
Dim testEnd As DateTime = item.SubItems(1).Text
Dim testStart As DateTime = item.Text
If StartTime < testEnd And EndTime > testStart Then
Return 0
End If
Next
Return 1
End Function
Thanks for any input!