-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathDateRangeControl.cs
305 lines (249 loc) · 9.52 KB
/
DateRangeControl.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
namespace DigitalPlatform.CommonControl
{
public partial class DateRangeControl : UserControl
{
int IngoreTextChange = 0;
[Category("New Event")]
public event EventHandler DateTextChanged = null;
[Category("Appearance")]
[DescriptionAttribute("Border style of the control")]
[DefaultValue(typeof(System.Windows.Forms.BorderStyle), "None")]
public new BorderStyle BorderStyle
{
get
{
return base.BorderStyle;
}
set
{
base.BorderStyle = value;
}
}
public DateRangeControl()
{
InitializeComponent();
this.dateTimePicker_start.Value = this.dateTimePicker_start.MinDate;
this.dateTimePicker_end.Value = this.dateTimePicker_end.MinDate;
RefreshDisplay(this.dateTimePicker_start);
RefreshDisplay(this.dateTimePicker_end);
}
public override Size MaximumSize
{
get
{
Size size = base.MaximumSize;
int nLimitHeight = this.dateTimePicker_end.Location.Y + this.dateTimePicker_end.Height
+ 4;
if (size.Height > nLimitHeight
|| size.Height == 0)
size.Height = nLimitHeight;
return size;
}
set
{
base.MaximumSize = value;
}
}
public override Size MinimumSize
{
get
{
Size size = base.MinimumSize;
int nLimitHeight = this.dateTimePicker_end.Location.Y + this.dateTimePicker_end.Height
+ 4;
int nLimitWidth = this.dateTimePicker_end.Location.X + this.dateTimePicker_end.Width
+ 4;
size.Height = nLimitHeight;
size.Width = nLimitWidth;
return size;
}
set
{
base.MinimumSize = value;
}
}
public override string Text
{
get
{
string strStart = "";
if (this.dateTimePicker_start.Value != this.dateTimePicker_start.MinDate)
strStart = this.dateTimePicker_start.Value.ToString("yyyyMMdd");
string strEnd = "";
if (this.dateTimePicker_end.Value != this.dateTimePicker_start.MinDate)
strEnd = this.dateTimePicker_end.Value.ToString("yyyyMMdd");
if (String.IsNullOrEmpty(strStart) == true
&& String.IsNullOrEmpty(strEnd) == true)
return "";
return strStart + "-" + strEnd;
}
set
{
SetStartEnd(value);
}
}
void SetStartEnd(string strValue)
{
this.dateTimePicker_start.Value = this.dateTimePicker_start.MinDate;
this.dateTimePicker_end.Value = this.dateTimePicker_start.MinDate;
strValue = strValue.Trim();
if (String.IsNullOrEmpty(strValue) == true)
return;
string strStart = "";
string strEnd = "";
int nRet = strValue.IndexOf("-");
if (nRet == -1)
{
strStart = strValue;
if (strStart.Length != 8)
throw new Exception("时间范围 '" + strValue + "' 格式不正确");
strEnd = "";
}
else
{
strStart = strValue.Substring(0, nRet).Trim();
if (strStart.Length != 8
&& string.IsNullOrEmpty(strStart) == false)
throw new Exception("时间范围 '" + strValue + "' 内 '" + strStart + "' 格式不正确");
strEnd = strValue.Substring(nRet + 1).Trim();
if (strEnd.Length != 8
&& string.IsNullOrEmpty(strEnd) == false)
throw new Exception("时间范围 '" + strValue + "' 内 '" + strEnd + "' 格式不正确");
}
if (String.IsNullOrEmpty(strStart) == false)
this.dateTimePicker_start.Value = Long8ToDateTime(strStart);
if (String.IsNullOrEmpty(strEnd) == false)
this.dateTimePicker_end.Value = Long8ToDateTime(strEnd);
}
public static DateTime Long8ToDateTime(string strDate8)
{
if (strDate8.Length != 8)
throw new Exception("日期字符串格式必须为8字符。");
int nYear = Convert.ToInt32(strDate8.Substring(0, 4));
int nMonth = Convert.ToInt32(strDate8.Substring(4, 2));
int nDay = Convert.ToInt32(strDate8.Substring(6, 2));
return new DateTime(nYear, nMonth, nDay);
}
private void dateTimePicker_start_ValueChanged(object sender, EventArgs e)
{
RefreshDisplay(this.dateTimePicker_start);
if (IngoreTextChange > 0)
return;
if (this.DateTextChanged != null)
{
this.DateTextChanged(sender, e);
}
}
void RefreshDisplay(DateTimePicker picker)
{
if (picker.Value == picker.MinDate)
picker.CustomFormat = " ";
else
picker.CustomFormat = "yyyy-MM-dd";
}
private void dateTimePicker_end_ValueChanged(object sender, EventArgs e)
{
RefreshDisplay(this.dateTimePicker_end);
if (IngoreTextChange > 0)
return;
if (this.DateTextChanged != null)
{
this.DateTextChanged(sender, e);
}
}
// 获得最近三年的年份字符串
List<string> GetRecentYear()
{
List<string> results = new List<string>();
int nCurrentYear = DateTime.Now.Year;
results.Add((nCurrentYear - 1).ToString().PadLeft(4, '0'));
results.Add(nCurrentYear.ToString().PadLeft(4, '0'));
results.Add((nCurrentYear + 1).ToString().PadLeft(4, '0'));
return results;
}
private void label_start_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Right)
return;
ContextMenu contextMenu = new ContextMenu();
MenuItem menuItem = null;
List<string> years = GetRecentYear();
for (int i = 0; i < years.Count; i++)
{
string strYear = years[i];
//
menuItem = new MenuItem(strYear);
contextMenu.MenuItems.Add(menuItem);
//
string strPart = "全年";
QuickSetParam param = new QuickSetParam();
param.Year = strYear;
param.Part = strPart;
MenuItem subMenuItem = new MenuItem(strPart);
subMenuItem.Click += new System.EventHandler(this.menu_quickSet_Click);
subMenuItem.Tag = param;
menuItem.MenuItems.Add(subMenuItem);
//
strPart = "上半年";
param = new QuickSetParam();
param.Year = strYear;
param.Part = strPart;
subMenuItem = new MenuItem(strPart);
subMenuItem.Click += new System.EventHandler(this.menu_quickSet_Click);
subMenuItem.Tag = param;
menuItem.MenuItems.Add(subMenuItem);
//
strPart = "下半年";
param = new QuickSetParam();
param.Year = strYear;
param.Part = strPart;
subMenuItem = new MenuItem(strPart);
subMenuItem.Click += new System.EventHandler(this.menu_quickSet_Click);
subMenuItem.Tag = param;
menuItem.MenuItems.Add(subMenuItem);
}
menuItem = new MenuItem("清空");
menuItem.Click += menu_clear_Click;
contextMenu.MenuItems.Add(menuItem);
contextMenu.Show(this.label_start, new Point(e.X, e.Y));
}
void menu_clear_Click(object sender, EventArgs e)
{
this.dateTimePicker_start.Value = this.dateTimePicker_start.MinDate;
this.dateTimePicker_end.Value = this.dateTimePicker_end.MinDate;
}
void menu_quickSet_Click(object sender, EventArgs e)
{
MenuItem menu = (MenuItem)sender;
QuickSetParam param = (QuickSetParam)menu.Tag;
if (param.Part == "全年")
{
this.Text = param.Year + "0101-" + param.Year + "1231";
return;
}
if (param.Part == "上半年")
{
this.Text = param.Year + "0101-" + param.Year + "0630";
return;
}
if (param.Part == "下半年")
{
this.Text = param.Year + "0701-" + param.Year + "1231";
return;
}
throw new Exception("未知的part参数值 '" + param.Part + "'");
}
}
public class QuickSetParam
{
public string Year = "";
public string Part = ""; // 全年/上半年/下半年 等等
}
}