Tuesday, March 27, 2012

DaysOfWeek CheckBoxList

I have a checkboxlist of days of the week that I wish to be checked if
they are in the current schedule.
Is there a neater way of accomplishing this than the code I have at
present?
Dim recurrence As New WeeklyRecurrence
recurrence = CType(schedule.Definition.Item, WeeklyRecurrence)
If recurrence.DaysOfWeek.Sunday Then
cblScheduleWeeklyDays.Items.FindByValue(0).Selected = True
End If
If recurrence.DaysOfWeek.Monday Then
cblScheduleWeeklyDays.Items.FindByValue(1).Selected = True
End If
If recurrence.DaysOfWeek.Tuesday Then
cblScheduleWeeklyDays.Items.FindByValue(2).Selected = True
End If
If recurrence.DaysOfWeek.Wednesday Then
cblScheduleWeeklyDays.Items.FindByValue(3).Selected = True
End If
If recurrence.DaysOfWeek.Thursday Then
cblScheduleWeeklyDays.Items.FindByValue(4).Selected = True
End If
If recurrence.DaysOfWeek.Friday Then
cblScheduleWeeklyDays.Items.FindByValue(5).Selected = True
End If
If recurrence.DaysOfWeek.Saturday Then
cblScheduleWeeklyDays.Items.FindByValue(6).Selected = True
End IfObviously this would be better
cblScheduleWeeklyDays.Items.FindByValue(0).Selected =recurrence.DaysOfWeek.Sunday
cblScheduleWeeklyDays.Items.FindByValue(1).Selected =recurrence.DaysOfWeek.Monday
cblScheduleWeeklyDays.Items.FindByValue(2).Selected =recurrence.DaysOfWeek.Tuesday
cblScheduleWeeklyDays.Items.FindByValue(3).Selected =recurrence.DaysOfWeek.Wednesday
cblScheduleWeeklyDays.Items.FindByValue(4).Selected =recurrence.DaysOfWeek.Thursday
cblScheduleWeeklyDays.Items.FindByValue(5).Selected =recurrence.DaysOfWeek.Friday
cblScheduleWeeklyDays.Items.FindByValue(6).Selected =recurrence.DaysOfWeek.Saturday
Not very elegant I know, but less code.
I would prefer a for each ... construct

No comments:

Post a Comment