惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

H
Help Net Security
博客园 - Franky
GbyAI
GbyAI
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
爱范儿
爱范儿
IT之家
IT之家
酷 壳 – CoolShell
酷 壳 – CoolShell
aimingoo的专栏
aimingoo的专栏
博客园_首页
MongoDB | Blog
MongoDB | Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Recent Announcements
Recent Announcements
Scott Helme
Scott Helme
有赞技术团队
有赞技术团队
M
MIT News - Artificial intelligence
C
CERT Recently Published Vulnerability Notes
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Jina AI
Jina AI
F
Fortinet All Blogs
N
Netflix TechBlog - Medium
L
LangChain Blog
L
LINUX DO - 最新话题
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
H
Hacker News: Front Page
MyScale Blog
MyScale Blog
P
Palo Alto Networks Blog
G
Google Developers Blog
Google DeepMind News
Google DeepMind News
AI
AI
T
Troy Hunt's Blog
Microsoft Azure Blog
Microsoft Azure Blog
阮一峰的网络日志
阮一峰的网络日志
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Vercel News
Vercel News
Microsoft Security Blog
Microsoft Security Blog
罗磊的独立博客
S
Secure Thoughts
大猫的无限游戏
大猫的无限游戏
博客园 - 叶小钗
人人都是产品经理
人人都是产品经理
Blog — PlanetScale
Blog — PlanetScale
博客园 - 司徒正美
Apple Machine Learning Research
Apple Machine Learning Research
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 三生石上(FineUI控件)
S
Security @ Cisco Blogs
Cloudbric
Cloudbric
E
Exploit-DB.com RSS Feed
Attack and Defense Labs
Attack and Defense Labs

博客园 - 回头重来

开发语言排名 释放C盘空间 窗体最小化后隐藏了再显示的小问题 用脚本修改数据库名和逻辑文件名 第一次尝试NHibernate winForm中使用log4net [转]SQL Server 管理技巧 [转]SQL Server开发技巧 [转]十二个笑话中的人生哲理 [转]Google Talk 的几点使用技巧 Google Talk Themes 需要GMail邀请的朋友请进来 将15位的身份证号码升级到18位的关键是校验码 让VBCommenter支持自定义用户名 简单的方向按钮 如何改变TextBox的高度 增强TextBox 在被调用函数中利用StackTrace确定是被哪个函数调用的 看图测试心理承受力(转载)
增强TextBox (可以对TextBox的内容进行自我验证的自定义控件)
回头重来 · 2005-05-01 · via 博客园 - 回头重来

这是我第一次写自定义控件,望各位批评指教.

控件从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 NothingThen
 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 ObjectByVal 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.ObjectByVal 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

五一期间我会不断改进,欢迎大家批评指正.

祝大家节日快乐!