我最近需要为项目使用MonthCalendar,但看起来这个控件真的有限,我无法用它做我想做的事。所以,如果你能给我一些提示,那将非常有帮助。
所以,我想要做的是选择多个日期,然后当我点击一个按钮时,这些日期会保存在一个数组中并在日历上禁用(或者至少,它们的单元格背景变为红色)。 / p>
我已经做的是允许多项选择(MaxSelectionCount = 31) 我写了几行来获取所选日期:
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
嗯,它不是很干净但是它可以工作,我只需要将所有字符串转换成日期后将它们保存到数组中。
因此,我们假设我的所有日期都在一个数组中,如何在MonthCalendar中禁用数组中包含的日期?
感谢阅读,对不起我的英语不好,这不是我的母语。
答案 0 :(得分:1)
本机控件仅支持将日期设为粗体或非粗体。您需要一个不同的日历控件来突出显示红色的日期。
但是,您可以通过处理DateChanged事件来禁用日期:
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
答案 1 :(得分:0)
再次感谢Denise,我修改了您编写的代码,以便它可以与数组一起使用
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
我不熟悉&#34; e&#34;事情,但我会记录自己。