IExtenderProvider 接口的应用.实现自定义组件LilyValidateProvider

今天在 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 方法设置该属性的值。请注意,扩展属性由 GetHelpTextSetHelpText 方法提供,并且 HelpLabel 不公开名为 HelpText 的属性。

    注:SetHelpTextGetHelpText 是成对出现的.
     这里有MSDN的一个例子:ms-help://MS.MSDNQTR.2003FEB.2052/cpguide/html/cpconwinformsextenderprovidersample.htm 很详细的.

这里有一个问题不清楚.方法的类型只能是基本数据类型吗?不能使用自定义类吗?如果要设置的方法比较多,这样就太麻烦?我试了老是出错,调试不能通过!

LilyValidateProvider组件的全部代码

ContractedBlock.gif ExpandedBlockStart.gif
None.gif    ''' -----------------------------------------------------------------------------
None.gif
    ''' Project     : Lily.WinUI
None.gif
    ''' Class     : WinUI.LilyValidateProvider
None.gif
    ''' 
None.gif
    ''' -----------------------------------------------------------------------------
None.gif
    ''' <summary>
None.gif
    '''  提供用于指示窗体上的控件输入验证功能
None.gif
    ''' </summary>
None.gif
    ''' <remarks>
None.gif
    ''' </remarks>
None.gif
    ''' <history>
None.gif
    '''     '''     [zqonline]    2006-08-28    Created
None.gif
    ''' </history>
None.gif
    ''' -----------------------------------------------------------------------------
None.gif
    <System.ComponentModel.ProvideProperty("ValidateType"GetType(Control)), _
None.gif     System.ComponentModel.ProvideProperty(
"ValidateMessage"GetType(Control)), _
None.gif     System.ComponentModel.ProvideProperty(
"ValidateExpression"GetType(Control)), _
None.gif     System.ComponentModel.ProvideProperty(
"ValidateIsNeed"GetType(Control))> _
ExpandedBlockStart.gifContractedBlock.gif    
Public Class LilyValidateProviderClass LilyValidateProvider
InBlock.gif        
Inherits System.ComponentModel.Component
InBlock.gif        
Implements System.ComponentModel.IExtenderProvider
InBlock.gif
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif
Windows 窗体设计器生成的代码#Region " Windows 窗体设计器生成的代码 "
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
Public Sub New()Sub New()
InBlock.gif            
MyBase.New()
InBlock.gif
InBlock.gif            
'该调用是 Windows 窗体设计器所必需的。
InBlock.gif
            InitializeComponent()
InBlock.gif
InBlock.gif            
'在 InitializeComponent() 调用之后添加任何初始化
InBlock.gif
            Me.md_hsValidateCtl = New Hashtable
InBlock.gif            
Me.md_hsErrorMessage = New Hashtable
InBlock.gif            
Me.md_hsRegularExpressions = New Hashtable
InBlock.gif            
Me.md_HsNeed = New Hashtable
InBlock.gif
ExpandedSubBlockEnd.gif        
End Sub

InBlock.gif
InBlock.gif        
'UserControl 重写 dispose 以清理组件列表。
ExpandedSubBlockStart.gifContractedSubBlock.gif
        Protected Overloads Overrides Sub Dispose()Sub Dispose(ByVal disposing As Boolean)
InBlock.gif            
If disposing Then
InBlock.gif                
If Not (components Is NothingThen
InBlock.gif                    components.Dispose()
InBlock.gif                
End If
InBlock.gif            
End If
InBlock.gif            
MyBase.Dispose(disposing)
ExpandedSubBlockEnd.gif        
End Sub

InBlock.gif
InBlock.gif        
'Windows 窗体设计器所必需的
InBlock.gif
        Private components As System.ComponentModel.IContainer
InBlock.gif
InBlock.gif        
'注意: 以下过程是 Windows 窗体设计器所必需的
InBlock.gif
        '可以使用 Windows 窗体设计器修改此过程。
InBlock.gif
        '不要使用代码编辑器修改它。
InBlock.gif
        Friend WithEvents Ep As System.Windows.Forms.ErrorProvider
ExpandedSubBlockStart.gifContractedSubBlock.gif        
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()Sub InitializeComponent()
InBlock.gif            
Me.Ep = New System.Windows.Forms.ErrorProvider
InBlock.gif
ExpandedSubBlockEnd.gif        
End Sub

InBlock.gif
ExpandedSubBlockEnd.gif
#End Region

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif
成员变量#Region "成员变量"
InBlock.gif        
'记录控件的检验类型
InBlock.gif
        Private md_hsValidateCtl As Hashtable
InBlock.gif        
'记录控件检验失败显示的提示信息
InBlock.gif
        Private md_hsErrorMessage As Hashtable
InBlock.gif        
'记录自定义验证的正则表达式
InBlock.gif
        Private md_hsRegularExpressions As Hashtable
InBlock.gif        
'记录此控件是否属于必填字段
InBlock.gif
        Private md_HsNeed As Hashtable
InBlock.gif
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})(\]?)$"
InBlock.gif        
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) "
InBlock.gif        
Private Const URL As String = "^(?<proto>\w+)://[^/]+?(?<port>:\d+)?/"
InBlock.gif        
Private Const NUMBER As String = "^(-?\d+)(\.\d+)?$"    '浮点数
InBlock.gif

ExpandedSubBlockEnd.gif
#End Region

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif
枚举定义#Region "枚举定义"
ExpandedSubBlockStart.gifContractedSubBlock.gif        
Public Enum MaskTypeEnum MaskType
InBlock.gif            NoMask 
= -1      '不起用控件验证
InBlock.gif
            [Text]           '文本
InBlock.gif
            Numeric          '数字
InBlock.gif
            Letter           '字母或数字
InBlock.gif
            [Decimal]        '数值
InBlock.gif
            DateTime         '日期或时间
InBlock.gif
            Email            '电子邮件
InBlock.gif
            URL              '网址
InBlock.gif
            IP               'IP地址
InBlock.gif
            Regular          '正常表达式
InBlock.gif
            LikeList         '判断字符是否在指定的范置内
InBlock.gif
            [Like]           '比较输入的字符
ExpandedSubBlockEnd.gif
        End Enum

ExpandedSubBlockEnd.gif
#End Region

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif
自定义事件#Region "自定义事件"
InBlock.gif
ExpandedSubBlockEnd.gif
#End Region

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif
自定义属性#Region "自定义属性"
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif
#End Region

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif
自定义方法#Region "自定义方法"
InBlock.gif
InBlock.gif        
''' -----------------------------------------------------------------------------
InBlock.gif
        ''' <summary>
InBlock.gif
        ''' 返回控件是否是必填字段
