NumericBox.cs
8.32 KB
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
namespace HHECS.Model.Controls
{
/// <summary>
/// 数字输入文本框
/// 创建者:Maximus
/// 创建时间:2013-5-13
/// 版本:0.2
/// 可输入:数字0-9,小数点,减号
/// 可设置小数位数
/// 可设置数值上下限,若上下限均设置为0,则上下限值无效
/// </summary>
public class NumericBox : TextBox
{
public NumericBox()
{
this.LostFocus += new RoutedEventHandler(NumericBox_LostFocus);
this.Text = "0";
}
#region 属性
/// <summary>
/// 在属性设计器中隐藏文本属性
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never), Browsable(false)]
public new string Text
{
get
{
return base.Text;
}
set
{
base.Text = value;
}
}
public static readonly DependencyProperty DecimalProperty = DependencyProperty.Register("DecimalPlaces", typeof(ushort), typeof(NumericBox));
/// <summary>
/// 允许输入的小数点个数
/// </summary>
[Description("小数位数,0表示不能输入小数"), Category("输入设置")]
public ushort DecimalPlaces
{
get
{
return (ushort)GetValue(DecimalProperty);
}
set
{
SetValue(DecimalProperty, value);
}
}
public static readonly DependencyProperty MaximumProperty =
DependencyProperty.Register("Maximum", typeof(decimal), typeof(NumericBox));
/// <summary>
/// 可输入的最大值
/// </summary>
[Description("可输入的最大值"), Category("输入设置"), DefaultValue(0)]
public decimal Maximum
{
get
{
return (decimal)GetValue(MaximumProperty);
}
set
{
SetValue(MaximumProperty, value);
}
}
public static readonly DependencyProperty MinimumProperty =
DependencyProperty.Register("Minimum", typeof(decimal), typeof(NumericBox));
/// <summary>
/// 可输入的最小值
/// </summary>
[Description("可输入的最小值"), Category("输入设置"), DefaultValue(0)]
public decimal Minimum
{
get
{
return (decimal)GetValue(MinimumProperty);
}
set
{
SetValue(MinimumProperty, value);
}
}
public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register("Value", typeof(decimal), typeof(NumericBox),
new PropertyMetadata(new decimal(0), null, OnValueChanged));
/// <summary>
/// 数值
/// </summary>
[Description("数值"), Category("输入设置"), DefaultValue(0)]
public decimal Value
{
get
{
return (decimal)GetValue(ValueProperty);
}
set
{
SetValue(ValueProperty, value);
}
}
private static Object OnValueChanged(DependencyObject d, object baseValue)
{
decimal value = (decimal)baseValue;
NumericBox Nmbox = d as NumericBox;
Nmbox.Text = value.ToString();
return value;
}
#endregion
#region 内部事件
/// <summary>
/// 丢失焦点
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void NumericBox_LostFocus(object sender, RoutedEventArgs e)
{
if (TextCheck())
{
this.Value = Convert.ToDecimal(this.Text);
}
}
#endregion
#region 输入限制
/// <summary>
/// 文本内容变更检查
/// </summary>
/// <param name="e"></param>
protected override void OnTextChanged(TextChangedEventArgs e)
{
if (this.Text.IndexOf('.') == 0
|| ((this.Text.Length > 1 && this.Text.Substring(0, 1) == "0" && this.Text.Substring(1, 1) != ".")))
{
this.Text = this.Text.Remove(0, 1);
this.SelectionStart = this.Text.Length;
}
}
/// <summary>
/// 输入限制
/// </summary>
/// <param name="e"></param>
protected override void OnKeyDown(KeyEventArgs e)
{//可输入:数字0-9,小数点,减号
int PPos = this.Text.IndexOf('.');//获取当前小数点位置
if ((e.Key >= Key.D1 && e.Key <= Key.D9)//数字0-9键
|| (e.Key >= Key.NumPad1 && e.Key <= Key.NumPad9))//小键盘数字0-9
{
if (PPos != -1 && this.SelectionStart > PPos)//可输入小数,且输入点在小数点后
{
if (this.Text.Length - PPos - 1 < DecimalPlaces)//小数位数是否已满
{
e.Handled = false;
return;
}
}
else
{
e.Handled = false;
return;
}
}
else if (e.Key == Key.D0 || e.Key == Key.NumPad0)//输入的是0
{
if (this.SelectionStart > 0)
{//非首位输入
if (this.Text.Substring(0, 1) != "0")
{//首位数字不为0
e.Handled = false;
return;
}
else
{//首位数字为0,
if (PPos == 1 && this.Text.Length - PPos - 1 < DecimalPlaces)
{ //0后面是小数点,小数位数未满
e.Handled = false;
return;
}
}
}
else
{
e.Handled = false;
return;
}
}
else if (e.Key == Key.Decimal || e.Key == Key.OemPeriod)//小数点
{
if (PPos == -1 && this.DecimalPlaces > 0)//未输入过小数点,且允许输入小数
{
if (this.SelectionStart == 0)
{
this.Text = "0.";
}
e.Handled = false;
return;
}
}
else if (e.Key == Key.Subtract || e.Key == Key.OemMinus)//减号
{
if (this.Minimum < 0)//允许输入的最小值小于0
{
if (this.Text.IndexOf('-') == -1 && this.SelectionStart == 0)
{
e.Handled = false;
return;
}
}
}
e.Handled = true;
}
/// <summary>
/// 输入合法性检查
/// </summary>
private bool TextCheck()
{
try
{
decimal d = Convert.ToDecimal(this.Text.Trim());//转换为数字
if (this.DecimalPlaces > 0)
{
d = Math.Round(d, this.DecimalPlaces);//舍入小数位数
}
if (!(this.Minimum == 0 && this.Maximum == 0))
{
if (d < this.Minimum)//输入的值小于最小值
{
d = this.Minimum;
}
else if (d > this.Maximum)//输入的值大于最大值
{
d = this.Maximum;
}
}
this.Value = d;
}
catch
{
this.Value = this.Minimum;
return false;
}
return true;
}
#endregion
}
}