0

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.

aa .
  • 51
  • 6
  • possible duplicate of [How can I change the color of certain dates in the MonthCalendar control?](http://stackoverflow.com/questions/5048872/how-can-i-change-the-color-of-certain-dates-in-the-monthcalendar-control) – Matt Wilko Jan 07 '15 at 16:26
  • not really, i just want to find how to disable dates, only if there is no other solutions, then i want to change their colors – aa . Jan 07 '15 at 17:10

2 Answers2

1

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
Denise Skidmore
  • 2,286
  • 22
  • 51
  • Thanks, this works, I'll have to adapt it when i'll fully understand your code, but that's a good start. – aa . Jan 07 '15 at 17:05
0

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.

aa .
  • 51
  • 6