InBlock.gif
        ''' </summary>
InBlock.gif
        ''' <param name="ctrl"></param>
InBlock.gif
        ''' <returns></returns>
InBlock.gif
        ''' <remarks>
InBlock.gif
        ''' </remarks>
InBlock.gif
        ''' <history>
InBlock.gif
        '''     [zqonline]    2006-08-29    Created
InBlock.gif
        ''' </history>
InBlock.gif
        ''' -----------------------------------------------------------------------------
InBlock.gif
        <System.ComponentModel.DefaultValue(False)> _
ExpandedSubBlockStart.gifContractedSubBlock.gif        
Public Function GetValidateIsNeed()Function GetValidateIsNeed(ByVal ctrl As Control) As Boolean
InBlock.gif
InBlock.gif            
Dim bln As Boolean = CBool(Me.md_HsNeed(ctrl))
InBlock.gif
InBlock.gif            
Return bln
ExpandedSubBlockEnd.gif        
End Function

InBlock.gif
InBlock.gif        
''' -----------------------------------------------------------------------------
InBlock.gif
        ''' <summary>
InBlock.gif
        ''' 设置当前控件是否是必填字段
InBlock.gif
        ''' </summary>
InBlock.gif
        ''' <param name="ctrl"></param>
InBlock.gif
        ''' <param name="isNeed"></param>
InBlock.gif
        ''' <remarks>
InBlock.gif
        ''' </remarks>
InBlock.gif
        ''' <history>
InBlock.gif
        '''     [zqonline]    2006-08-29    Created
InBlock.gif
        ''' </history>
