How to select all days in week when week number is clicked in MonthCalendarAdv control
Categories: DotNetBar for Windows Forms, Edit Controls, How To
To select all week days when week number in MonthCalendarAdv is clicked handle ItemClick event and use following code:
C#
private void monthCalendarAdv1_ItemClick(object sender, EventArgs e) { DayLabel label = sender as DayLabel; // If day label is clicked and label is used to display week of year if (label != null && label.IsWeekOfYear) { // Ensure that multi-selection is enabled monthCalendarAdv1.MultiSelect = true; // Get the month control that represents the month that label is assigned to // so complete date can be retrieved SingleMonthCalendar month = label.Parent as SingleMonthCalendar; // Parse the week of year from text int week = int.Parse(label.Text); // Get the month where week of year clicked resides DateTime d = month.DisplayMonth; // Forward date to the beginning of the week while (CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(d, month.WeekOfYearRule, month.FirstDayOfWeek) != week) d = d.AddDays(1); // Finally select the week... monthCalendarAdv1.SelectionStart = d; monthCalendarAdv1.SelectionEnd = d.AddDays(6); } }
VB
Private Sub monthCalendarAdv1_ItemClick(sender As Object, e As EventArgs) Dim label As DayLabel = TryCast(sender, DayLabel) ' If day label is clicked and label is used to display week of year If label IsNot Nothing AndAlso label.IsWeekOfYear Then ' Ensure that multi-selection is enabled monthCalendarAdv1.MultiSelect = True ' Get the month control that represents the month that label is assigned to ' so complete date can be retrieved Dim month As SingleMonthCalendar = TryCast(label.Parent, SingleMonthCalendar) ' Parse the week of year from text Dim week As Integer = Integer.Parse(label.Text) ' Get the month where week of year clicked resides Dim d As DateTime = month.DisplayMonth ' Forward date to the beginning of the week While CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(d, month.WeekOfYearRule, month.FirstDayOfWeek) <> week d = d.AddDays(1) End While ' Finally select the week... monthCalendarAdv1.SelectionStart = d monthCalendarAdv1.SelectionEnd = d.AddDays(6) End If End Sub
Related posts:
Leave a Reply!