增强文本框2
终极管理员 知识笔记 44阅读
根据网友的建议,对0.1版本做了以下调整。1)增加常用验证和选择功能。目前,已经添加了一些常见的验证。我会慢慢收集一些常用的正则表达式来补充。2)增加边框颜色调整功能。普通文本框的边框颜色只能是黑色。该控件的边框颜色可以由用户定义。自定义控件的完整代码如下:1程序描述和更改历史# Region '程序描述和更改历史' 2'-。 rgba(0,128,0,1) 8' 9 '创建日期:2005-05-10 '上次修改时间:2005-05-11 12 '更改历史13 14 '序列号版本日期描述15'10.12005-05-01基本验证功能16' 20.22005-05-01 -
8, 0, 1)">18#End Region
19
20Option Strict On
21Option Explicit On
22
23#Region "导入命名空间"
24Imports System
25Imports System.Windows.Forms
26Imports System.ComponentModel
27Imports System.Drawing
28#End Region
29
30Public Class HD_TextBox
31 Inherits System.Windows.Forms.TextBox
32
33#Region " 组件设计器生成的代码 "
34
35 Public Sub New(ByVal Container As 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 Public Sub New()
48 MyBase.New()
49
50 '该调用是组件设计器所必需的。
51 InitializeComponent()
52
53 '在 InitializeComponent() 调用之后添加任何初始化
54
55 End Sub
56
57 '组件重写 dispose 以清理组件列表。
58 Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
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 <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
74 components = New System.ComponentModel.Container
75 End Sub
76
77#End Region
78
79#Region "成员变量"
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#Region "扩展属性"
105
106 <Description("扩展属性:是否允许空值."), Category("HD_TextBox")> _
107 Public Property AllowNull() As Boolean
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 Public Property Regex() As String
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 Public Property ErrorMsg() As String
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 Public Property BoundColor() As 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#Region "内置正则表达式"
151
152 Private _comRegex As CommanRegex = CommanRegex.Null
153
154 '内置正则表达式
155 Public Enum CommanRegex
156 Null = 0
157 Email = 1
158 Telephone = 2
159 Mobile = 3
160 End Enum
161
162 Public Property ComRegex() As 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 Private Sub HD_TextBox_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Validating
193
194 Dim result As Boolean
195 result = ValidateData()
196 ShowErr(result)
197 e.Cancel = result
198 End Sub
199
200 '对文本框的内容进行验证
201 Private Function ValidateData() As Boolean
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 Private Sub ShowErr(ByVal bShow As Boolean)
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 Private Sub PaintBound(ByVal sender As Object, ByVal e As 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 Private Sub 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 Private Sub HD_TextBox_HandleCreated(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.HandleCreated
271 If _bConstructed = True Then
272 '运行时被创建
273 Init()
274 Else
275 '设计时被创建
276 End If
277 End Sub
278
279End Class
280

调用方式:
1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.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 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
存在的一些问题:
1) 不知道TextBox有没有类似窗体的Form_Load事件,就是在运行时创建完TextBox之后触发的事件?
我发现HandleCreated事件在窗体设计状态下,把TextBox加入到Form中的时候会被触发,如果直接在这里加入
AddHandler Me.Parent.Paint, AddressOf PaintBound
这样在设计界面中就无法正确显示TextBox控件,
所以我加了一个_bConstructed变量标识是运行时还是设计时,如果是设计时就不调用.
如果有只在运行时触发的事件(并且是在HandleCreated之后的事件),我就把初始化代码写在这里了.
2.当文本框输入的内容非法时,其它控件无法获得焦点,有些时候需要取消验证,比如点击"取消"或"退出"按钮的时候
就不想再执行验证 否则非得输入一个合法得数据后才能取消太不爽了.

标签: