Highlight entire week in MonthCalendar Control

  • Thread starter Thread starter Randy
  • Start date Start date
R

Randy

Based on the date that the user selects, I'm trying to make the
MonthCalendar control highlight the entire week. In my case, the week
is from Monday - Sunday. I thought that using AddBoldedDate along
with selectionrange.start and .end would accomplish this, but it
doesn't.

Here is my code:

Private Sub MonthCalendar1_DateChanged(ByVal sender As System.Object,
ByVal e As System.Windows.Forms.DateRangeEventArgs) Handles
MonthCalendar1.DateChanged
Dim dateToday As Date = MonthCalendar1.SelectionRange.Start
Dim LastMonday As Date
Select Case dateToday.DayOfWeek
Case DayOfWeek.Monday
LastMonday = MonthCalendar1.SelectionRange.Start
Case DayOfWeek.Tuesday
LastMonday =
MonthCalendar1.SelectionRange.Start.AddDays(-1)
Case DayOfWeek.Wednesday
LastMonday =
MonthCalendar1.SelectionRange.Start.AddDays(-2)
Case DayOfWeek.Thursday
LastMonday =
MonthCalendar1.SelectionRange.Start.AddDays(-3)
Case DayOfWeek.Friday
LastMonday =
MonthCalendar1.SelectionRange.Start.AddDays(-4)
Case DayOfWeek.Saturday
LastMonday =
MonthCalendar1.SelectionRange.Start.AddDays(-5)
Case DayOfWeek.Sunday
LastMonday =
MonthCalendar1.SelectionRange.Start.AddDays(-6)
End Select

With MonthCalendar1
.SelectionRange.Start = LastMonday
.SelectionRange.End = LastMonday.AddDays(7)
for x = 0 to 6
.AddBoldedDate(LastMonday.AddDays(x))
next
End With
End Sub

Thanks,
Randy
 
I was looking to solve the same problem when I found your post. This
works for me:

Private Sub dtPick1_DateSelected(ByVal sender As System.Object,
ByVal e As System.Windows.Forms.DateRangeEventArgs) Handles
dtPick1.DateSelected
Dim i As Integer = CInt(dtPick1.SelectionStart.DayOfWeek)
Dim d As Date = dtPick1.SelectionStart
dtPick1.SelectionStart = d.AddDays(1 - i)
dtPick1.SelectionEnd = d.AddDays(7 - i)
End Sub

Note that I'm triggering off the DateSelected event, not DateChanged,
that way it doesn't re-trigger itself.
The seed values in the AddDays method make it Monday first day of
week. Otherewise use 0 and 6.
Enums always have an integer value behind, so you can avoid the Select
Case language.

Bob Graham
 
Actually I've found a flaw in the case where a user selects a sunday,
it goes to the next week, but that should be easy to fix with an IIF
or whatever.

Sayonnara, Bob
 
Another flaw!!! Works pretty well in Vista but causes some display
corruption in XP...
 
That works perfectly.

For anybody who happens upon this post, the major difference (besides
that Bob's code is much more efficient) seems to be that I was using
selectionrange.start (and end) where Bob is using selectionstart and
selectionend.

Thanks, Bob!
Randy
 
Back
Top