InBlock.gif
        ''' -----------------------------------------------------------------------------
ExpandedSubBlockStart.gifContractedSubBlock.gif
        Public Sub SetValidateIsNeed()Sub SetValidateIsNeed(ByVal ctrl As Control, ByVal isNeed As Boolean)
InBlock.gif            
'判断控件在集合中是否存在
InBlock.gif
            If Me.md_HsNeed.Contains(ctrl) Then
InBlock.gif                
If isNeed Then
InBlock.gif                    
Me.md_HsNeed(ctrl) = isNeed
InBlock.gif                
Else
InBlock.gif                    
Me.md_HsNeed.Remove(ctrl)
InBlock.gif                
End If
InBlock.gif
InBlock.gif            
Else
InBlock.gif                
'添加到集合
InBlock.gif
                Me.md_HsNeed.Add(ctrl, isNeed)
InBlock.gif
InBlock.gif                
'判断控件是否在验证集合中
InBlock.gif
                If Not Me.md_hsValidateCtl.Contains(ctrl) Then
InBlock.gif                    
Me.SetValidateType(ctrl, MaskType.Text)
InBlock.gif                
End If
InBlock.gif
InBlock.gif            
End If
InBlock.gif
ExpandedSubBlockEnd.gif        
End Sub

InBlock.gif
InBlock.gif        
''' -----------------------------------------------------------------------------
InBlock.gif
        ''' <summary>
InBlock.gif
        ''' 返回指定控件自定义正则表达式字符串
InBlock.gif
        ''' </summary>
InBlock.gif
        ''' <param name="ctrl"></param>
InBlock.gif
        ''' <returns></returns>
InBlock.gif
        ''' <remarks>
InBlock.gif
        ''' </remarks>
InBlock.gif
        ''' <history>
InBlock.gif
        '''     [zqonline]    2006-08-28    Created
InBlock.gif
        ''' </history>
InBlock.gif
        ''' -----------------------------------------------------------------------------
InBlock.gif
        <System.ComponentModel.DefaultValue("")> _
ExpandedSubBlockStart.gifContractedSubBlock.gif        
Public Function GetValidateExpression()Function GetValidateExpression(ByVal ctrl As Control) As String
InBlock.gif            
Dim strRegularExpression As String = CStr(Me.md_hsRegularExpressions(ctrl))
InBlock.gif            
If strRegularExpression Is Nothing Then
InBlock.gif                
Return String.Empty
InBlock.gif            
Else
InBlock.gif                
Return strRegularExpression
InBlock.gif            
End If
ExpandedSubBlockEnd.gif        
End Function

InBlock.gif
InBlock.gif        
''' -----------------------------------------------------------------------------
InBlock.gif
        ''' <summary>
InBlock.gif
        ''' 设置指定控件自定义正则表达式字符串
InBlock.gif
        ''' </summary>
InBlock.gif
        ''' <param name="ctrl"></param>
InBlock.gif
        ''' <param name="strRegularExpression"></param>
InBlock.gif
        ''' <remarks>
InBlock.gif
        ''' </remarks>
InBlock.gif
        ''' <history>
InBlock.gif
        '''     [zqonline]    2006-08-28    Created
InBlock.gif
        ''' </history>
InBlock.gif
        ''' -----------------------------------------------------------------------------
ExpandedSubBlockStart.gifContractedSubBlock.gif
        Public Sub SetValidateExpression()Sub SetValidateExpression(ByVal ctrl As Control, ByVal strRegularExpression As String)
InBlock.gif            
If strRegularExpression Is Nothing OrElse strRegularExpression.Length = 0 Then
InBlock.gif
InBlock.gif                
'判断控件是否存在
InBlock.gif
                If Not Me.md_hsRegularExpressions.Contains(ctrl) Then
InBlock.gif                    
Return
InBlock.gif                
Else
InBlock.gif
InBlock.gif                    
'复位验证掩码
InBlock.gif
                    '不验证
InBlock.gif
                    If Me.md_hsValidateCtl.Contains(ctrl) Then
InBlock.gif                        
Me.md_hsValidateCtl(ctrl) = Me.MaskType.NoMask
InBlock.gif                        
'从集合中移除
InBlock.gif
                        Me.md_hsValidateCtl.Remove(ctrl)
InBlock.gif                    
End If
InBlock.gif
InBlock.gif                    md_hsRegularExpressions.Remove(ctrl)
InBlock.gif
InBlock.gif                
End If
InBlock.gif
InBlock.gif            
Else
InBlock.gif
InBlock.gif                
'判断控件在集合中是否存在
InBlock.gif
                If Me.md_hsRegularExpressions.Contains(ctrl) Then
InBlock.gif                    
Me.md_hsRegularExpressions(ctrl) = strRegularExpression
InBlock.gif                
Else
InBlock.gif                    
'添加到集合
InBlock.gif
                    Me.md_hsRegularExpressions.Add(ctrl, strRegularExpression)
InBlock.gif                
End If
InBlock.gif
InBlock.gif            
End If
ExpandedSubBlockEnd.gif        
End Sub

InBlock.gif
InBlock.gif        
''' -----------------------------------------------------------------------------
InBlock.gif
        ''' <summary>
InBlock.gif
        ''' 返回指定控件校验失败显示的提示信息
