Reputation: 51
I recently needed to use a MonthCalendar for a project, but it looks like this control is really limited and I can't do what I want with it. So if you could give me some tips, it would be really helpful.
So, what I want to do is to select multiple dates, and then when I click on a button, these dates are saved in an array and disabled on the calendar(or at least, their cell background become red).
What I already done is to allow multiple selection (MaxSelectionCount = 31) And I wrote some lines to get the selected days :
Dim nbrJours As Integer = MonthCalendar1.SelectionRange.End.Day - MonthCalendar1.SelectionRange.Start.Day
For jour As Integer = 0 To nbrJours
MsgBox(jour + MonthCalendar1.SelectionRange.Start.Day & "/" & MonthCalendar1.SelectionRange.Start.Month & "/" & MonthCalendar1.SelectionRange.Start.Year)
Next
Well, it's not really clean but it works, I just have to save these into an array after converting all the strings into dates I guess.
So, we suppose that all my dates are in an array, how can I disable the dates contained in the array in my MonthCalendar?
Thanks for reading, and sorry for my bad english, it's not my native language.
Upvotes: 0
Views: 2307
Reputation: 51
Thanks again Denise, i modified the code you wrote to this one so it can be used with arrays
Private Sub MonthCalendar1_DateChanged(sender As System.Object, e As System.Windows.Forms.DateRangeEventArgs) Handles MonthCalendar1.DateChanged
Dim day As DateTime
'Dim disabledDay As DateTime
Dim defaultDay As DateTime
Dim disabledDay = {New DateTime(2015, 1, 8), New DateTime(2015, 1, 9)} 'you might actually have a list of days
defaultDay = New DateTime(2015, 1, 1)
day = e.Start
While (day <= e.End)
For Each DisabledDate As Date In disabledDay
If day = DisabledDate Then 'if you have a list, you need a linq statement or a double loop
MsgBox("Can't select that day")
MonthCalendar1.AddBoldedDate(DisabledDate)
MonthCalendar1.UpdateBoldedDates()
MonthCalendar1.SetSelectionRange(defaultDay, defaultDay)
Exit Sub
End If
Next
day = day.AddDays(1)
End While
End Sub
I'm not familiar with the "e" thing, but I'll document myself.
Upvotes: 0
Reputation: 2416
Natively the control only supports making dates bold or not bold. You need a different calendar control to highlight dates in red.
You can however disable the dates by handling the DateChanged event:
Private Sub MonthCalendar1_DateChanged(sender As System.Object, e As System.Windows.Forms.DateRangeEventArgs) Handles MonthCalendar1.DateChanged
Dim day As DateTime
Dim disabledDay As DateTime
Dim defaultDay As DateTime
disabledDay = New DateTime(2015, 1, 8) 'you might actually have a list of days
defaultDay = New DateTime(2015, 1, 1)
day = e.Start
While (day <= e.End)
If day = disabledDay Then 'if you have a list, you need a linq statement or a double loop
MsgBox("Can't select that day")
MonthCalendar1.AddBoldedDate(disabledDay)
MonthCalendar1.UpdateBoldedDates()
MonthCalendar1.SetSelectionRange(defaultDay, defaultDay)
Exit Sub
End If
day = day.AddDays(1)
End While
End Sub
Upvotes: 1