一个自定义的数字输入框控件
项目开发中经常会用到只允许输入数字的文本框控件。虽然很多第三方控商已经提供了不少此类优秀的控件,但是我们为什么就不能自已动手也来DIY一把呢。以下就是本人的一个小例子:
我的设计思路是在用户每次敲击键盘时对键入的字符进行校验以过滤不合法的字符。在文本框失去焦点时再对录入的数据进行一次校验以符合预定的格式。以下是该控件的实现代码。
1
using System;
2
using System.Collections.Generic;
3
using System.ComponentModel;
4
using System.Drawing;
5
using System.Data;
6
using System.Text;
7
using System.Windows.Forms;
8
9
namespace Nelson.ControlLibrary
10

{
11
public partial class NumberBox : System.Windows.Forms.TextBox
12
{
13
public NumberBox()
14
{
15
InitializeComponent();
16
}
17
18
自定义成员#region 自定义成员
19
private int maxIntegerLength = 10;
20
private int minIntegerLength = 0;
21
private int maxDecimalLength = 4;
22
private int minDecimalLength = 0;
23
private int integerLength;
24
private int decimalLength;
25
#endregion
26
27
自定义属性#region 自定义属性
28
/**//// <summary>
29
/// 最大整数位数
30
/// </summary>
31
public int MaxIntegerLength
32
{
33
get
34
{
35
return maxIntegerLength;
36
}
37
set
38
{
39
if (value >= 0 && value >= minIntegerLength)
40
{
41
maxIntegerLength = value;
42
}
43
else
44
{
45
throw new Exception("最大整数位数不应小于最小整数位数");
46
}
47
}
48
}
49
50
/**//// <summary>
51
/// 最小整数位数
52
/// </summary>
53
public int MinIntegerLength
54
{
55
get
56
{
57
return minIntegerLength;
58
}
59
set
60
{
61
if (value >= 0 && value <= maxIntegerLength)
62
{
63
minIntegerLength = value;
64
}
65
else
66
{
67
throw new Exception("最小整数位数不应大于最大整数位数");
68
}
69
}
70
}
71
72
/**//// <summary>
73
/// 最大小数位数
74
/// </summary>
75
public int MaxDecimalLength
76
{
77
get
78
{
79
return maxDecimalLength;
80
}
81
set
82
{
83
if (value >= 0 && value >= minDecimalLength)
84
{
85
maxDecimalLength = value;
86
}
87
else
88
{
89
throw new Exception("最大小数位数不应小于最小小数位数");
90
}
91
}
92
}
93
94
/**//// <summary>
95
/// 最小小数位数
96
/// </summary>
97
public int MinDecimalLength
98
{
99
get
100
{
101
return minDecimalLength;
102
}
103
set
104
{
105
if (value >= 0 && value <= maxDecimalLength)
106
{
107
minDecimalLength = value;
108
}
109
else
110
{
111
throw new Exception("最小小数位数不应大于最大小数位数");
112
}
113
}
114
}
115
#endregion
116
117
重写方法#region 重写方法
118
protected override void OnKeyPress(KeyPressEventArgs e)
119
{
120
int editIndex = SelectionStart; //获取当前编辑位
121
122
if (e.KeyChar == (char)Keys.Back) return; //放行"退格"键
123
124
if (e.KeyChar.Equals('.') || Char.IsNumber(e.KeyChar)) //过滤非数字与非小数点
125
{
126
if (Text.IndexOf(".") > -1) //是否存在小数点
127
{
128
//禁止重复输入小数点
129
if (e.KeyChar.Equals('.'))
130
{
131
e.Handled = true;
132
return;
133
}
134
else
135
{
136
if (SelectedText.Length > 0)
137
{
138
return;
139
}
140
141
integerLength = Text.IndexOf(".");
142
decimalLength = Text.Length - integerLength - 1;
143
144
//控制最大小数位数
145
if (decimalLength >= maxDecimalLength && editIndex > Text.IndexOf("."))
146
{
147
e.Handled = true;
148
return;
149
}
150
151
//控制最大整数位数
152
if (integerLength >= maxIntegerLength && editIndex <= Text.IndexOf("."))
153
{
154
e.Handled = true;
155
return;
156
}
157
}
158
}
159
else
160
{
161
//控制最大整数位数
162
integerLength = Text.Length;
163
if (integerLength == maxIntegerLength && !e.KeyChar.Equals('.'))
164
{
165
e.Handled = true;
166
}
167
}
168
}
169
else
170
{
171
e.Handled = true;
172
}
173
174
base.OnKeyPress(e);
175
}
176
177
178
protected override void OnLeave(EventArgs e)
179
{
180
if (Text == null || Text == "") return;
181
182
Text = Text.TrimStart('0');
183
184
//取整数位数与小数位数
185
if (Text.IndexOf(".") == -1)
186
{
187
integerLength = Text.Length;
188
decimalLength = 0;
189
}
190
else
191
{
192
integerLength = Text.IndexOf(".");
193
decimalLength = Text.Length - integerLength - 1;
194
195
//验证小数位数是否符合最小值(不足补零)
196
if (decimalLength < minDecimalLength)
197
{
198
Text = Text.PadRight(integerLength + minDecimalLength + 1, '0');
199
}
200
}
201
202
//整数未输自动补零
203
if (integerLength == 0)
204
{
205
Text = "0" + Text;
206
}
207
208
//验证整数位数是否符合最小值
209
if (integerLength < minIntegerLength)
210
{
211
Focus();
212
Select(0, integerLength);
213
}
214
215
//验证整数位数是否符合最大值
216
if (integerLength > maxIntegerLength)
217
{
218
Focus();
219
Select(0, integerLength);
220
}
221
base.OnLeave(e);
222
}
223
#endregion
224
}
225
}
226
using System;2
using System.Collections.Generic;3
using System.ComponentModel;4
using System.Drawing;5
using System.Data;6
using System.Text;7
using System.Windows.Forms;8

