* UIDatePicker: 增加MaxDate,MinDate
This commit is contained in:
parent
5e3a387635
commit
f3f48d0de3
@ -336,6 +336,9 @@ namespace Sunny.UI
|
|||||||
base.SetDPIScale();
|
base.SetDPIScale();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public DateTime max = DateTime.MaxValue;
|
||||||
|
public DateTime min = DateTime.MinValue;
|
||||||
|
|
||||||
public void Translate()
|
public void Translate()
|
||||||
{
|
{
|
||||||
months.Clear();
|
months.Clear();
|
||||||
@ -357,6 +360,18 @@ namespace Sunny.UI
|
|||||||
{
|
{
|
||||||
if (TabControl.SelectedIndex > 0)
|
if (TabControl.SelectedIndex > 0)
|
||||||
{
|
{
|
||||||
|
if (TabControl.SelectedIndex == 2)
|
||||||
|
{
|
||||||
|
if (new DateTime(Year, Month, 1) > max) return;
|
||||||
|
if (new DateTime(Year, Month, 1).EndOfMonth() < min) return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (TabControl.SelectedIndex == 1)
|
||||||
|
{
|
||||||
|
if (Year < min.Year) return;
|
||||||
|
if (Year > max.Year) return;
|
||||||
|
}
|
||||||
|
|
||||||
TabControl.SelectedIndex--;
|
TabControl.SelectedIndex--;
|
||||||
activeDay = -1;
|
activeDay = -1;
|
||||||
}
|
}
|
||||||
@ -581,15 +596,21 @@ namespace Sunny.UI
|
|||||||
int left = width * (i % 4);
|
int left = width * (i % 4);
|
||||||
int top = height * (i / 4);
|
int top = height * (i / 4);
|
||||||
|
|
||||||
SizeF sf = e.Graphics.MeasureString(months[i], Font);
|
Color color = ForeColor;
|
||||||
if (i + 1 == Month)
|
if (new DateTime(Year, i + 1, 1) > max || new DateTime(Year, i + 1, 1).EndOfMonth() < min)
|
||||||
{
|
{
|
||||||
e.Graphics.DrawString(months[i], Font, PrimaryColor, left + (width - sf.Width) / 2, top + (height - sf.Height) / 2);
|
color = Color.DarkGray;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
e.Graphics.DrawString(months[i], Font, i == activeMonth ? PrimaryColor : ForeColor, left + (width - sf.Width) / 2, top + (height - sf.Height) / 2);
|
if (i + 1 == Month) color = PrimaryColor;
|
||||||
|
if (i + 1 == Month) color = PrimaryColor;
|
||||||
|
if (i == activeMonth) color = PrimaryColor;
|
||||||
|
if (i == activeMonth) color = PrimaryColor;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SizeF sf = e.Graphics.MeasureString(months[i], Font);
|
||||||
|
e.Graphics.DrawString(months[i], Font, color, left + (width - sf.Width) / 2, top + (height - sf.Height) / 2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -601,6 +622,10 @@ namespace Sunny.UI
|
|||||||
int y = e.Location.Y / height;
|
int y = e.Location.Y / height;
|
||||||
Month = x + y * 4 + 1;
|
Month = x + y * 4 + 1;
|
||||||
if (Month <= 0 || Month > 12) return;
|
if (Month <= 0 || Month > 12) return;
|
||||||
|
|
||||||
|
if (new DateTime(Year, Month, 1) > max) return;
|
||||||
|
if (new DateTime(Year, Month, 1).EndOfMonth() < min) return;
|
||||||
|
|
||||||
SetYearMonth(Year, Month);
|
SetYearMonth(Year, Month);
|
||||||
activeMonth = -1;
|
activeMonth = -1;
|
||||||
|
|
||||||
@ -625,6 +650,11 @@ namespace Sunny.UI
|
|||||||
int x = e.Location.X / width;
|
int x = e.Location.X / width;
|
||||||
int y = e.Location.Y / height;
|
int y = e.Location.Y / height;
|
||||||
int im = x + y * 4;
|
int im = x + y * 4;
|
||||||
|
|
||||||
|
if (im + 1 <= 0 || im + 1 > 12) return;
|
||||||
|
if (new DateTime(Year, im + 1, 1) > max) return;
|
||||||
|
if (new DateTime(Year, im + 1, 1).EndOfMonth() < min) return;
|
||||||
|
|
||||||
if (activeMonth != im)
|
if (activeMonth != im)
|
||||||
{
|
{
|
||||||
activeMonth = im;
|
activeMonth = im;
|
||||||
@ -647,9 +677,21 @@ namespace Sunny.UI
|
|||||||
|
|
||||||
SizeF sf = e.Graphics.MeasureString(years[i].ToString(), Font);
|
SizeF sf = e.Graphics.MeasureString(years[i].ToString(), Font);
|
||||||
Color color = (i == 0 || i == 11) ? Color.DarkGray : ForeColor;
|
Color color = (i == 0 || i == 11) ? Color.DarkGray : ForeColor;
|
||||||
|
if (years[i] < min.Year || years[i] > max.Year)
|
||||||
|
{
|
||||||
|
color = Color.DarkGray;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (years[i] == Year) color = PrimaryColor;
|
||||||
|
if (years[i] == Year) color = PrimaryColor;
|
||||||
|
if (i == activeYear) color = PrimaryColor;
|
||||||
|
if (i == activeYear) color = PrimaryColor;
|
||||||
|
}
|
||||||
|
|
||||||
if (years[i] != 10000)
|
if (years[i] != 10000)
|
||||||
{
|
{
|
||||||
e.Graphics.DrawString(years[i].ToString(), Font, (i == activeYear || years[i] == Year) ? PrimaryColor : color, left + (width - sf.Width) / 2, top + (height - sf.Height) / 2);
|
e.Graphics.DrawString(years[i].ToString(), Font, color, left + (width - sf.Width) / 2, top + (height - sf.Height) / 2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -661,6 +703,10 @@ namespace Sunny.UI
|
|||||||
int x = e.Location.X / width;
|
int x = e.Location.X / width;
|
||||||
int y = e.Location.Y / height;
|
int y = e.Location.Y / height;
|
||||||
int iy = x + y * 4;
|
int iy = x + y * 4;
|
||||||
|
|
||||||
|
if (years[iy] < min.Year) return;
|
||||||
|
if (years[iy] > max.Year) return;
|
||||||
|
|
||||||
if (activeYear != iy)
|
if (activeYear != iy)
|
||||||
{
|
{
|
||||||
activeYear = iy;
|
activeYear = iy;
|
||||||
@ -676,6 +722,9 @@ namespace Sunny.UI
|
|||||||
int y = e.Location.Y / height;
|
int y = e.Location.Y / height;
|
||||||
int iy = x + y * 4;
|
int iy = x + y * 4;
|
||||||
if (iy < 0 || iy >= 12) return;
|
if (iy < 0 || iy >= 12) return;
|
||||||
|
if (years[iy] < min.Year) return;
|
||||||
|
if (years[iy] > max.Year) return;
|
||||||
|
|
||||||
Year = years[iy] > 9999 ? 9999 : years[iy];
|
Year = years[iy] > 9999 ? 9999 : years[iy];
|
||||||
activeYear = -1;
|
activeYear = -1;
|
||||||
|
|
||||||
@ -723,6 +772,8 @@ namespace Sunny.UI
|
|||||||
sf = e.Graphics.MeasureString(days[i].Day.ToString(), Font);
|
sf = e.Graphics.MeasureString(days[i].Day.ToString(), Font);
|
||||||
Color color = (days[i].Month == Month) ? ForeColor : Color.DarkGray;
|
Color color = (days[i].Month == Month) ? ForeColor : Color.DarkGray;
|
||||||
color = (days[i].DateString() == date.DateString()) ? PrimaryColor : color;
|
color = (days[i].DateString() == date.DateString()) ? PrimaryColor : color;
|
||||||
|
if (days[i] < min) color = Color.DarkGray;
|
||||||
|
if (days[i] > max) color = Color.DarkGray;
|
||||||
|
|
||||||
if (!maxDrawer)
|
if (!maxDrawer)
|
||||||
{
|
{
|
||||||
@ -768,6 +819,17 @@ namespace Sunny.UI
|
|||||||
int x = e.Location.X / width;
|
int x = e.Location.X / width;
|
||||||
int y = (e.Location.Y - 30) / height;
|
int y = (e.Location.Y - 30) / height;
|
||||||
int iy = x + y * 7;
|
int iy = x + y * 7;
|
||||||
|
|
||||||
|
if (iy.InRange(0, days.Count))
|
||||||
|
{
|
||||||
|
if (days[iy] < min) return;
|
||||||
|
if (days[iy] > max) return;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
bool istoday = ShowToday && e.Location.Y > p3.Top + p3.Height - height && e.Location.X > p3.Left + width * 3;
|
bool istoday = ShowToday && e.Location.Y > p3.Top + p3.Height - height && e.Location.X > p3.Left + width * 3;
|
||||||
|
|
||||||
if (activeDay != iy || istoday != isToday)
|
if (activeDay != iy || istoday != isToday)
|
||||||
@ -787,12 +849,19 @@ namespace Sunny.UI
|
|||||||
int y = (e.Location.Y - 30) / height;
|
int y = (e.Location.Y - 30) / height;
|
||||||
int id = x + y * 7;
|
int id = x + y * 7;
|
||||||
if (id < 0 || id >= 42) return;
|
if (id < 0 || id >= 42) return;
|
||||||
date = days[id].Date;
|
|
||||||
|
|
||||||
if (ShowToday && e.Location.Y > p3.Height - height && e.Location.X > p3.Width - width * 4)
|
if (ShowToday && e.Location.Y > p3.Height - height && e.Location.X > p3.Width - width * 4)
|
||||||
{
|
{
|
||||||
|
if (DateTime.Now.Date < min) return;
|
||||||
|
if (DateTime.Now.Date > max) return;
|
||||||
date = DateTime.Now.Date;
|
date = DateTime.Now.Date;
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (days[id].Date < min) return;
|
||||||
|
if (days[id].Date > max) return;
|
||||||
|
date = days[id].Date;
|
||||||
|
}
|
||||||
|
|
||||||
DoValueChanged(this, Date);
|
DoValueChanged(this, Date);
|
||||||
Close();
|
Close();
|
||||||
|
@ -20,10 +20,12 @@
|
|||||||
* 2020-08-07: V2.2.7 可编辑输入,日期范围控制以防止出错
|
* 2020-08-07: V2.2.7 可编辑输入,日期范围控制以防止出错
|
||||||
* 2021-04-15: V3.0.3 增加ShowToday显示今日属性
|
* 2021-04-15: V3.0.3 增加ShowToday显示今日属性
|
||||||
* 2021-08-14: V3.0.6 增加可选择年、年月、年月日
|
* 2021-08-14: V3.0.6 增加可选择年、年月、年月日
|
||||||
|
* 2022-11-08: V3.2.8 增加MaxDate,MinDate
|
||||||
******************************************************************************/
|
******************************************************************************/
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.ComponentModel;
|
using System.ComponentModel;
|
||||||
|
using System.Globalization;
|
||||||
using System.Windows.Forms;
|
using System.Windows.Forms;
|
||||||
|
|
||||||
namespace Sunny.UI
|
namespace Sunny.UI
|
||||||
@ -46,6 +48,131 @@ namespace Sunny.UI
|
|||||||
CreateInstance();
|
CreateInstance();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private DateTime max = DateTime.MaxValue;
|
||||||
|
private DateTime min = DateTime.MinValue;
|
||||||
|
|
||||||
|
static internal DateTime EffectiveMaxDate(DateTime maxDate)
|
||||||
|
{
|
||||||
|
DateTime maxSupportedDate = DateTimePicker.MaximumDateTime;
|
||||||
|
if (maxDate > maxSupportedDate)
|
||||||
|
{
|
||||||
|
return maxSupportedDate;
|
||||||
|
}
|
||||||
|
return maxDate;
|
||||||
|
}
|
||||||
|
|
||||||
|
static internal DateTime EffectiveMinDate(DateTime minDate)
|
||||||
|
{
|
||||||
|
DateTime minSupportedDate = DateTimePicker.MinimumDateTime;
|
||||||
|
if (minDate < minSupportedDate)
|
||||||
|
{
|
||||||
|
return minSupportedDate;
|
||||||
|
}
|
||||||
|
return minDate;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
|
||||||
|
public static readonly DateTime MinDateTime = new DateTime(1753, 1, 1);
|
||||||
|
|
||||||
|
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
|
||||||
|
public static readonly DateTime MaxDateTime = new DateTime(9998, 12, 31);
|
||||||
|
|
||||||
|
[DefaultValue(typeof(DateTime), "9998/12/31")]
|
||||||
|
[Description("最大日期"), Category("SunnyUI")]
|
||||||
|
public DateTime MaxDate
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return EffectiveMaxDate(max);
|
||||||
|
}
|
||||||
|
set
|
||||||
|
{
|
||||||
|
if (value != max)
|
||||||
|
{
|
||||||
|
if (value < EffectiveMinDate(min))
|
||||||
|
{
|
||||||
|
value = EffectiveMinDate(min);
|
||||||
|
}
|
||||||
|
|
||||||
|
// If trying to set the maximum greater than MaxDateTime, throw.
|
||||||
|
if (value > MaximumDateTime)
|
||||||
|
{
|
||||||
|
value = MaximumDateTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
max = value;
|
||||||
|
|
||||||
|
//If Value (which was once valid) is suddenly greater than the max (since we just set it)
|
||||||
|
//then adjust this...
|
||||||
|
if (Value > max)
|
||||||
|
{
|
||||||
|
Value = max;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[DefaultValue(typeof(DateTime), "1753/1/1")]
|
||||||
|
[Description("最小日期"), Category("SunnyUI")]
|
||||||
|
public DateTime MinDate
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return EffectiveMinDate(min);
|
||||||
|
}
|
||||||
|
set
|
||||||
|
{
|
||||||
|
if (value != min)
|
||||||
|
{
|
||||||
|
if (value > EffectiveMaxDate(max))
|
||||||
|
{
|
||||||
|
value = EffectiveMaxDate(max);
|
||||||
|
}
|
||||||
|
|
||||||
|
// If trying to set the minimum less than MinimumDateTime, throw.
|
||||||
|
if (value < MinimumDateTime)
|
||||||
|
{
|
||||||
|
value = MinimumDateTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
min = value;
|
||||||
|
|
||||||
|
//If Value (which was once valid) is suddenly less than the min (since we just set it)
|
||||||
|
//then adjust this...
|
||||||
|
if (Value < min)
|
||||||
|
{
|
||||||
|
Value = min;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static DateTime MaximumDateTime
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
DateTime maxSupportedDateTime = CultureInfo.CurrentCulture.Calendar.MaxSupportedDateTime;
|
||||||
|
if (maxSupportedDateTime.Year > MaxDateTime.Year)
|
||||||
|
{
|
||||||
|
return MaxDateTime;
|
||||||
|
}
|
||||||
|
return maxSupportedDateTime;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static DateTime MinimumDateTime
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
DateTime minSupportedDateTime = CultureInfo.CurrentCulture.Calendar.MinSupportedDateTime;
|
||||||
|
if (minSupportedDateTime.Year < 1753)
|
||||||
|
{
|
||||||
|
return new DateTime(1753, 1, 1);
|
||||||
|
}
|
||||||
|
return minSupportedDateTime;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
[DefaultValue(false)]
|
[DefaultValue(false)]
|
||||||
[Description("日期输入时,是否可空显示"), Category("SunnyUI")]
|
[Description("日期输入时,是否可空显示"), Category("SunnyUI")]
|
||||||
public bool CanEmpty { get; set; }
|
public bool CanEmpty { get; set; }
|
||||||
@ -177,6 +304,8 @@ namespace Sunny.UI
|
|||||||
item.Translate();
|
item.Translate();
|
||||||
item.SetDPIScale();
|
item.SetDPIScale();
|
||||||
item.SetStyleColor(UIStyles.ActiveStyleColor);
|
item.SetStyleColor(UIStyles.ActiveStyleColor);
|
||||||
|
item.max = MaxDate;
|
||||||
|
item.min = MinDate;
|
||||||
ItemForm.Show(this);
|
ItemForm.Show(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user