-
Notifications
You must be signed in to change notification settings - Fork 5
/
TimeBaseLine.cs
314 lines (260 loc) · 7.57 KB
/
TimeBaseLine.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
306
307
308
309
310
311
312
313
314
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
namespace Subindex
{
/// <summary>
/// TimeBaseLine 的摘要说明。
/// </summary>
///
[DefaultEvent("ValueChanged")]
public class TimeBaseLine : System.Windows.Forms.UserControl
{
private System.Windows.Forms.TextBox valueBox;
private System.Windows.Forms.VScrollBar upDown;
private readonly int[,] point =new int[,] {{1,1}, {3,2}, {6,2}, {9,2}, {12,3},{12,3}};
private DateTime timeValue=DateTime.Parse("00:00:00.000");
private bool Symbol=true;
private bool inputState;
/// <summary>
/// 数据值已改变
/// </summary>
public event EventHandler ValueChanged;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public TimeBaseLine()
{
// 该调用是 Windows.Forms 窗体设计器所必需的。
InitializeComponent();
valueBox.SelectionStart=15;
// TODO: 在 InitializeComponent 调用后添加任何初始化
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region 组件设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器
/// 修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.valueBox = new System.Windows.Forms.TextBox();
this.upDown = new System.Windows.Forms.VScrollBar();
this.SuspendLayout();
//
// valueBox
//
this.valueBox.Location = new System.Drawing.Point(1, 2);
this.valueBox.Name = "valueBox";
this.valueBox.Size = new System.Drawing.Size(112, 21);
this.valueBox.TabIndex = 0;
this.valueBox.Text = " + 00:00:00,000";
this.valueBox.KeyDown += new System.Windows.Forms.KeyEventHandler(this.valueBox_KeyDown);
this.valueBox.MouseDown += new System.Windows.Forms.MouseEventHandler(this.valueBox_MouseDown);
this.valueBox.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.valueBox_KeyPress);
this.valueBox.DoubleClick += new System.EventHandler(this.valueBox_DoubleClick);
this.valueBox.MouseMove += new System.Windows.Forms.MouseEventHandler(this.valueBox_MouseMove);
//
// upDown
//
this.upDown.LargeChange = 1;
this.upDown.Location = new System.Drawing.Point(96, 3);
this.upDown.Maximum = 1;
this.upDown.Name = "upDown";
this.upDown.Size = new System.Drawing.Size(16, 19);
this.upDown.TabIndex = 0;
this.upDown.Scroll += new System.Windows.Forms.ScrollEventHandler(this.upDown_Scroll);
//
// TimeBaseLine
//
this.Controls.Add(this.upDown);
this.Controls.Add(this.valueBox);
this.Name = "TimeBaseLine";
this.Size = new System.Drawing.Size(113, 24);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 设置或返回时间基线值
/// </summary>
public TimeSpan Value
{
get
{
TimeSpan ts=new TimeSpan(0,timeValue.Hour,timeValue.Minute,timeValue.Second,timeValue.Millisecond);
return Symbol?ts:-ts;
}
set
{
Symbol=value>=TimeSpan.Zero;
ValueChange(DateTime.Parse(value.ToString()));
}
}
private void valueBox_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
string filter=valueBox.SelectionStart<3?"+-":"0123456789";
if (filter.IndexOf(e.KeyChar)>-1)
{
switch(valueBox.SelectionStart/3)
{
case 0:
if (Symbol!=(e.KeyChar=='+')) SymbolChange();
break;
case 1:
if (inputState)
{
filter=timeValue.Hour.ToString()+e.KeyChar;
filter=filter.Substring(filter.Length-2);
if (int.Parse(filter)>23) filter=e.KeyChar.ToString();
}
else
filter=e.KeyChar.ToString();
inputState=int.Parse(filter)<10;
filter+=":"+timeValue.Minute.ToString()+":"+timeValue.Second.ToString()+"."+timeValue.Millisecond.ToString();
ValueChange(DateTime.Parse(filter));
break;
case 2:
if (inputState)
{
filter=timeValue.Minute.ToString()+e.KeyChar;
filter=filter.Substring(filter.Length-2);
if (int.Parse(filter)>59) filter=e.KeyChar.ToString();
}
else
filter=e.KeyChar.ToString();
inputState=int.Parse(filter)<10;
filter=timeValue.Hour.ToString()+":"+ filter +":"+timeValue.Second.ToString()+"."+timeValue.Millisecond.ToString();
ValueChange(DateTime.Parse(filter));
break;
case 3:
if (inputState)
{
filter=timeValue.Second.ToString()+e.KeyChar;
filter=filter.Substring(filter.Length-2);
if (int.Parse(filter)>59) filter=e.KeyChar.ToString();
}
else
filter=e.KeyChar.ToString();
inputState=int.Parse(filter)<10;
filter=timeValue.Hour.ToString()+":"+ timeValue.Minute.ToString() +":"+ filter +"."+timeValue.Millisecond.ToString();
ValueChange(DateTime.Parse(filter));
break;
case 4:
if (inputState)
{
filter="0"+(timeValue.Millisecond).ToString()+e.KeyChar;
filter=filter.Substring(filter.Length-3);
}
else
filter="00"+e.KeyChar.ToString();
inputState=int.Parse(filter)<100;
filter=timeValue.Hour.ToString()+":"+ timeValue.Minute.ToString() +":"+ timeValue.Second.ToString() +"."+ filter;
ValueChange(DateTime.Parse(filter));
break;
}
}
e.Handled=true;
}
private void valueBox_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
int selPos=valueBox.SelectionStart/3;
switch (e.KeyCode)
{
case Keys.Left:
selPos=selPos>0?selPos-1:4;
goto case Keys.Space;
case Keys.Right:
selPos=selPos<4?selPos+1:0;
goto case Keys.Space;
case Keys.Up:
UpDown(true);
goto case Keys.Space;
case Keys.Down:
UpDown(false);
goto case Keys.Space;
case Keys.Space:
IndexChange(selPos);
inputState=false;
break;
}
e.Handled=true;
}
private void UpDown(bool isUp)
{
switch (valueBox.SelectionStart/3)
{
case 0:
SymbolChange();
break;
case 1:
ValueChange(timeValue.AddHours(isUp?1:-1));
break;
case 2:
ValueChange(timeValue.AddMinutes(isUp?1:-1));
break;
case 3:
ValueChange(timeValue.AddSeconds(isUp?1:-1));
break;
case 4:
ValueChange(timeValue.AddMilliseconds(isUp?1:-1));
break;
}
}
private void ValueChange(DateTime newValue)
{
timeValue=newValue;
Repaint();
}
private void SymbolChange()
{
Symbol=!Symbol;
Repaint();
}
private void Repaint()
{
int i=valueBox.SelectionStart/3;
valueBox.Text=(Symbol?" + ":" - ") + timeValue.ToString(Pub.Format);
IndexChange(i);
if (ValueChanged!=null) ValueChanged(this,null);
}
private void IndexChange(int selectIndex)
{
valueBox.Select(point[selectIndex,0],point[selectIndex,1]);
}
private void upDown_Scroll(object sender, System.Windows.Forms.ScrollEventArgs e)
{
if (e.Type==ScrollEventType.SmallDecrement||e.Type==ScrollEventType.SmallIncrement)
UpDown(e.NewValue==0);
}
private void valueBox_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
IndexChange(valueBox.SelectionStart/3);
inputState=false;
}
private void valueBox_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
IndexChange(valueBox.SelectionStart/3);
}
private void valueBox_DoubleClick(object sender, System.EventArgs e)
{
SymbolChange();
}
}
}