一个自定义的数字输入框控件
项目开发中经常会用到只允许输入数字的文本框控件。虽然很多第三方控商已经提供了不少此类优秀的控件,但是我们为什么就不能自已动手也来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

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
