diff options
author | Alexis Christoforides <alexis@thenull.net> | 2017-10-16 18:03:00 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-10-16 18:03:00 +0300 |
commit | 23d2615f3da106a86a8e0bd59ae66b18b3adb930 (patch) | |
tree | 0f9d0a8b8636b66ba98ed8bca615635fea67ed1c /mcs | |
parent | 3a8e4fcfd448c0b67bed69af14882bcd35308696 (diff) | |
parent | 1dcaf8fddce7c04f5caef0a8f0c1815006a5950d (diff) |
Merge pull request #5782 from YuriyGS/bug60115
[System.Windows.Forms] In MonthCalendar added validation on acceptable date interval. Fixes #60115
Diffstat (limited to 'mcs')
-rw-r--r-- | mcs/class/System.Windows.Forms/System.Windows.Forms/MonthCalendar.cs | 75 |
1 files changed, 59 insertions, 16 deletions
diff --git a/mcs/class/System.Windows.Forms/System.Windows.Forms/MonthCalendar.cs b/mcs/class/System.Windows.Forms/System.Windows.Forms/MonthCalendar.cs index ca69ae7123f..e542440c01e 100644 --- a/mcs/class/System.Windows.Forms/System.Windows.Forms/MonthCalendar.cs +++ b/mcs/class/System.Windows.Forms/System.Windows.Forms/MonthCalendar.cs @@ -1029,11 +1029,15 @@ namespace System.Windows.Forms { // Handle arrow keys protected override bool IsInputKey (Keys keyData) { - switch (keyData) { + switch (keyData & ~Keys.Shift) { case Keys.Up: case Keys.Down: case Keys.Right: case Keys.Left: + case Keys.PageUp: + case Keys.PageDown: + case Keys.Home: + case Keys.End: return true; default: break; @@ -1299,15 +1303,23 @@ namespace System.Windows.Forms { internal DateTime CurrentMonth { set { // only interested in if the month (not actual date) has change - if (value < MinDate || value > MaxDate) { + if (value < new DateTime(MinDate.Year, MinDate.Month, 1) || value > MaxDate) { return; } if (value.Month != current_month.Month || value.Year != current_month.Year) { - this.SelectionRange = new SelectionRange( - this.SelectionStart.Add(value.Subtract(current_month)), - this.SelectionEnd.Add(value.Subtract(current_month))); + DateTime start = this.SelectionStart.Add(value.Subtract(current_month)); + if (start < MinDate) + start = MinDate; + else if (start > MaxDate) + start = MaxDate; + DateTime end = this.SelectionEnd.Add (value.Subtract (current_month)); + if (end < MinDate) + end = MinDate; + else if (end > MaxDate) + end = MaxDate; + this.SelectionRange = new SelectionRange (start, end); current_month = value; UpdateBoldedDates(); this.Invalidate(); @@ -1574,6 +1586,12 @@ namespace System.Windows.Forms { } } + // validate range + if (range.Start < MinDate) + range.Start = MinDate; + if (range.End > MaxDate) + range.End = MaxDate; + // Avoid re-setting SelectionRange to the same value and fire an extra DateChanged event if (range.Start != selection_range.Start || range.End != selection_range.End) SelectionRange = range; @@ -1581,6 +1599,8 @@ namespace System.Windows.Forms { // attempts to add the date to the selection without throwing exception private void SelectDate (DateTime date) { + if (date < MinDate || date > MaxDate) + return; // try and add the new date to the selction range SelectionRange range = null; if (is_shift_pressed || (click_state [0])) { @@ -1594,14 +1614,12 @@ namespace System.Windows.Forms { } } } else { - if (date >= MinDate && date <= MaxDate) { - range = new SelectionRange (date, date); - first_select_start_date = date; - } + range = new SelectionRange (date, date); + first_select_start_date = date; } // Only set if we re actually getting a different range (avoid an extra DateChanged event) - if (range != null && (range.Start != selection_range.Start || range.End != selection_range.End)) + if (range.Start != selection_range.Start || range.End != selection_range.End) SelectionRange = range; } @@ -1723,7 +1741,10 @@ namespace System.Windows.Forms { // called when today context menu is clicked private void TodayMenuItemClickHandler (object sender, EventArgs e) { - this.SetSelectionRange (DateTime.Now.Date, DateTime.Now.Date); + DateTime date = DateTime.Now.Date; + if (date < MinDate || date > MaxDate) + return; + this.SetSelectionRange (date, date); this.OnDateSelected (new DateRangeEventArgs (SelectionStart, SelectionEnd)); } @@ -2010,10 +2031,14 @@ namespace System.Windows.Forms { ShowYearUpDown = true; } break; - case HitArea.TodayLink: - this.SetSelectionRange (DateTime.Now.Date, DateTime.Now.Date); - this.OnDateSelected (new DateRangeEventArgs (SelectionStart, SelectionEnd)); + case HitArea.TodayLink: { + DateTime date = DateTime.Now.Date; + if (date >= MinDate && date <= MaxDate) { + this.SetSelectionRange (date, date); + this.OnDateSelected (new DateRangeEventArgs (SelectionStart, SelectionEnd)); + } break; + } default: this.is_previous_clicked = false; this.is_next_clicked = false; @@ -2055,9 +2080,13 @@ namespace System.Windows.Forms { if (date < first_select_start_date.AddDays ((MaxSelectionCount-1)*-1)) { date = first_select_start_date.AddDays ((MaxSelectionCount-1)*-1); } + if (date < MinDate) + date = MinDate; this.SetSelectionRange (date, first_select_start_date); } else { DateTime date = GetFirstDateInMonth (this.SelectionStart); + if (date < MinDate) + date = MinDate; this.SetSelectionRange (date, date); } e.Handled = true; @@ -2069,9 +2098,13 @@ namespace System.Windows.Forms { if (date > first_select_start_date.AddDays (MaxSelectionCount-1)) { date = first_select_start_date.AddDays (MaxSelectionCount-1); } + if (date > MaxDate) + date = MaxDate; this.SetSelectionRange (date, first_select_start_date); } else { DateTime date = GetLastDateInMonth (this.SelectionStart); + if (date > MaxDate) + date = MaxDate; this.SetSelectionRange (date, date); } e.Handled = true; @@ -2082,6 +2115,8 @@ namespace System.Windows.Forms { this.AddTimeToSelection (-1, false); } else { DateTime date = this.SelectionStart.AddMonths (-1); + if (date < MinDate) + date = MinDate; this.SetSelectionRange (date, date); } e.Handled = true; @@ -2092,6 +2127,8 @@ namespace System.Windows.Forms { this.AddTimeToSelection (1, false); } else { DateTime date = this.SelectionStart.AddMonths (1); + if (date > MaxDate) + date = MaxDate; this.SetSelectionRange (date, date); } e.Handled = true; @@ -2102,6 +2139,8 @@ namespace System.Windows.Forms { this.AddTimeToSelection (-7, true); } else { DateTime date = this.SelectionStart.AddDays (-7); + if (date < MinDate) + date = MinDate; this.SetSelectionRange (date, date); } e.Handled = true; @@ -2112,6 +2151,8 @@ namespace System.Windows.Forms { this.AddTimeToSelection (7, true); } else { DateTime date = this.SelectionStart.AddDays (7); + if (date > MaxDate) + date = MaxDate; this.SetSelectionRange (date, date); } e.Handled = true; @@ -2122,7 +2163,8 @@ namespace System.Windows.Forms { this.AddTimeToSelection (-1, true); } else { DateTime date = this.SelectionStart.AddDays (-1); - this.SetSelectionRange (date, date); + if (date >= MinDate) + this.SetSelectionRange (date, date); } e.Handled = true; break; @@ -2132,7 +2174,8 @@ namespace System.Windows.Forms { this.AddTimeToSelection (1, true); } else { DateTime date = this.SelectionStart.AddDays (1); - this.SetSelectionRange (date, date); + if (date <= MaxDate) + this.SetSelectionRange (date, date); } e.Handled = true; break; |