这是我第一次写自定义控件,望各位批评指教.
控件从System.Windows.Forms.TextBox继承而来,主要是对文本框的内容进行验证,
如果不符合要求,则进行错误提示.
目前的验证采用正则表达式.
代码(HD_TextBox.VB)
调用代码:
五一期间我会不断改进,欢迎大家批评指正.
祝大家节日快乐!
控件从System.Windows.Forms.TextBox继承而来,主要是对文本框的内容进行验证,
如果不符合要求,则进行错误提示.
目前的验证采用正则表达式.
代码(HD_TextBox.VB)
1#Region " 程序描述 和 变更历史记录 "
2' --------------------------------------------------------------------
3'作者: hudan
4'网址: http://www.cnblogs.com/hudan/
5'版本: 0.1
6'功能说明:
7' 可以对文本框内容进行验证(通过正则表达式)的TextBox
8'
9'最后修改日期: 2005-05-01
10
11'变更历史
12'序号 版本 日期 说明
13'1 0.1 2005-05-01 基本验证功能
14
15' ---------------------------------------------------------------------
16#End Region
17
18Option Strict On
19Option Explicit On
20
21#Region "导入命名空间"
22Imports System
23Imports System.Windows.Forms
24Imports System.ComponentModel
25#End Region
26
27Public Class HD_TextBox
28 Inherits System.Windows.Forms.TextBox
29
30#Region " 组件设计器生成的代码 "
31
32 Public Sub New(ByVal Container As System.ComponentModel.IContainer)
33 MyClass.New()
34
35 'Windows.Forms 类撰写设计器支持所必需的
36 Container.Add(Me)
37 End Sub
38
39 Public Sub New()
40 MyBase.New()
41
42 '该调用是组件设计器所必需的。
43 InitializeComponent()
44
45 '在 InitializeComponent() 调用之后添加任何初始化
46
47 End Sub
48
49 '组件重写 dispose 以清理组件列表。
50 Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
51 If disposing Then
52 If Not (components Is Nothing) Then
53 components.Dispose()
54 End If
55 End If
56 MyBase.Dispose(disposing)
57 End Sub
58
59 '组件设计器所必需的
60 Private components As System.ComponentModel.IContainer
61
62 '注意: 以下过程是组件设计器所必需的
63 '可以使用组件设计器修改此过程。
64 '不要使用代码编辑器修改它。
65 <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
66 components = New System.ComponentModel.Container
67 End Sub
68
69#End Region
70
71#Region "成员变量"
72
73 '是否允许空值
74 Private _AllowNull As Boolean = True
75
76 '验证的正则表达式
77 Private _Regex As String = ""
78
79 '非法提示信息
80 Private _ErrorMsg As String = ""
81
82 '错误提示控件
83 Private _ErrorProvider As ErrorProvider
84
85
86#End Region
87
88#Region "扩展属性"
89
90 <Description("扩展属性:是否允许空值."), Category("HD_TextBox")> _
91 Public Property AllowNull() As Boolean
92 Get
93 Return _AllowNull
94 End Get
95 Set(ByVal Value As Boolean)
96 _AllowNull = Value
97 End Set
98 End Property
99
100 <Description("扩展属性:验证文本的正则表达式."), Category("HD_TextBox")> _
101 Public Property Regex() As String
102 Get
103 Return _Regex
104 End Get
105 Set(ByVal Value As String)
106 _Regex = Value
107 End Set
108 End Property
109
110 <Description("扩展属性:输入非法数据时的错误提示信息."), Category("HD_TextBox")> _
111 Public Property ErrorMsg() As String
112 Get
113 Return _ErrorMsg
114 End Get
115 Set(ByVal Value As String)
116 _ErrorMsg = Value
117 End Set
118 End Property
119
120#End Region
121
122 '在控件验证事件中对文本框的内容进行验证
123 Private Sub HD_TextBox_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Validating
124
125 Dim result As Boolean
126 result = ValidateData()
127 ShowErr(result)
128 e.Cancel = result
129 End Sub
130
131 '对文本框的内容进行验证
132 Private Function ValidateData() As Boolean
133
134 Dim content As String = Me.Text
135 If content.Length = 0 Then
136 Return Not _AllowNull
137 Else
138 '不允许空值
139 Dim reg As System.Text.RegularExpressions.Regex
140 If reg.IsMatch(content, _Regex) = True Then
141 Return False
142 Else
143 Return True
144 End If
145 End If
146 End Function
147
148 '显示错误提示
149 Private Sub ShowErr(ByVal bShow As Boolean)
150 If _ErrorProvider Is Nothing Then
151 _ErrorProvider = New ErrorProvider(CType(Me.Parent, ContainerControl))
152 End If
153 Dim errMsg As String
154 If bShow Then
155 errMsg = _ErrorMsg
156 Else
157 errMsg = ""
158 End If
159 _ErrorProvider.SetError(Me, errMsg)
160 End Sub
161
162
163End Class
164
2' --------------------------------------------------------------------
3'作者: hudan
4'网址: http://www.cnblogs.com/hudan/
5'版本: 0.1
6'功能说明:
7' 可以对文本框内容进行验证(通过正则表达式)的TextBox
8'
9'最后修改日期: 2005-05-01
10
11'变更历史
12'序号 版本 日期 说明
13'1 0.1 2005-05-01 基本验证功能
14
15' ---------------------------------------------------------------------
16#End Region
17
18Option Strict On
19Option Explicit On
20
21#Region "导入命名空间"
22Imports System
23Imports System.Windows.Forms
24Imports System.ComponentModel
25#End Region
26
27Public Class HD_TextBox
28 Inherits System.Windows.Forms.TextBox
29
30#Region " 组件设计器生成的代码 "
31
32 Public Sub New(ByVal Container As System.ComponentModel.IContainer)
33 MyClass.New()
34
35 'Windows.Forms 类撰写设计器支持所必需的
36 Container.Add(Me)
37 End Sub
38
39 Public Sub New()
40 MyBase.New()
41
42 '该调用是组件设计器所必需的。
43 InitializeComponent()
44
45 '在 InitializeComponent() 调用之后添加任何初始化
46
47 End Sub
48
49 '组件重写 dispose 以清理组件列表。
50 Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
51 If disposing Then
52 If Not (components Is Nothing) Then
53 components.Dispose()
54 End If
55 End If
56 MyBase.Dispose(disposing)
57 End Sub
58
59 '组件设计器所必需的
60 Private components As System.ComponentModel.IContainer
61
62 '注意: 以下过程是组件设计器所必需的
63 '可以使用组件设计器修改此过程。
64 '不要使用代码编辑器修改它。
65 <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
66 components = New System.ComponentModel.Container
67 End Sub
68
69#End Region
70
71#Region "成员变量"
72
73 '是否允许空值
74 Private _AllowNull As Boolean = True
75
76 '验证的正则表达式
77 Private _Regex As String = ""
78
79 '非法提示信息
80 Private _ErrorMsg As String = ""
81
82 '错误提示控件
83 Private _ErrorProvider As ErrorProvider
84
85
86#End Region
87
88#Region "扩展属性"
89
90 <Description("扩展属性:是否允许空值."), Category("HD_TextBox")> _
91 Public Property AllowNull() As Boolean
92 Get
93 Return _AllowNull
94 End Get
95 Set(ByVal Value As Boolean)
96 _AllowNull = Value
97 End Set
98 End Property
99
100 <Description("扩展属性:验证文本的正则表达式."), Category("HD_TextBox")> _
101 Public Property Regex() As String
102 Get
103 Return _Regex
104 End Get
105 Set(ByVal Value As String)
106 _Regex = Value
107 End Set
108 End Property
109
110 <Description("扩展属性:输入非法数据时的错误提示信息."), Category("HD_TextBox")> _
111 Public Property ErrorMsg() As String
112 Get
113 Return _ErrorMsg
114 End Get
115 Set(ByVal Value As String)
116 _ErrorMsg = Value
117 End Set
118 End Property
119
120#End Region
121
122 '在控件验证事件中对文本框的内容进行验证
123 Private Sub HD_TextBox_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Validating
124
125 Dim result As Boolean
126 result = ValidateData()
127 ShowErr(result)
128 e.Cancel = result
129 End Sub
130
131 '对文本框的内容进行验证
132 Private Function ValidateData() As Boolean
133
134 Dim content As String = Me.Text
135 If content.Length = 0 Then
136 Return Not _AllowNull
137 Else
138 '不允许空值
139 Dim reg As System.Text.RegularExpressions.Regex
140 If reg.IsMatch(content, _Regex) = True Then
141 Return False
142 Else
143 Return True
144 End If
145 End If
146 End Function
147
148 '显示错误提示
149 Private Sub ShowErr(ByVal bShow As Boolean)
150 If _ErrorProvider Is Nothing Then
151 _ErrorProvider = New ErrorProvider(CType(Me.Parent, ContainerControl))
152 End If
153 Dim errMsg As String
154 If bShow Then
155 errMsg = _ErrorMsg
156 Else
157 errMsg = ""
158 End If
159 _ErrorProvider.SetError(Me, errMsg)
160 End Sub
161
162
163End Class
164
调用代码:
1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
2 Me.HD_TextBox1.Regex = "^(\(\d{3}\)|\d{3}-)?\d{8}$"
3 Me.HD_TextBox1.ErrorMsg = "请输入合法的电话号码,比如 021-64370000"
4
5 Me.HD_TextBox2.Regex = "^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$"
6 Me.HD_TextBox2.ErrorMsg = "请输入合法的Email地址,比如 iHudan@GMail.com "
7 End Sub
2 Me.HD_TextBox1.Regex = "^(\(\d{3}\)|\d{3}-)?\d{8}$"
3 Me.HD_TextBox1.ErrorMsg = "请输入合法的电话号码,比如 021-64370000"
4
5 Me.HD_TextBox2.Regex = "^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$"
6 Me.HD_TextBox2.ErrorMsg = "请输入合法的Email地址,比如 iHudan@GMail.com "
7 End Sub
五一期间我会不断改进,欢迎大家批评指正.
祝大家节日快乐!