今天在 www.codeproject.com 上看到一段源代码,如何验证文本框输入的字符是否在合法的范围之类,不过实际应用起来有点麻烦,只是针对了TextBox,虽然经过改进可以支持Comobox,不过使用起来不直接(我太懒了).
突然想到如果能使用ToolTip控件一样进行设置就好了.特别是当前窗体上需要进行验证的控件比较多时.
说干就干.通过MSDN发现Tootip具有接口IExtenderProvider.我想估计是这玩意.
IExtenderProvider介绍:
扩展程序提供程序是一个向其他组件提供属性的组件。例如,ToolTip 控件即是一个扩展程序提供程序。当向某个 Form 添加 ToolTip 控件时,将向该窗体上的所有其他控件的属性列表中添加 ToolTip 属性。
提供扩展程序属性的任何组件都必须实现 IExtenderProvider。然后,可视化设计器可以调用 CanExtend 来确定容器中的哪些对象应收到扩展程序属性。
我们发现只能要实现接口的方法CanExtend方法就能实现ToolTip的效果.
不过我们要注意以下几点(摘至MSDN)
- 扩展程序提供程序
HelpLabel
实现 。 HelpLabel
本身是 Windows 窗体控件,因而从 Control 派生。 - CanExtend 方法对于任何控件都返回“true”,但
HelpLabel
除外(原因是在属性自身上进行扩展是无意义的)。 HelpLabel
具有一个名为 GetHelpText
的方法,该方法获取 HelpLabel
使其可为其他控件使用的属性。SetHelpText
方法设置该属性的值。请注意,扩展属性由 GetHelpText
和 SetHelpText
方法提供,并且 HelpLabel
不公开名为 HelpText
的属性。
注:SetHelpText 与GetHelpText 是成对出现的.
这里有MSDN的一个例子:ms-help://MS.MSDNQTR.2003FEB.2052/cpguide/html/cpconwinformsextenderprovidersample.htm 很详细的.
这里有一个问题不清楚.方法的类型只能是基本数据类型吗?不能使用自定义类吗?如果要设置的方法比较多,这样就太麻烦?我试了老是出错,调试不能通过!
LilyValidateProvider组件的全部代码
''' -----------------------------------------------------------------------------
''' Project : Lily.WinUI
''' Class : WinUI.LilyValidateProvider
'''
''' -----------------------------------------------------------------------------
''' <summary>
''' 提供用于指示窗体上的控件输入验证功能
''' </summary>
''' <remarks>
''' </remarks>
''' <history>
''' ''' [zqonline] 2006-08-28 Created
''' </history>
''' -----------------------------------------------------------------------------
<System.ComponentModel.ProvideProperty("ValidateType", GetType(Control)), _
System.ComponentModel.ProvideProperty("ValidateMessage", GetType(Control)), _
System.ComponentModel.ProvideProperty("ValidateExpression", GetType(Control)), _
System.ComponentModel.ProvideProperty("ValidateIsNeed", GetType(Control))> _
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
Public Class LilyValidateProviderClass LilyValidateProvider
Inherits System.ComponentModel.Component
Implements System.ComponentModel.IExtenderProvider
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
Windows 窗体设计器生成的代码#Region " Windows 窗体设计器生成的代码 "
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Sub New()Sub New()
MyBase.New()
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
'在 InitializeComponent() 调用之后添加任何初始化
Me.md_hsValidateCtl = New Hashtable
Me.md_hsErrorMessage = New Hashtable
Me.md_hsRegularExpressions = New Hashtable
Me.md_HsNeed = New Hashtable
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
End Sub
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
'UserControl 重写 dispose 以清理组件列表。
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Protected Overloads Overrides Sub Dispose()Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
'注意: 以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
Friend WithEvents Ep As System.Windows.Forms.ErrorProvider
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()Sub InitializeComponent()
Me.Ep = New System.Windows.Forms.ErrorProvider
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
End Sub
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
#End Region
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
成员变量#Region "成员变量"
'记录控件的检验类型
Private md_hsValidateCtl As Hashtable
'记录控件检验失败显示的提示信息
Private md_hsErrorMessage As Hashtable
'记录自定义验证的正则表达式
Private md_hsRegularExpressions As Hashtable
'记录此控件是否属于必填字段
Private md_HsNeed As Hashtable
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
Private Const EMAIL As String = "^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"
Private Const IP As String = "(?<First>[01]?\d\d?|2[0-4]\d|25[0-5])\.(?<Second>[01]?\d\d?|2[0-4]\d|25[0-5])\.(?<Third>[01]?\d\d?|2[0-4]\d|25[0-5])\.(?<Fourth>[01]?\d\d?|2[0-4]\d|25[0-5])(?x) "
Private Const URL As String = "^(?<proto>\w+)://[^/]+?(?<port>:\d+)?/"
Private Const NUMBER As String = "^(-?\d+)(\.\d+)?$" '浮点数
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
#End Region
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
枚举定义#Region "枚举定义"
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Enum MaskTypeEnum MaskType
NoMask = -1 '不起用控件验证
[Text] '文本
Numeric '数字
Letter '字母或数字
[Decimal] '数值
DateTime '日期或时间
Email '电子邮件
URL '网址
IP 'IP地址
Regular '正常表达式
LikeList '判断字符是否在指定的范置内
[Like] '比较输入的字符
End Enum
#End Region
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
自定义事件#Region "自定义事件"
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
#End Region
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
自定义属性#Region "自定义属性"
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
#End Region
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
自定义方法#Region "自定义方法"
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
''' -----------------------------------------------------------------------------
''' <summary>
''' 返回控件是否是必填字段
''' </summary>
''' <param name="ctrl"></param>
''' <returns></returns>
''' <remarks>
''' </remarks>
''' <history>
''' [zqonline] 2006-08-29 Created
''' </history>
''' -----------------------------------------------------------------------------
<System.ComponentModel.DefaultValue(False)> _
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Function GetValidateIsNeed()Function GetValidateIsNeed(ByVal ctrl As Control) As Boolean
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
Dim bln As Boolean = CBool(Me.md_HsNeed(ctrl))
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
Return bln
End Function
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
''' -----------------------------------------------------------------------------
''' <summary>
''' 设置当前控件是否是必填字段
''' </summary>
''' <param name="ctrl"></param>
''' <param name="isNeed"></param>
''' <remarks>
''' </remarks>
''' <history>
''' [zqonline] 2006-08-29 Created
''' </history>
''' -----------------------------------------------------------------------------
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Sub SetValidateIsNeed()Sub SetValidateIsNeed(ByVal ctrl As Control, ByVal isNeed As Boolean)
'判断控件在集合中是否存在
If Me.md_HsNeed.Contains(ctrl) Then
If isNeed Then
Me.md_HsNeed(ctrl) = isNeed
Else
Me.md_HsNeed.Remove(ctrl)
End If
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
Else
'添加到集合
Me.md_HsNeed.Add(ctrl, isNeed)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
'判断控件是否在验证集合中
If Not Me.md_hsValidateCtl.Contains(ctrl) Then
Me.SetValidateType(ctrl, MaskType.Text)
End If
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
End If
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
End Sub
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
''' -----------------------------------------------------------------------------
''' <summary>
''' 返回指定控件自定义正则表达式字符串
''' </summary>
''' <param name="ctrl"></param>
''' <returns></returns>
''' <remarks>
''' </remarks>
''' <history>
''' [zqonline] 2006-08-28 Created
''' </history>
''' -----------------------------------------------------------------------------
<System.ComponentModel.DefaultValue("")> _
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Function GetValidateExpression()Function GetValidateExpression(ByVal ctrl As Control) As String
Dim strRegularExpression As String = CStr(Me.md_hsRegularExpressions(ctrl))
If strRegularExpression Is Nothing Then
Return String.Empty
Else
Return strRegularExpression
End If
End Function
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
''' -----------------------------------------------------------------------------
''' <summary>
''' 设置指定控件自定义正则表达式字符串
''' </summary>
''' <param name="ctrl"></param>
''' <param name="strRegularExpression"></param>
''' <remarks>
''' </remarks>
''' <history>
''' [zqonline] 2006-08-28 Created
''' </history>
''' -----------------------------------------------------------------------------
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Sub SetValidateExpression()Sub SetValidateExpression(ByVal ctrl As Control, ByVal strRegularExpression As String)
If strRegularExpression Is Nothing OrElse strRegularExpression.Length = 0 Then
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
'判断控件是否存在
If Not Me.md_hsRegularExpressions.Contains(ctrl) Then
Return
Else
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
'复位验证掩码
'不验证
If Me.md_hsValidateCtl.Contains(ctrl) Then
Me.md_hsValidateCtl(ctrl) = Me.MaskType.NoMask
'从集合中移除
Me.md_hsValidateCtl.Remove(ctrl)
End If
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
md_hsRegularExpressions.Remove(ctrl)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
End If
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
Else
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
'判断控件在集合中是否存在
If Me.md_hsRegularExpressions.Contains(ctrl) Then
Me.md_hsRegularExpressions(ctrl) = strRegularExpression
Else
'添加到集合
Me.md_hsRegularExpressions.Add(ctrl, strRegularExpression)
End If
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
End If
End Sub
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
''' -----------------------------------------------------------------------------
''' <summary>
''' 返回指定控件校验失败显示的提示信息
''' </summary>
''' <param name="ctrl"></param>
''' <returns></returns>
''' <remarks>
''' </remarks>
''' <history>
''' [zqonline] 2006-08-28 Created
''' </history>
''' -----------------------------------------------------------------------------
<System.ComponentModel.DefaultValue("")> _
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Function GetValidateMessage()Function GetValidateMessage(ByVal ctrl As Control) As String
Dim message As String = CStr(Me.md_hsErrorMessage(ctrl))
If message Is Nothing Then
Return String.Empty
Else
Return message
End If
End Function
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
''' -----------------------------------------------------------------------------
''' <summary>
''' 设置指定控件检验失败显示的提示信息
''' </summary>
''' <param name="ctrl"></param>
''' <param name="strErrorMessage"></param>
''' <remarks>
''' </remarks>
''' <history>
''' [zqonline] 2006-08-28 Created
''' </history>
''' -----------------------------------------------------------------------------
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Sub SetValidateMessage()Sub SetValidateMessage(ByVal ctrl As Control, ByVal strErrorMessage As String)
If strErrorMessage Is Nothing OrElse strErrorMessage.Length = 0 Then
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
'判断控件是否存在
If Not Me.md_hsErrorMessage.Contains(ctrl) Then
Return
Else
'从集合中移除
Me.md_hsErrorMessage.Remove(ctrl)
End If
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
Else
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
'判断控件在集合中是否存在
If Me.md_hsErrorMessage.Contains(ctrl) Then
Me.md_hsErrorMessage(ctrl) = strErrorMessage
Else
'添加到集合
Me.md_hsErrorMessage.Add(ctrl, strErrorMessage)
End If
End If
End Sub
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
''' -----------------------------------------------------------------------------
''' <summary>
''' 返回指定的控件校验的类型
''' </summary>
''' <param name="ctrl"></param>
''' <returns></returns>
''' <remarks>
''' </remarks>
''' <history>
''' [zqonline] 2006-08-28 Created
''' </history>
''' -----------------------------------------------------------------------------
<System.ComponentModel.DefaultValue(-1)> _
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Function GetValidateType()Function GetValidateType(ByVal ctrl As Control) As MaskType
Dim Value As Object = md_hsValidateCtl(ctrl)
If Value Is Nothing Then
Value = -1
End If
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
Return Value
End Function
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
''' -----------------------------------------------------------------------------
''' <summary>
''' 设置指定的控件要校验的类型
''' </summary>
''' <param name="ctrl"></param>
''' <param name="value"></param>
''' <remarks>
''' </remarks>
''' <history>
''' [zqonline] 2006-08-28 Created
''' </history>
''' -----------------------------------------------------------------------------
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Sub SetValidateType()Sub SetValidateType(ByVal ctrl As Control, ByVal value As MaskType)
Select Case value
Case MaskType.NoMask
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
'移除事件
RemoveHandler ctrl.TextChanged, AddressOf TextChanged
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
'从控件验证集合中移除
Me.md_hsValidateCtl.Remove(ctrl)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
'判断是否是必填项目集合中存在
If Me.md_HsNeed.Contains(ctrl) Then
Me.md_HsNeed.Remove(ctrl)
End If
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
Case Else
If Me.md_hsValidateCtl.Contains(ctrl) Then
Me.md_hsValidateCtl(ctrl) = value
Else
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
'注册事件
AddHandler ctrl.TextChanged, AddressOf TextChanged
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
Me.md_hsValidateCtl.Add(ctrl, value)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
End If
End Select
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
End Sub
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
'校验控件的值是否正确
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Private Function ValidateData()Function ValidateData(ByVal ctrl As Control) As Boolean
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
'验证是否通过的标志
Dim blnSuccess As Boolean = True
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
'控件不在HashTable中存在,验证通过
If ctrl Is Nothing OrElse Not Me.md_hsValidateCtl.Contains(ctrl) Then
Return True
End If
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
'当前控件的值
Dim value As String = ctrl.Text
'是否是必填字段
Dim blnIsNeed As Boolean = False
If Me.md_HsNeed.Contains(ctrl) AndAlso CBool(Me.md_HsNeed(ctrl)) Then
blnIsNeed = True
End If
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
'判断当前控件是否是必填字段,但又没有指定值,返回False.
If blnIsNeed AndAlso value.Length = 0 Then
Me.Ep.SetError(ctrl, "当前项目属于必填项目。")
Return False
Else
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
'正则表达式
Dim strPatronExpresion As String
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
'获取当前控件的校验类型
Dim Mask As MaskType = Me.md_hsValidateCtl(ctrl)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
Select Case Mask
Case MaskType.Regular
'使用正则表达式
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
If Me.md_hsRegularExpressions.Contains(ctrl) Then
strPatronExpresion = Me.md_hsRegularExpressions(ctrl)
Else
strPatronExpresion = ""
End If
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
Case MaskType.LikeList
'判断文本的字符是否都在指定字符范围
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
Dim strLike As String
If Me.md_hsRegularExpressions.Contains(ctrl) Then
strLike = Me.md_hsRegularExpressions(ctrl)
End If
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
If strLike Is Nothing Then strLike = String.Empty
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
For i As Integer = 1 To value.Length
If Not Mid(value, i, 1) Like strLike Then
blnSuccess = False
Exit For
End If
Next
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
Case MaskType.Like
'比较输入的字符
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
Dim strLike As String
If Me.md_hsRegularExpressions.Contains(ctrl) Then
strLike = Me.md_hsRegularExpressions(ctrl)
End If
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
If strLike Is Nothing Then strLike = String.Empty
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
blnSuccess = value Like strlike
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
Case MaskType.DateTime
'日期或时间
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
blnSuccess = IsDate(value)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
Case MaskType.Email
'电子邮件
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
strPatronExpresion = Me.EMAIL
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
Case MaskType.IP
'IP地址
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
strPatronExpresion = Me.IP
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
Case MaskType.Letter
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
'字母或数字
For i As Integer = 1 To value.Length
If Not Mid(value, i, 1) Like "[0-9A-Za-z]" Then
blnSuccess = False
Exit For
End If
Next
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
Case MaskType.NoMask, MaskType.Text
'不需要校验
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
blnSuccess = True
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
Case MaskType.Numeric
'数字
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
For i As Integer = 1 To value.Length
If Not Mid(value, i, 1) Like "[0-9]" Then
blnSuccess = False
Exit For
End If
Next
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
Case MaskType.URL
'网址
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
strPatronExpresion = Me.URL
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
Case MaskType.Decimal
'浮点数
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
strPatronExpresion = Me.NUMBER
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
Case Else
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
blnSuccess = True
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
End Select
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
'判断是否指定了正则表达式
If Not strPatronExpresion Is Nothing AndAlso strPatronExpresion.Length <> 0 Then
blnSuccess = System.Text.RegularExpressions.Regex.IsMatch(value, strPatronExpresion)
End If
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
If blnSuccess Then
Ep.SetError(ctrl, String.Empty)
Else
'判断是否指定了自定义指示信息
If Me.md_hsErrorMessage.Contains(ctrl) Then
Dim str As String = CStr(Me.md_hsErrorMessage(ctrl))
Ep.SetError(ctrl, str)
Else
Ep.SetError(ctrl, "输入无效,请重新输入。")
End If
End If
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
Return blnSuccess
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
End If
End Function
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Private Sub TextChanged()Sub TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
'判断是否对需要对控件的内容校验
If Not Me.md_hsValidateCtl.Contains(sender) Then
Return
Else
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
'对控件内容进行校验
Me.ValidateData(sender)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
End If
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
End Sub
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
''' -----------------------------------------------------------------------------
''' <summary>
''' 判断控件是否都能通过验证
''' </summary>
''' <returns></returns>
''' <remarks>
''' </remarks>
''' <history>
''' [zqonline] 2006-08-29 Created
''' </history>
''' -----------------------------------------------------------------------------
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Function IsValidate()Function IsValidate() As Boolean
Dim blnSuccess As Boolean = True
For Each ctrl As Control In Me.md_hsValidateCtl.Keys
If Not Me.ValidateData(ctrl) Then
blnSuccess = False
End If
Next
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
Return blnSuccess
End Function
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
#End Region
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
IExtenderProvider 接口实现#Region "IExtenderProvider 接口实现"
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Function CanExtend()Function CanExtend(ByVal extendee As Object) As Boolean Implements System.ComponentModel.IExtenderProvider.CanExtend
If TypeOf extendee Is Control And Not TypeOf extendee Is LilyValidateProvider Then
Return True
Else
Return False
End If
End Function
#End Region
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
End Class