9
namespace Nelson.ControlLibrary10


{11
public partial class NumberBox : System.Windows.Forms.TextBox12

{13
public NumberBox()14

{15
InitializeComponent();16
}17

18

自定义成员#region 自定义成员19
private int maxIntegerLength = 10;20
private int minIntegerLength = 0;21
private int maxDecimalLength = 4;22
private int minDecimalLength = 0;23
private int integerLength;24
private int decimalLength;25
#endregion26

27

自定义属性#region 自定义属性28

/**//// <summary>29
/// 最大整数位数30
/// </summary>31
public int MaxIntegerLength32

{33
get34

{35
return maxIntegerLength;36
}37
set38

{39
if (value >= 0 && value >= minIntegerLength)40

{41
maxIntegerLength = value;42
}43
else44

{45
throw new Exception("最大整数位数不应小于最小整数位数");46
}47
}48
}49

50

/**//// <summary>51
/// 最小整数位数52
/// </summary>53
public int MinIntegerLength54

{55
get56

{57
return minIntegerLength;58
}59
set60

{61
if (value >= 0 && value <= maxIntegerLength)62

{63
minIntegerLength = value;64
}65
else66

{67
throw new Exception("最小整数位数不应大于最大整数位数");68
}69
}70
}71

72

/**//// <summary>73
/// 最大小数位数74
/// </summary>75
public int MaxDecimalLength76

{77
get78

{79
return maxDecimalLength;80
}81
set82

{83
if (value >= 0 && value >= minDecimalLength)84

{85
maxDecimalLength = value;86
}87
else88

{89
throw new Exception("最大小数位数不应小于最小小数位数");90
}91
}92
}93

94

/**//// <summary>95
/// 最小小数位数96
/// </summary>97
public int MinDecimalLength98

{99
get100

{101
return minDecimalLength;102
}103
set104

{105
if (value >= 0 && value <= maxDecimalLength)106

{107
minDecimalLength = value;108
}109
else110

{111
throw new Exception("最小小数位数不应大于最大小数位数");112
}113
}114
}115
#endregion116

117

重写方法#region 重写方法118
protected override void OnKeyPress(KeyPressEventArgs e)119

{120
int editIndex = SelectionStart; //获取当前编辑位121

122
if (e.KeyChar == (char)Keys.Back) return; //放行"退格"键123

124
if (e.KeyChar.Equals('.') || Char.IsNumber(e.KeyChar)) //过滤非数字与非小数点125

{126
if (Text.IndexOf(".") > -1) //是否存在小数点127

{128
//禁止重复输入小数点129
if (e.KeyChar.Equals('.'))130

{131
e.Handled = true;132
return;133
}134
else135

{136
if (SelectedText.Length > 0)137

{138
return;139
}140

141
integerLength = Text.IndexOf(".");142
decimalLength = Text.Length - integerLength - 1;143

144
//控制最大小数位数145
if (decimalLength >= maxDecimalLength && editIndex > Text.IndexOf("."))146

{147
e.Handled = true;148
return;149
}150

151
//控制最大整数位数152
if (integerLength >= maxIntegerLength && editIndex <= Text.IndexOf("."))153

{154
e.Handled = true;155
return;156
}157
}158
}159
else160

{ 161
//控制最大整数位数162
integerLength = Text.Length;163
if (integerLength == maxIntegerLength && !e.KeyChar.Equals('.'))164

{165
e.Handled = true;166
}167
}168
}169
else170

{171
e.Handled = true;172
}173

174
base.OnKeyPress(e);175
}176

177

178
protected override void OnLeave(EventArgs e)179

{180
if (Text == null || Text == "") return;181

182
Text = Text.TrimStart('0');183

184
//取整数位数与小数位数185
if (Text.IndexOf(".") == -1)186

{187
integerLength = Text.Length;188
decimalLength = 0;189
}190
else191

{192
integerLength = Text.IndexOf(".");193
decimalLength = Text.Length - integerLength - 1;194
195
//验证小数位数是否符合最小值(不足补零) 196
if (decimalLength < minDecimalLength)197

{198
Text = Text.PadRight(integerLength + minDecimalLength + 1, '0');199
}200
}201

202
//整数未输自动补零203
if (integerLength == 0)204

{205
Text = "0" + Text;206
}207

208
//验证整数位数是否符合最小值209
if (integerLength < minIntegerLength)210

{211
Focus();212
Select(0, integerLength);213
}214

215
//验证整数位数是否符合最大值216
if (integerLength > maxIntegerLength)217

{218
Focus();219
Select(0, integerLength);220
}221
base.OnLeave(e);222
}223
#endregion224
}225
}226


浙公网安备 33010602011771号