* UIDatePicker:更改日期范围最小值和最大值

This commit is contained in:
Sunny 2020-08-04 23:59:31 +08:00
parent 6a47388f74
commit 49416a6e96
7 changed files with 105 additions and 41 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -30,6 +30,7 @@ namespace Sunny.UI
public sealed class UIDateItem : UIDropDownItem public sealed class UIDateItem : UIDropDownItem
{ {
#region InitializeComponent #region InitializeComponent
private UITabControl TabControl; private UITabControl TabControl;
private System.Windows.Forms.TabPage tabPage1; private System.Windows.Forms.TabPage tabPage1;
private System.Windows.Forms.TabPage tabPage2; private System.Windows.Forms.TabPage tabPage2;
@ -289,9 +290,9 @@ namespace Sunny.UI
this.tabPage2.ResumeLayout(false); this.tabPage2.ResumeLayout(false);
this.tabPage3.ResumeLayout(false); this.tabPage3.ResumeLayout(false);
this.ResumeLayout(false); this.ResumeLayout(false);
} }
#endregion
#endregion InitializeComponent
private readonly List<string> months = new List<string>(); private readonly List<string> months = new List<string>();
private readonly List<int> years = new List<int>(); private readonly List<int> years = new List<int>();
@ -362,12 +363,32 @@ namespace Sunny.UI
DateTime dt = new DateTime(iYear, iMonth, 1); DateTime dt = new DateTime(iYear, iMonth, 1);
int week = (int)dt.DayOfWeek; int week = (int)dt.DayOfWeek;
bool maxToEnd = false;
DateTime dtBegin = week == 0 ? dt.AddDays(-7) : dt.AddDays(-week); DateTime dtBegin = week == 0 ? dt.AddDays(-7) : dt.AddDays(-week);
for (int i = 1; i <= 42; i++) for (int i = 1; i <= 42; i++)
{
try
{
if (!maxToEnd && dtBegin.AddDays(i - 1).Date.Equals(DateTime.MaxValue.Date))
{
maxToEnd = true;
}
if (!maxToEnd)
{ {
DateTime lblDate = dtBegin.AddDays(i - 1); DateTime lblDate = dtBegin.AddDays(i - 1);
days.Add(lblDate); days.Add(lblDate);
} }
else
{
days.Add(DateTime.MaxValue.Date);
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
p3.Invalidate(); p3.Invalidate();
TopPanel.Text = Year + "年" + Month + "月"; TopPanel.Text = Year + "年" + Month + "月";
@ -443,6 +464,7 @@ namespace Sunny.UI
private void b3_Click(object sender, EventArgs e) private void b3_Click(object sender, EventArgs e)
{ {
DateTime dt = new DateTime(Year, Month, 1); DateTime dt = new DateTime(Year, Month, 1);
if (dt.Year == DateTime.MaxValue.Year && dt.Month == DateTime.MaxValue.Month) return;
dt = dt.AddMonths(1); dt = dt.AddMonths(1);
Year = dt.Year; Year = dt.Year;
Month = dt.Month; Month = dt.Month;
@ -455,16 +477,19 @@ namespace Sunny.UI
{ {
case 0: case 0:
Year = year / 10 * 10; Year = year / 10 * 10;
if (year == 9990) return;
Year += 10; Year += 10;
SetYears(Year); SetYears(Year);
break; break;
case 1: case 1:
if (Year == DateTime.MaxValue.Year) return;
Year += 1; Year += 1;
TopPanel.Text = Year + "年"; TopPanel.Text = Year + "年";
break; break;
case 2: case 2:
if (Year == DateTime.MaxValue.Year) return;
Year += 1; Year += 1;
SetYearMonth(Year, Month); SetYearMonth(Year, Month);
break; break;
@ -500,13 +525,14 @@ 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;
Month = x + y * 4 + 1; Month = x + y * 4 + 1;
if (Month <= 0 || Month > 12) return;
SetYearMonth(Year, Month); SetYearMonth(Year, Month);
activeMonth = -1; activeMonth = -1;
TabControl.SelectedTab = tabPage3; TabControl.SelectedTab = tabPage3;
} }
private int activeMonth = -1; private int activeMonth = -1;
private void p2_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e) private void p2_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{ {
int width = p2.Width / 4; int width = p2.Width / 4;
@ -522,6 +548,7 @@ namespace Sunny.UI
} }
private int activeYear = -1; private int activeYear = -1;
private void p1_PaintOther(object sender, System.Windows.Forms.PaintEventArgs e) private void p1_PaintOther(object sender, System.Windows.Forms.PaintEventArgs e)
{ {
for (int i = 0; i < 12; i++) for (int i = 0; i < 12; i++)
@ -533,9 +560,12 @@ 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] != 10000)
{
e.Graphics.DrawString(years[i].ToString(), Font, i == activeYear ? UIColor.Blue : color, left + (width - sf.Width) / 2, top + (height - sf.Height) / 2); e.Graphics.DrawString(years[i].ToString(), Font, i == activeYear ? UIColor.Blue : color, left + (width - sf.Width) / 2, top + (height - sf.Height) / 2);
} }
} }
}
private void p1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e) private void p1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{ {
@ -558,7 +588,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 (iy<0 ||iy>=12) return;
if (years[iy] > 9999)
Year = 9999;
else
Year = years[iy]; Year = years[iy];
activeYear = -1; activeYear = -1;
TabControl.SelectedTab = tabPage2; TabControl.SelectedTab = tabPage2;
@ -581,6 +614,7 @@ namespace Sunny.UI
e.Graphics.DrawLine(Color.DarkGray, 8, 26, 268, 26); e.Graphics.DrawLine(Color.DarkGray, 8, 26, 268, 26);
bool maxDrawed = false;
for (int i = 0; i < 42; i++) for (int i = 0; i < 42; i++)
{ {
int left = width * (i % 7); int left = width * (i % 7);
@ -589,11 +623,21 @@ 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()) ? UIColor.Blue : color; color = (days[i].DateString() == date.DateString()) ? UIColor.Blue : color;
if (!maxDrawed)
{
e.Graphics.DrawString(days[i].Day.ToString(), Font, i == activeDay ? UIColor.Blue : color, left + (width - sf.Width) / 2, top + 30 + (height - sf.Height) / 2); e.Graphics.DrawString(days[i].Day.ToString(), Font, i == activeDay ? UIColor.Blue : color, left + (width - sf.Width) / 2, top + 30 + (height - sf.Height) / 2);
} }
if (!maxDrawed && days[i].Date.Equals(DateTime.MaxValue.Date))
{
maxDrawed = true;
}
}
} }
private int activeDay = -1; private int activeDay = -1;
private void p3_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e) private void p3_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{ {
int width = p3.Width / 7; int width = p3.Width / 7;
@ -616,6 +660,7 @@ 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;
date = days[id].Date; date = days[id].Date;
DoValueChanged(this, Date); DoValueChanged(this, Date);
CloseParent(); CloseParent();

View File

@ -188,17 +188,34 @@ namespace Sunny.UI
} }
} }
//[Browsable(false)]
//public List<string> SelectedItems
//{
// get
// {
// List<string> items = new List<string>();
// foreach (var checkBox in boxes)
// {
// if (checkBox.Checked)
// items.Add(checkBox.Text);
// }
// return items;
// }
//}
[Browsable(false)] [Browsable(false)]
public List<string> SelectedItems public List<object> SelectedItems
{ {
get get
{ {
List<string> items = new List<string>(); List<object> items = new List<object>();
foreach (var checkBox in boxes) for (int i = 0; i < boxes.Count; i++)
{ {
if (checkBox.Checked) if (boxes[i].Checked)
items.Add(checkBox.Text); items.Add(Items[i]);
} }
return items; return items;

View File

@ -59,6 +59,8 @@ namespace Sunny.UI
get => item.Date; get => item.Date;
set set
{ {
if (value< new DateTime(1753,1,1))
value = new DateTime(1753,1,1);
Text = value.ToString(dateFormat); Text = value.ToString(dateFormat);
item.Date = value; item.Date = value;
} }