Question Weekending only dates in a datetimepicker or a combobox

divjoy

Well-known member
Joined
Aug 25, 2013
Messages
159
Programming Experience
1-3
Hi Guys, Im using vb.net 2010.

I am looking for a way to populate a combox or restrict a datetimepicker with/to a list of Week Ending dates only that are all Sundays !
Any advice how to do this via code.

I could of course create a table but I don't want to keep maintaining a table. But would rather use code to produce dates from the current system date timetodays date to create a list going back to the 30/3/14 ( a Sunday).

Any suggestions...
 
Dim sundays As New List(Of Date)
Dim currentDate = Date.Today
Dim endDate = #3/30/2014#

'Find the most recent Sunday.
While currentDate.DayOfWeek <> DayOfWeek.Sunday

    'Go back one day.
    currentDate = currentDate.AddDays(-1)
End While

'Find all Sundays back to the end date.
While currentDate >= endDate
    sundays.Add(currentDate)

    'Go back one week to the previous Sunday.
    currentDate = currentDate.AddDays(-7)
End While
That will create a list of dates of Sundays between today and the specified end date inclusive. The list will be in descending order so, if you want ascending, you can use Insert instead of Add or call Sort at the end.
 
That is brilliant, tbank you.

I know I can assign the list
to a combo box, can I use the list to restrict a datetimepicker ?
 
can I use the list to restrict a datetimepicker ?

No you can't. You can only specify the start and end of a valid range in a DTP, not pick and choose dates within a range. You could use a DTP and let the user pick any date and then automatically change it to the Sunday for the same week. You could also use a MonthCalendar and do something similar while also highlighting the Sundays.
 
Back
Top