Пример #1
0
 /// <summary>
 /// Determines if the specified <paramref name="day"/> is a valid day in regard to the <paramref name="date"/> value.
 /// </summary>
 /// <param name="day">The day value.</param>
 /// <param name="date">The year and month value.</param>
 /// <param name="cal">The calendar to use.</param>
 /// <returns>true if it's a valid day; false otherwise.</returns>
 private static bool IsValidDay(int day, DateTime date, Calendar cal)
 {
     return(day >= 1 && day <= DateMethods.GetDaysInMonth(new MonthCalendarDate(cal, date)));
 }
Пример #2
0
        /// <summary>
        /// Processes a dialog key.
        /// </summary>
        /// <param name="keyData">One of the <see cref="System.Windows.Forms.Keys"/> values that represents the key to process.</param>
        /// <returns>true if the key was processed by the control; otherwise, false.</returns>
        protected override bool ProcessDialogKey(Keys keyData)
        {
            if (keyData == Keys.Left || keyData == Keys.Right)
            {
                this.SetDatePart(keyData == Keys.Left ? this.RightToLeft == RightToLeft.No : this.RightToLeft == RightToLeft.Yes);

                return(true);
            }

            Calendar cal = this.datePicker.Picker.CultureCalendar;

            MonthCalendarDate dt = new MonthCalendarDate(cal, this.currentDate);

            DateTime date = this.Date;

            if (keyData == Keys.Up || keyData == Keys.Down)
            {
                bool up = keyData == Keys.Up;

                switch (this.selectedPart)
                {
                case SelectedDatePart.Day:
                {
                    int day = dt.Day + (up ? 1 : -1);

                    int daysInMonth = DateMethods.GetDaysInMonth(dt);

                    if (day > daysInMonth)
                    {
                        day = 1;
                    }
                    else if (day < 1)
                    {
                        day = daysInMonth;
                    }

                    date = new DateTime(dt.Year, dt.Month, day, cal);

                    break;
                }

                case SelectedDatePart.Month:
                {
                    int day = dt.Day;

                    int month = dt.Month + (up ? 1 : -1);

                    int monthsInYear = cal.GetMonthsInYear(dt.Year);

                    if (month > monthsInYear)
                    {
                        month = 1;
                    }
                    else if (month < 1)
                    {
                        month = monthsInYear;
                    }

                    DateTime newDate = new DateTime(dt.Year, month, 1, cal);

                    dt = new MonthCalendarDate(cal, newDate);

                    int daysInMonth = DateMethods.GetDaysInMonth(dt);

                    newDate = daysInMonth < day?cal.AddDays(newDate, daysInMonth - 1) : cal.AddDays(newDate, day - 1);

                    date = newDate;

                    break;
                }

                case SelectedDatePart.Year:
                {
                    int year    = dt.Year + (up ? 1 : -1);
                    int minYear = cal.GetYear(this.MinDate);
                    int maxYear = cal.GetYear(this.MaxDate);

                    year = Math.Max(minYear, Math.Min(year, maxYear));

                    int yearDiff = year - dt.Year;

                    date = cal.AddYears(this.currentDate, yearDiff);

                    break;
                }
                }

                this.Date = date < this.MinDate ? this.MinDate : (date > this.MaxDate ? this.MaxDate : date);

                this.Refresh();

                return(true);
            }

            if (keyData == Keys.Home || keyData == Keys.End)
            {
                bool first = keyData == Keys.Home;

                switch (this.selectedPart)
                {
                case SelectedDatePart.Day:
                {
                    date = first ? new DateTime(dt.Year, dt.Month, 1, cal)
                        : new DateTime(dt.Year, dt.Month, DateMethods.GetDaysInMonth(dt), cal);

                    break;
                }

                case SelectedDatePart.Month:
                {
                    int day = dt.Day;

                    date = first ? new DateTime(dt.Year, 1, 1, cal)
                        : new DateTime(dt.Year, cal.GetMonthsInYear(dt.Year), 1, cal);

                    int daysInMonth = DateMethods.GetDaysInMonth(dt);

                    date = day > daysInMonth?cal.AddDays(date, daysInMonth - 1)
                               : cal.AddDays(date, day - 1);

                    break;
                }

                case SelectedDatePart.Year:
                {
                    date = first ? this.MinDate.Date : this.MaxDate.Date;

                    break;
                }
                }

                this.Date = date < this.MinDate ? this.MinDate : (date > this.MaxDate ? this.MaxDate : date);

                this.Refresh();

                return(true);
            }

            if (keyData == Keys.Space && !this.inEditMode)
            {
                this.datePicker.SwitchPickerState();

                return(true);
            }

            return(base.ProcessDialogKey(keyData));
        }