Click here to Skip to main content
15,894,896 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an application where users can schedule service. I have been able to limit the selection to weekdays only, but I'm having trouble limiting the time selection as well?
I'm trying to keep the service window between 8a-4p local time.

What I have tried:

VB
Private Sub DateTimePick_CloseUp(ByVal sender As Object, ByVal e As System.EventArgs) Handles DateTimePick.ValueChanged
    If (DateTimePick.Value.DayOfWeek = DayOfWeek.Saturday) Or (DateTimePick.Value.DayOfWeek = DayOfWeek.Sunday) Then

        'Now just add the right amount of days to make it Monday
        Select Case DateTimePick.Value.DayOfWeek
            Case DayOfWeek.Saturday
                DateTimePick.Value = DateTimePick.Value.AddDays(2)
            Case DayOfWeek.Sunday
                DateTimePick.Value = DateTimePick.Value.AddDays(1)
        End Select

        MsgBox("We're sorry, we are not currently scheduling maintenance for Saturday or Sunday. The following Monday, " &
               DateTimePick.Value.ToShortDateString & ", has been selected.",
               MsgBoxStyle.Exclamation, "Invalid selection")
    End If
End Sub
Posted
Updated 23-Jun-20 15:38pm
Comments
Maciej Los 22-Jun-20 13:15pm    
And you want to limit time in the same control?
Kyle_Forward 22-Jun-20 15:29pm    
Correct. The users should only be able to schedule "M-F - 8a-4p"
Richard MacCutchan 22-Jun-20 15:39pm    
Check the day is correct, and then check the time starts after 8 and ends before 4. What is the problem?

Kyle_Forward wrote:
...The users should only be able to schedule "M-F - 8a-4p"


So, i'd suggest to create BusinessDay class:

VB
Public Class BusinessDay
	
	Private dt As DateTime = DateTime.Now
	
	Public Sub New(initialDate As DateTime)
		Init(initialDate)
	End Sub
	
	Private Sub Init(selectedDT As DateTime)
		dt = selectedDT
		Dim daysToAdd As Integer = If(dt.DayOfWeek = DayOfWeek.Saturday, 2, If(dt.DayOfWeek = DayOfWeek.Sunday, 1, 0))
		dt = dt.AddDays(daysToAdd) 'saturday/sunday => monday
		Dim minDate As DateTime  = dt.AddHours(-dt.Hour+8).AddMinutes(-dt.Minute) 'selected date - 8:00 AM
		Dim maxDate As DateTime  = minDate.AddHours(8) 'selected date - 4:00 PM
		
		If dt> maxDate Or dt<minDate
			Throw New Exception($"Selected date {dt.ToString("yyyy-MM-dd HH:mm")} is out out working hours (8AM - 4PM)!")
		End If

	End Sub
	
	Public Overrides Function ToString() As String
		Return dt.ToString("yyyy-MM-dd HH:mm")
	End Function

End Class


Usage:
VB.NET
Dim selectedDT As DateTime  = DateTimePick.Value
Dim bd As BusinessDay

Try
    bd = New BusinessDay(selectedDT)
Catch Ex As Exception
    MessageBox.Show(Ex.Message)
End Try
'bd is out of working hours - exit sub!
If bd Is Nothing Then
    Exit Sub
End If
'everything is OK, so you can proceed
 
Share this answer
 
Comments
Patrice T 23-Jun-20 4:12am    
+5
Maciej Los 23-Jun-20 4:20am    
Thank you very much!
Quote:
Restrict users from selecting time outside of business hours

Good start, but your design is flawed, you forgot to handle holidays (as days closed in the week).
Independence day is the 4th of July, and is closed, no matter the day of week. So, adding test for the date is a good idea.
And Easter day date changes every year, somewhere, you need to be able to list those days.
 
Share this answer
 
Comments
Maciej Los 23-Jun-20 4:21am    
Good point!
Patrice T 23-Jun-20 6:27am    
Thank you too
Additional to the Solution from Maciej AND ALSO the Solution (Suggestion) from Patrice I perhaps would go this way :
- Create your own myDateTimePicker, which inherits DateTimePicker.
- Add the Properties for the Limits you want to have
- Perhaps change or customize the Design
- Override the OnValueChanged-Method to do the Limitation and the Reaction inside the (new) Control - perhaps with the Code suggested by Maciej

Do you need a Code-Sample for this ?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900