InBlock.gif
        ''' </summary>
InBlock.gif
        ''' <param name="ctrl"></param>
InBlock.gif
        ''' <returns></returns>
InBlock.gif
        ''' <remarks>
InBlock.gif
        ''' </remarks>
InBlock.gif
        ''' <history>
InBlock.gif
        '''     [zqonline]    2006-08-28    Created
InBlock.gif
        ''' </history>
InBlock.gif
        ''' -----------------------------------------------------------------------------
InBlock.gif
        <System.ComponentModel.DefaultValue("")> _
ExpandedSubBlockStart.gifContractedSubBlock.gif        
Public Function GetValidateMessage()Function GetValidateMessage(ByVal ctrl As Control) As String
InBlock.gif            
Dim message As String = CStr(Me.md_hsErrorMessage(ctrl))
InBlock.gif            
If message Is Nothing Then
InBlock.gif                
Return String.Empty
InBlock.gif            
Else
InBlock.gif                
Return message
InBlock.gif            
End If
ExpandedSubBlockEnd.gif        
End Function

InBlock.gif
InBlock.gif        
''' -----------------------------------------------------------------------------
InBlock.gif
        ''' <summary>
InBlock.gif
        ''' 设置指定控件检验失败显示的提示信息
InBlock.gif
        ''' </summary>
InBlock.gif
        ''' <param name="ctrl"></param>
InBlock.gif
        ''' <param name="strErrorMessage"></param>
InBlock.gif
        ''' <remarks>
InBlock.gif
        ''' </remarks>
InBlock.gif
        ''' <history>
InBlock.gif
        '''     [zqonline]    2006-08-28    Created
InBlock.gif
        ''' </history>
InBlock.gif
        ''' -----------------------------------------------------------------------------
ExpandedSubBlockStart.gifContractedSubBlock.gif
        Public Sub SetValidateMessage()Sub SetValidateMessage(ByVal ctrl As Control, ByVal strErrorMessage As String)
InBlock.gif            
If strErrorMessage Is Nothing OrElse strErrorMessage.Length = 0 Then
InBlock.gif
InBlock.gif                
'判断控件是否存在
InBlock.gif
                If Not Me.md_hsErrorMessage.Contains(ctrl) Then
InBlock.gif                    
Return
InBlock.gif                
Else
InBlock.gif                    
'从集合中移除
InBlock.gif
                    Me.md_hsErrorMessage.Remove(ctrl)
InBlock.gif                
End If
InBlock.gif
InBlock.gif            
Else
InBlock.gif
InBlock.gif                
'判断控件在集合中是否存在
InBlock.gif
                If Me.md_hsErrorMessage.Contains(ctrl) Then
InBlock.gif                    
Me.md_hsErrorMessage(ctrl) = strErrorMessage
InBlock.gif                
Else
InBlock.gif                    
'添加到集合
InBlock.gif
                    Me.md_hsErrorMessage.Add(ctrl, strErrorMessage)
InBlock.gif                
End If
InBlock.gif            
End If
ExpandedSubBlockEnd.gif        
End Sub

InBlock.gif
InBlock.gif        
''' -----------------------------------------------------------------------------
InBlock.gif
        ''' <summary>
InBlock.gif
        ''' 返回指定的控件校验的类型
InBlock.gif
        ''' </summary>
InBlock.gif
        ''' <param name="ctrl"></param>
InBlock.gif
        ''' <returns></returns>
InBlock.gif
        ''' <remarks>
InBlock.gif
        ''' </remarks>
InBlock.gif
        ''' <history>
InBlock.gif
        '''     [zqonline]    2006-08-28    Created
InBlock.gif
        ''' </history>
