/// <summary>If changing the used calendar, then this method reassigns the selection mode to set the correct selection range.</summary> private void ReAssignSelectionMode() { this.SelectionRange = null; MonthCalendarSelectionMode selMode = this.daySelectionMode; this.daySelectionMode = MonthCalendarSelectionMode.Manual; this.SelectionMode = selMode; }
/// <summary> /// Sets the selection range for the specified <see cref="MonthCalendarSelectionMode"/>. /// </summary> /// <param name="selMode"> /// The <see cref="MonthCalendarSelectionMode"/> value to set the selection range for. /// </param> private void SetSelectionRange(MonthCalendarSelectionMode selMode) { switch (selMode) { case MonthCalendarSelectionMode.Day: { this.selectionEnd = this.selectionStart; break; } case MonthCalendarSelectionMode.WorkWeek: case MonthCalendarSelectionMode.FullWeek: { MonthCalendarDate dt = new MonthCalendarDate(this.CultureCalendar, this.selectionStart).GetFirstDayInWeek( this._formatProvider); this.selectionStart = dt.Date; this.selectionEnd = dt.AddDays(6).Date; break; } case MonthCalendarSelectionMode.Month: { MonthCalendarDate dt = new MonthCalendarDate(this.CultureCalendar, this.selectionStart).FirstOfMonth; this.selectionStart = dt.Date; this.selectionEnd = dt.AddMonths(1).AddDays(-1).Date; break; } } }
/// <summary> /// Initializes a new instance of the <see cref="MonthCalendar"/> class. /// </summary> public MonthCalendar() { SetStyle( ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.Selectable | ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.SupportsTransparentBackColor, true); // initialize menus this.InitializeComponent(); this.extendSelection = false; this.showFooter = true; this.showWeekHeader = true; this.mouseLocation = Point.Empty; this.yearSelected = DateTime.MinValue; this.monthSelected = DateTime.MinValue; this.selectionStart = DateTime.Today; this.selectionEnd = DateTime.Today; this.currentHitType = MonthCalendarHitType.None; this.boldedDates = new List<DateTime>(); this.boldDatesCollection = new BoldedDatesCollection(); this.boldDateCategoryCollection = new BoldedDateCategoryCollection(this); this.currentMoveBounds = Rectangle.Empty; this.mouseMoveFlags = new MonthCalendarMouseMoveFlags(); this.selectionRanges = new List<SelectionRange>(); this.daySelectionMode = MonthCalendarSelectionMode.Manual; this.nonWorkDays = CalendarDayOfWeek.Saturday | CalendarDayOfWeek.Sunday; this.culture = CultureInfo.CurrentUICulture; this.cultureCalendar = CultureInfo.CurrentUICulture.DateTimeFormat.Calendar; this.eraRanges = GetEraRanges(this.cultureCalendar); this.minDate = this.cultureCalendar.MinSupportedDateTime.Date < new DateTime(1900, 1, 1) ? new DateTime(1900, 1, 1) : this.cultureCalendar.MinSupportedDateTime.Date; this.maxDate = this.cultureCalendar.MaxSupportedDateTime.Date > new DateTime(9998, 12, 31) ? new DateTime(9998, 12, 31) : this.cultureCalendar.MaxSupportedDateTime.Date; this.formatProvider = new MonthCalendarFormatProvider(this.culture, null, this.culture.TextInfo.IsRightToLeft) { MonthCalendar = this }; this.renderer = new MonthCalendarRenderer(this); this.calendarDimensions = new Size(1, 1); this.headerFont = new Font("Segoe UI", 9f, FontStyle.Regular); this.footerFont = new Font("Arial", 9f, FontStyle.Bold); this.dayHeaderFont = new Font("Segoe UI", 8f, FontStyle.Regular); this.dayTextAlign = ContentAlignment.MiddleCenter; // update year menu this.UpdateYearMenu(DateTime.Today.Year); // set start date this.SetStartDate(new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1)); this.CalculateSize(true); }