在网友的建议下,对0.1版作如下调整:
1) 增加常用验证选择功能
目前增加的常用验证还比较少,我会慢慢搜集一些常用的正则表达式增加进去.
2) 增加边框颜色调整功能
普通TextBox的边框颜色只能是黑色,本控件的边框颜色可由用户自己定义
自定义控件完整代码如下:
1![]()
2
' --------------------------------------------------------------------
3
'作者: hudan
4
'网址: http://www.cnblogs.com/hudan/
5
'版本: 0.2
6
'功能说明:
7
' 可以对文本框内容进行验证(通过正则表达式)的TextBox
8
'
9
'创建日期: 2005-05-01
10
'最后修改: 2005-05-05
11
12
'变更历史记录
13
14
'序号 版本 日期 说明
15
'1 0.1 2005-05-01 基本验证功能
16
'2 0.2 2005-05-05 增加边框颜色和常用验证功能
17
' ---------------------------------------------------------------------
18
#End Region
19
20
Option Strict On
21
Option Explicit On
22
23![]()
24
Imports System
25
Imports System.Windows.Forms
26
Imports System.ComponentModel
27
Imports System.Drawing
28
#End Region
29
30
HD_TextBox
31
Inherits System.Windows.Forms.TextBox
32
33![]()
34
35
System.ComponentModel.IContainer)
36
MyClass.New()
37
38
'Windows.Forms 类撰写设计器支持所必需的
39
Container.Add(Me)
40
41
'初始化
42
'Init()
43
_bConstructed = True
44
45
End Sub
46
47
()
48
MyBase.New()
49
50
'该调用是组件设计器所必需的。
51
InitializeComponent()
52
53
'在 InitializeComponent() 调用之后添加任何初始化
54
55
End Sub
56
57
'组件重写 dispose 以清理组件列表。
58
)
59
If disposing Then
60
If Not (components Is Nothing) Then
61
components.Dispose()
62
End If
63
End If
64
MyBase.Dispose(disposing)
65
End Sub
66
67
'组件设计器所必需的
68
Private components As System.ComponentModel.IContainer
69
70
'注意: 以下过程是组件设计器所必需的
71
'可以使用组件设计器修改此过程。
72
'不要使用代码编辑器修改它。
73
InitializeComponent()
74
components = New System.ComponentModel.Container
75
End Sub
76
77
#End Region
78
79![]()
80
81
'是否允许空值
82
Private _AllowNull As Boolean = True
83
84
'验证的正则表达式
85
Private _Regex As String = ""
86
87
'非法提示信息
88
Private _ErrorMsg As String = ""
89
90
'错误提示控件
91
Private _ErrorProvider As ErrorProvider
92
93
'边框颜色
94
Private _BoundColor As Color = Color.Transparent
95
96
'边框画笔
97
Private pen_bound As Pen
98
99
'是否运行时实例化
100
Private _bConstructed As Boolean = False
101
102
#End Region
103
104![]()
105
106
<Description("扩展属性:是否允许空值."), Category("HD_TextBox")> _
107![]()
108
Get
109
Return _AllowNull
110
End Get
111
Set(ByVal Value As Boolean)
112
_AllowNull = Value
113
End Set
114
End Property
115
116
<Description("扩展属性:验证文本的正则表达式."), Category("HD_TextBox")> _
117![]()
118
Get
119
Return _Regex
120
End Get
121
Set(ByVal Value As String)
122
_Regex = Value
123
End Set
124
End Property
125
126
<Description("扩展属性:输入非法数据时的错误提示信息."), Category("HD_TextBox")> _
127![]()
128
Get
129
Return _ErrorMsg
130
End Get
131
Set(ByVal Value As String)
132
_ErrorMsg = Value
133
End Set
134
End Property
135
136
137
<Description("扩展属性:边框颜色."), Category("HD_TextBox")> _
138
Color
139
Get
140
Return _BoundColor
141
End Get
142
Set(ByVal Value As Color)
143
_BoundColor = Value
144
pen_bound = New Pen(Value, 2)
145
End Set
146
End Property
147
148
#End Region
149
150![]()
151
152
Private _comRegex As CommanRegex = CommanRegex.Null
153
154
'内置正则表达式
155
CommanRegex
156
Null = 0
157
Email = 1
158
Telephone = 2
159
Mobile = 3
160
End Enum
161
162
CommanRegex
163
Get
164
Return _comRegex
165
End Get
166
Set(ByVal Value As CommanRegex)
167
_comRegex = Value
168
End Set
169
End Property
170
171
Private _Regexs() As String = { _
172
"", _
173
"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$", _
174
"^(\(\d{3,5}\)|\d{3,5}-)?\d{6,8}$", _
175
"", _
176
"", _
177
"" _
178
}
179
180
Private _ErrorMsgs() As String = { _
181
"", _
182
"请输入合法的Email地址,例如: iHudan@GMail.com ", _
183
"请输入合法的电话号码,比如 021-12345678", _
184
"", _
185
"", _
186
"" _
187
}
188
189
#End Region
190
191
'在控件验证事件中对文本框的内容进行验证
192
.Validating
193
194
Dim result As Boolean
195
result = ValidateData()
196
ShowErr(result)
197
e.Cancel = result
198
End Sub
199
200
'对文本框的内容进行验证
201![]()
202
203
Dim content As String = Me.Text
204
If content.Length = 0 Then
205
Return Not _AllowNull
206
Else
207
'不允许空值
208
Dim reg As System.Text.RegularExpressions.Regex
209
Dim tmpRegex As String
210
If _comRegex = CommanRegex.Null Then
211
tmpRegex = _Regex
212
Else
213
tmpRegex = Me._Regexs(_comRegex)
214
End If
215
If reg.IsMatch(content, tmpRegex) = True Then
216
Return False
217
Else
218
Return True
219
End If
220
End If
221
End Function
222
223
'显示错误提示
224
'bShow=true 显示错误提示
225
'bShow=false清楚错误提示
226
)
227
If _ErrorProvider Is Nothing Then
228
_ErrorProvider = New ErrorProvider(CType(Me.TopLevelControl, ContainerControl))
229
End If
230
Dim errMsg As String
231
If bShow Then
232
'显示错误提示
233
If _comRegex = CommanRegex.Null Then
234
'用户自定义验证方式
235
errMsg = _ErrorMsg
236
Else
237
'系统内置验证方式
238
errMsg = Me._ErrorMsgs(Me._comRegex)
239
End If
240
Else
241
'清除错误提示
242
errMsg = ""
243
End If
244
_ErrorProvider.SetError(Me, "")
245
_ErrorProvider.SetError(Me, errMsg)
246
End Sub
247
248
'在父窗体中绘画边框
249
System.Windows.Forms.PaintEventArgs)
250
Dim g As Graphics
251
g = e.Graphics
252
253
Dim r As Rectangle = Me.Bounds
254
g.DrawRectangle(pen_bound, r)
255
End Sub
256
257
'初始化数据
258
Init()
259
'初始化边框画笔
260
pen_bound = New Pen(_BoundColor, 2)
261
262
'添加父窗体的Paint事件
263
If Me.BorderStyle = BorderStyle.None Then
264
AddHandler Me.Parent.Paint, AddressOf PaintBound
265
End If
266
267
End Sub
268
269
'TextBox被创建
270
.HandleCreated
271
If _bConstructed = True Then
272
'运行时被创建
273
Init()
274
Else
275
'设计时被创建
276
End If
277
End Sub
278
279
End Class
280
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
调用方式:
1
.Load
2
'设置边框样式
3
Me.HD_TextBox1.BorderStyle = BorderStyle.None
4
'设置边框颜色
5
Me.HD_TextBox1.BoundColor = Color.Blue
6
'设置内置验证方式(Email地址)
7
Me.HD_TextBox1.ComRegex = HD_TextBox.HD_TextBox.CommanRegex.Email
8
9
'使用自定义验证方式
10
Me.HD_TextBox2.Regex = "^YS\d{6}$"
11
Me.HD_TextBox2.ErrorMsg = "请输入合法的编号,例如 YS100001"
12
End Sub
2
3
4
5
6
7
8
9
10
11
12
存在的一些问题:
1) 不知道TextBox有没有类似窗体的Form_Load事件,就是在运行时创建完TextBox之后触发的事件?
我发现HandleCreated事件在窗体设计状态下,把TextBox加入到Form中的时候会被触发,如果直接在这里加入
AddHandler Me.Parent.Paint, AddressOf PaintBound
这样在设计界面中就无法正确显示TextBox控件,
所以我加了一个_bConstructed变量标识是运行时还是设计时,如果是设计时就不调用.
如果有只在运行时触发的事件(并且是在HandleCreated之后的事件),我就把初始化代码写在这里了.
2.当文本框输入的内容非法时,其它控件无法获得焦点,有些时候需要取消验证,比如点击"取消"或"退出"按钮的时候
就不想再执行验证 否则非得输入一个合法得数据后才能取消太不爽了.