InBlock.gif
        ''' -----------------------------------------------------------------------------
InBlock.gif
        <System.ComponentModel.DefaultValue(-1)> _
ExpandedSubBlockStart.gifContractedSubBlock.gif      
Public Function GetValidateType()Function GetValidateType(ByVal ctrl As Control) As MaskType
InBlock.gif            
Dim Value As Object = md_hsValidateCtl(ctrl)
InBlock.gif            
If Value Is Nothing Then
InBlock.gif                Value 
= -1
InBlock.gif            
End If
InBlock.gif
InBlock.gif            
Return Value
ExpandedSubBlockEnd.gif        
End Function

InBlock.gif
InBlock.gif        
''' -----------------------------------------------------------------------------
InBlock.gif
        ''' <summary>
InBlock.gif
        ''' 设置指定的控件要校验的类型
InBlock.gif
        ''' </summary>
InBlock.gif
        ''' <param name="ctrl"></param>
InBlock.gif
        ''' <param name="value"></param>
InBlock.gif
        ''' <remarks>
InBlock.gif
        ''' </remarks>
InBlock.gif
        ''' <history>
InBlock.gif
        '''     [zqonline]    2006-08-28    Created
InBlock.gif
        ''' </history>
InBlock.gif
        ''' -----------------------------------------------------------------------------
ExpandedSubBlockStart.gifContractedSubBlock.gif
        Public Sub SetValidateType()Sub SetValidateType(ByVal ctrl As Control, ByVal value As MaskType)
InBlock.gif            
Select Case value
InBlock.gif                
Case MaskType.NoMask
InBlock.gif
InBlock.gif                    
'移除事件
InBlock.gif
                    RemoveHandler ctrl.TextChanged, AddressOf TextChanged
InBlock.gif
InBlock.gif                    
'从控件验证集合中移除
InBlock.gif
                    Me.md_hsValidateCtl.Remove(ctrl)
InBlock.gif
InBlock.gif                    
'判断是否是必填项目集合中存在
InBlock.gif
                    If Me.md_HsNeed.Contains(ctrl) Then
InBlock.gif                        
Me.md_HsNeed.Remove(ctrl)
InBlock.gif                    
End If
InBlock.gif
InBlock.gif                
Case Else
InBlock.gif                    
If Me.md_hsValidateCtl.Contains(ctrl) Then
InBlock.gif                        
Me.md_hsValidateCtl(ctrl) = value
InBlock.gif                    
Else
InBlock.gif
InBlock.gif
InBlock.gif                        
'注册事件
InBlock.gif
                        AddHandler ctrl.TextChanged, AddressOf TextChanged
InBlock.gif
InBlock.gif                        
Me.md_hsValidateCtl.Add(ctrl, value)
InBlock.gif
InBlock.gif
InBlock.gif                    
End If
InBlock.gif            
End Select
InBlock.gif
ExpandedSubBlockEnd.gif        
End Sub

InBlock.gif
InBlock.gif        
'校验控件的值是否正确
ExpandedSubBlockStart.gifContractedSubBlock.gif
        Private Function ValidateData()Function ValidateData(ByVal ctrl As Control) As Boolean
InBlock.gif
InBlock.gif            
'验证是否通过的标志
InBlock.gif
            Dim blnSuccess As Boolean = True
InBlock.gif
InBlock.gif            
'控件不在HashTable中存在,验证通过
InBlock.gif
            If ctrl Is Nothing OrElse Not Me.md_hsValidateCtl.Contains(ctrl) Then
InBlock.gif                
Return True
InBlock.gif            
End If
InBlock.gif
InBlock.gif
InBlock.gif            
'当前控件的值
InBlock.gif
            Dim value As String = ctrl.Text
InBlock.gif            
'是否是必填字段
InBlock.gif
            Dim blnIsNeed As Boolean = False
InBlock.gif            
If Me.md_HsNeed.Contains(ctrl) AndAlso CBool(Me.md_HsNeed(ctrl)) Then
InBlock.gif                blnIsNeed 
= True
InBlock.gif            
End If
InBlock.gif
InBlock.gif
InBlock.gif            
'判断当前控件是否是必填字段,但又没有指定值,返回False.
InBlock.gif
            If blnIsNeed AndAlso value.Length = 0 Then
InBlock.gif                
Me.Ep.SetError(ctrl, "当前项目属于必填项目。")
InBlock.gif                
Return False
InBlock.gif            
Else
InBlock.gif
InBlock.gif                
'正则表达式
InBlock.gif
                Dim strPatronExpresion As String
InBlock.gif
InBlock.gif                
'获取当前控件的校验类型
InBlock.gif
                Dim Mask As MaskType = Me.md_hsValidateCtl(ctrl)
InBlock.gif
InBlock.gif
InBlock.gif                
Select Case Mask
InBlock.gif                    
Case MaskType.Regular
InBlock.gif                        
'使用正则表达式
InBlock.gif

InBlock.gif                        
If Me.md_hsRegularExpressions.Contains(ctrl) Then
InBlock.gif                            strPatronExpresion 
= Me.md_hsRegularExpressions(ctrl)
InBlock.gif                        
Else
InBlock.gif                            strPatronExpresion 
= ""
InBlock.gif                        
End If
InBlock.gif
InBlock.gif                    
Case MaskType.LikeList
InBlock.gif                        
'判断文本的字符是否都在指定字符范围
InBlock.gif

InBlock.gif                        
Dim strLike As String
InBlock.gif                        
If Me.md_hsRegularExpressions.Contains(ctrl) Then
InBlock.gif                            strLike 
= Me.md_hsRegularExpressions(ctrl)
InBlock.gif                        
End If
InBlock.gif
InBlock.gif                        
If strLike Is Nothing Then strLike = String.Empty
InBlock.gif
InBlock.gif                        
For i As Integer = 1 To value.Length
InBlock.gif                            
If Not Mid(value, i, 1) Like strLike Then
InBlock.gif                                blnSuccess 
= False
InBlock.gif                                
Exit For
InBlock.gif                            
End If
InBlock.gif                        
Next
InBlock.gif
InBlock.gif                    
Case MaskType.Like
InBlock.gif                        
'比较输入的字符
InBlock.gif

InBlock.gif                        
Dim strLike As String
InBlock.gif                        
If Me.md_hsRegularExpressions.Contains(ctrl) Then
InBlock.gif                            strLike 
= Me.md_hsRegularExpressions(ctrl)
InBlock.gif                        
End If
InBlock.gif
InBlock.gif                        
If strLike Is Nothing Then strLike = String.Empty
InBlock.gif
InBlock.gif                        blnSuccess 
= value Like strlike
InBlock.gif
InBlock.gif
InBlock.gif                    
Case MaskType.DateTime
InBlock.gif                        
'日期或时间
InBlock.gif

InBlock.gif                        blnSuccess 
= IsDate(value)
InBlock.gif
InBlock.gif                    
Case MaskType.Email
InBlock.gif                        
'电子邮件
InBlock.gif

InBlock.gif                        strPatronExpresion 
= Me.EMAIL
InBlock.gif
InBlock.gif                    
Case MaskType.IP
InBlock.gif                        
'IP地址
InBlock.gif

InBlock.gif                        strPatronExpresion 
= Me.IP
InBlock.gif
InBlock.gif                    
Case MaskType.Letter
InBlock.gif
InBlock.gif                        
'字母或数字
InBlock.gif
                        For i As Integer = 1 To value.Length
InBlock.gif                            
If Not Mid(value, i, 1) Like "[0-9A-Za-z]" Then
InBlock.gif                                blnSuccess 
= False
InBlock.gif                                
Exit For
InBlock.gif                            
End If
InBlock.gif                        
Next
InBlock.gif
InBlock.gif
InBlock.gif                    
Case MaskType.NoMask, MaskType.Text
InBlock.gif                        
'不需要校验
InBlock.gif

InBlock.gif                        blnSuccess 
= True
InBlock.gif
InBlock.gif                    
Case MaskType.Numeric
InBlock.gif                        
'数字
InBlock.gif

InBlock.gif                        
For i As Integer = 1 To value.Length
InBlock.gif                            
If Not Mid(value, i, 1) Like "[0-9]" Then
InBlock.gif                                blnSuccess 
= False
InBlock.gif                                
Exit For
InBlock.gif                            
End If
InBlock.gif                        
Next
InBlock.gif
InBlock.gif
InBlock.gif                    
Case MaskType.URL
InBlock.gif                        
'网址
InBlock.gif

InBlock.gif                        strPatronExpresion 
= Me.URL
InBlock.gif
InBlock.gif                    
Case MaskType.Decimal
InBlock.gif                        
'浮点数
InBlock.gif

InBlock.gif                        strPatronExpresion 
= Me.NUMBER
InBlock.gif
InBlock.gif                    
Case Else
InBlock.gif
InBlock.gif                        blnSuccess 
= True
InBlock.gif
InBlock.gif                
End Select
InBlock.gif
InBlock.gif                
'判断是否指定了正则表达式
InBlock.gif
                If Not strPatronExpresion Is Nothing AndAlso strPatronExpresion.Length <> 0 Then
InBlock.gif                    blnSuccess 
= System.Text.RegularExpressions.Regex.IsMatch(value, strPatronExpresion)
InBlock.gif                
End If
InBlock.gif
InBlock.gif                
If blnSuccess Then
InBlock.gif                    Ep.SetError(ctrl, 
String.Empty)
InBlock.gif                
Else
InBlock.gif                    
'判断是否指定了自定义指示信息
InBlock.gif
                    If Me.md_hsErrorMessage.Contains(ctrl) Then
InBlock.gif                        
Dim str As String = CStr(Me.md_hsErrorMessage(ctrl))
InBlock.gif                        Ep.SetError(ctrl, 
str)
InBlock.gif                    
Else
InBlock.gif                        Ep.SetError(ctrl, 
"输入无效,请重新输入。")
InBlock.gif                    
End If
InBlock.gif                
End If
InBlock.gif
InBlock.gif                
Return blnSuccess
InBlock.gif
InBlock.gif            
End If
ExpandedSubBlockEnd.gif        
End Function

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
Private Sub TextChanged()Sub TextChanged(ByVal sender As ObjectByVal e As System.EventArgs)
InBlock.gif
InBlock.gif            
'判断是否对需要对控件的内容校验
InBlock.gif
            If Not Me.md_hsValidateCtl.Contains(sender) Then
InBlock.gif                
Return
InBlock.gif            
Else
InBlock.gif
InBlock.gif                
'对控件内容进行校验
InBlock.gif
                Me.ValidateData(sender)
InBlock.gif
InBlock.gif            
End If
InBlock.gif
ExpandedSubBlockEnd.gif        
End Sub

InBlock.gif
InBlock.gif        
''' -----------------------------------------------------------------------------
InBlock.gif
        ''' <summary>
InBlock.gif
        ''' 判断控件是否都能通过验证
