* UIDatetimePicker: 添加MaxDate与MinDate属性

This commit is contained in:
yhuse
2025-09-20 20:05:44 +08:00
parent 3cfe4cbb97
commit 572bcb8e4b
2 changed files with 225 additions and 6 deletions

View File

@@ -695,6 +695,8 @@ namespace Sunny.UI
private readonly List<DateTime> days = new List<DateTime>();
public Color PrimaryColor { get; set; } = UIColor.Blue;
public bool ShowToday { get; set; }
public DateTime max = DateTime.MaxValue;
public DateTime min = DateTime.MinValue;
public UIDateTimeItem()
{
@@ -806,6 +808,18 @@ namespace Sunny.UI
{
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--;
activeDay = -1;
}
@@ -1182,14 +1196,22 @@ namespace Sunny.UI
int height = p2.Height / 3;
int left = width * (i % 4);
int top = height * (i / 4);
if (i + 1 == Month)
Color color = ForeColor;
if (new DateTime(Year, i + 1, 1) > max || new DateTime(Year, i + 1, 1).EndOfMonth() < min)
{
e.Graphics.DrawString(months[i], font, PrimaryColor, new Rectangle(left, top, width, height), ContentAlignment.MiddleCenter);
color = Color.DarkGray;
}
else
{
e.Graphics.DrawString(months[i], font, i == activeMonth ? PrimaryColor : ForeColor, new Rectangle(left, top, width, height), ContentAlignment.MiddleCenter);
if (i + 1 == Month) color = PrimaryColor;
if (i + 1 == Month) color = PrimaryColor;
if (i == activeMonth) color = PrimaryColor;
if (i == activeMonth) color = PrimaryColor;
}
e.Graphics.DrawString(months[i], font, color, new Rectangle(left, top, width, height), ContentAlignment.MiddleCenter);
}
}
@@ -1200,6 +1222,11 @@ namespace Sunny.UI
int x = e.Location.X / width;
int y = e.Location.Y / height;
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)
{
activeMonth = im;
@@ -1215,6 +1242,10 @@ namespace Sunny.UI
int y = e.Location.Y / height;
Month = x + y * 4 + 1;
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);
activeMonth = -1;
TabControl.SelectedTab = tabPage3;
@@ -1229,8 +1260,24 @@ namespace Sunny.UI
int height = p1.Height / 3;
int left = width * (i % 4);
int top = height * (i / 4);
Color color = (i == 0 || i == 11) ? Color.DarkGray : ForeColor;
e.Graphics.DrawString(years[i].ToString(), font, (i == activeYear || years[i] == Year) ? PrimaryColor : color, new Rectangle(left, top, width, height), ContentAlignment.MiddleCenter);
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)
{
e.Graphics.DrawString(years[i].ToString(), font, color, new Rectangle(left, top, width, height), ContentAlignment.MiddleCenter);
}
}
}
@@ -1241,6 +1288,12 @@ namespace Sunny.UI
int x = e.Location.X / width;
int y = e.Location.Y / height;
int iy = x + y * 4;
if (iy < 0) return;
if (iy >= 12) return;
if (years[iy] < min.Year) return;
if (years[iy] > max.Year) return;
if (activeYear != iy)
{
activeYear = iy;
@@ -1256,6 +1309,10 @@ namespace Sunny.UI
int y = e.Location.Y / height;
int iy = x + y * 4;
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];
activeYear = -1;
TabControl.SelectedTab = tabPage2;
@@ -1282,6 +1339,8 @@ namespace Sunny.UI
int top = height * (i / 7);
Color color = (days[i].Month == Month) ? ForeColor : Color.DarkGray;
color = (days[i].DateString() == date.DateString()) ? b3.SymbolColor : color;
if (days[i] < min) color = Color.DarkGray;
if (days[i] > max) color = Color.DarkGray;
if (days[i].DateString() == date.DateString())
{
@@ -1325,6 +1384,17 @@ namespace Sunny.UI
int x = e.Location.X / width;
int y = (e.Location.Y - 30) / height;
int iy = x + y * 7;
if (iy.InRange(0, days.Count - 1))
{
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;
if (activeDay != iy || istoday != isToday)
@@ -1344,13 +1414,23 @@ namespace Sunny.UI
int y = (e.Location.Y - 30) / height;
int id = x + y * 7;
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 (DateTime.Now.Date < min) return;
if (DateTime.Now.Date > max) return;
date = DateTime.Now.Date;
DoValueChanged(this, Date);
Close();
}
else
{
if (days[id].Date < min) return;
if (days[id].Date > max) return;
date = days[id].Date;
}
date = new DateTime(date.Year, date.Month, date.Day, Hour, Minute, Second);
DoValueChanged(this, Date);
@@ -1367,17 +1447,28 @@ namespace Sunny.UI
int y = (e.Location.Y - 30) / height;
int id = x + y * 7;
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 (DateTime.Now.Date < min) return;
if (DateTime.Now.Date > max) return;
date = DateTime.Now.Date;
DoValueChanged(this, Date);
Close();
}
else
{
if (days[id].Date < min) return;
if (days[id].Date > max) return;
date = days[id].Date;
}
date = new DateTime(date.Year, date.Month, date.Day, Hour, Minute, Second);
DoValueChanged(this, Date);
p3.Invalidate();
Close();
}

View File

@@ -25,6 +25,7 @@
* 2024-07-13: V3.6.7 修改选择日期在下拉框中显示方式
* 2024-08-28: V3.7.0 修复格式化字符串包含/时显示错误
* 2024-11-10: V3.7.2 增加StyleDropDown属性手动修改Style时设置此属性以修改下拉框主题
* 2025-09-20: V3.8.8 添加MaxDate与MinDate属性
******************************************************************************/
using System;
@@ -203,6 +204,8 @@ namespace Sunny.UI
item.Translate();
item.SetDPIScale();
item.SetStyleColor(UIStyles.ActiveStyleColor);
item.max = MaxDate;
item.min = MinDate;
if (StyleDropDown != UIStyle.Inherited) item.Style = StyleDropDown;
Size size = SizeMultiple == 1 ? new Size(452, 200) : new Size(904, 400);
ItemForm.Show(this, size);
@@ -222,5 +225,130 @@ namespace Sunny.UI
MaxLength = dateFormat.Length;
}
}
private DateTime max = DateTime.MaxValue;
private DateTime min = DateTime.MinValue;
internal static DateTime EffectiveMaxDate(DateTime maxDate)
{
DateTime maxSupportedDate = DateTimePicker.MaximumDateTime;
if (maxDate > maxSupportedDate)
{
return maxSupportedDate;
}
return maxDate;
}
internal static DateTime EffectiveMinDate(DateTime minDate)
{
DateTime minSupportedDate = DateTimePicker.MinimumDateTime;
if (minDate < minSupportedDate)
{
return minSupportedDate;
}
return minDate;
}
[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;
}
}
[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);
internal static DateTime MinimumDateTime
{
get
{
DateTime minSupportedDateTime = CultureInfo.CurrentCulture.Calendar.MinSupportedDateTime;
if (minSupportedDateTime.Year < 1753)
{
return new DateTime(1753, 1, 1);
}
return minSupportedDateTime;
}
}
}
}