private void SyncMonths()
{
// We need the today header if we show the today button or a button spec
this[0].Visible = _showToday || (_buttonSpecs.Count > 0);
this[0].Enabled = Enabled;
_drawToday.Visible = _showToday;
// How many month children instances do we need?
int months = Months;
// Do we need to create more month view?
if (Count < (months + 1))
{
for (int i = Count - 1; i < months; i++)
Add(new ViewDrawMonth(_calendar, this, _redirector, _needPaintDelegate));
}
else if (Count > (months + 1))
{
// Remove excess month view instances
for (int i = Count - 1; i > months; i--)
{
this[i].Dispose();
this.RemoveAt(i);
}
}
// Is there a change in the selection range?
if ((_oldSelectionStart != _calendar.SelectionStart) ||
(_oldSelectionEnd != _calendar.SelectionEnd) ||
(_oldFocusDay != FocusDay) ||
_firstTimeSync)
{
_firstTimeSync = false;
_oldSelectionStart = _calendar.SelectionStart;
_oldSelectionEnd = _calendar.SelectionEnd;
_oldFocusDay = FocusDay;
// If we have a day with the focus
if (FocusDay != null)
{
// If focus day is before the first month
if (FocusDay.Value < _displayMonth)
_displayMonth = new DateTime(FocusDay.Value.Year, FocusDay.Value.Month, 1);
else
{
// If focus day is after the last month
DateTime endDate = _displayMonth.AddMonths(months);
if (FocusDay.Value >= endDate)
_displayMonth = new DateTime(FocusDay.Value.Year, FocusDay.Value.Month, 1).AddMonths(-(months - 1));
}
}
else
{
// Bring the selection into the display range
DateTime endMonth = _displayMonth.AddMonths(months - 1);
DateTime oldSelEndDate = _oldSelectionEnd.Date;
DateTime oldSelEndMonth = new DateTime(oldSelEndDate.Year, oldSelEndDate.Month, 1);
if (oldSelEndMonth >= endMonth)
_displayMonth = oldSelEndMonth.AddMonths(-(months - 1));
if (_oldSelectionStart < _displayMonth)
_displayMonth = new DateTime(_calendar.SelectionStart.Year, _calendar.SelectionStart.Month, 1);
}
}
DateTime currentMonth = _displayMonth;
// Inform each view which month it should be drawing
for (int i = 1; i < Count; i++)
{
ViewDrawMonth viewMonth = (ViewDrawMonth)this[i];
viewMonth.Enabled = Enabled;
viewMonth.Month = currentMonth;
viewMonth.FirstMonth = (i == 1);
viewMonth.LastMonth = (i == (Count - 1));
viewMonth.UpdateButtons((i == 1), ((i - 1) == (_calendar.CalendarDimensions.Width - 1)));
// Move forward to next month
currentMonth = currentMonth.AddMonths(1);
}
}