InBlock.gif
        ''' </summary>
InBlock.gif
        ''' <returns></returns>
InBlock.gif
        ''' <remarks>
InBlock.gif
        ''' </remarks>
InBlock.gif
        ''' <history>
InBlock.gif
        '''     [zqonline]    2006-08-29    Created
InBlock.gif
        ''' </history>
InBlock.gif
        ''' -----------------------------------------------------------------------------
ExpandedSubBlockStart.gifContractedSubBlock.gif
        Public Function IsValidate()Function IsValidate() As Boolean
InBlock.gif            
Dim blnSuccess As Boolean = True
InBlock.gif            
For Each ctrl As Control In Me.md_hsValidateCtl.Keys
InBlock.gif                
If Not Me.ValidateData(ctrl) Then
InBlock.gif                    blnSuccess 
= False
InBlock.gif                
End If
InBlock.gif            
Next
InBlock.gif
InBlock.gif            
Return blnSuccess
ExpandedSubBlockEnd.gif        
End Function

InBlock.gif
ExpandedSubBlockEnd.gif
#End Region

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif
IExtenderProvider 接口实现#Region "IExtenderProvider 接口实现"
ExpandedSubBlockStart.gifContractedSubBlock.gif        
Public Function CanExtend()Function CanExtend(ByVal extendee As ObjectAs Boolean Implements System.ComponentModel.IExtenderProvider.CanExtend
InBlock.gif            
If TypeOf extendee Is Control And Not TypeOf extendee Is LilyValidateProvider Then
InBlock.gif                
Return True
InBlock.gif            
Else
InBlock.gif                
Return False
InBlock.gif            
End If
ExpandedSubBlockEnd.gif        
End Function

ExpandedSubBlockEnd.gif
#End Region

InBlock.gif
ExpandedBlockEnd.gif    
End Class

None.gif





 

转载于:https://www.cnblogs.com/zqonline/archive/2006/08/29/488932.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值