-1

I have a program that controls a relay board via RS232, the program allows multiple schedules to be setup so that these relays are turned on/off as the user needs.

I am trying to use the monthCalendar control to display a monthly view of what relays are on/off by date.

When the monthCalendar control month is scrolled forwards or back, I would like to iterate all of the dates shown on the control but I am not finding an enumerable member of the control to do this. Perhaps I am going about this in the wrong way, and am open to suggestions as to how to accomplish this.

While iterating the dates, each date is passed into a class member which returns true of false, based on this result I will add an entry to a grid or listbox.

Here is a rough example of what I would like to do:

  foreach (DateTime date in monthCalendar1.*"Dates"*)
  {
      if (Scheduler.TurnOn(date))
      {
          Console.WriteLine("Would turn on: " + date.ToString());
      }
  }
CodeCaster
  • 147,647
  • 23
  • 218
  • 272

1 Answers1

1

MonthCalendar is a BYOB class. You can easily bring the flavor you like with an extension method:

public static class Extensions {
    public static IEnumerable<DateTime> Dates(this MonthCalendar cal) {
        DateTime day = cal.SelectionStart;
        do {
            yield return day;
            day = day.AddDays(1);
        } while (day <= cal.SelectionEnd);
    }
}

So you can write:

foreach (var day in monthCalendar1.Dates()) {
    // etc...
}
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536