myvalidatingtextbox.vb

来自「vb 应用实例 简单应用 dddddddddddddddddddddddddd」· VB 代码 · 共 61 行

VB
61
字号
Imports System.Text.RegularExpressions

Public Class MyValidatingTextBox
    Inherits System.Windows.Forms.TextBox

    Protected _ErrorMessage As String
    Public Property ErrorMessage() As String
        Get
            Return _ErrorMessage
        End Get
        Set(ByVal Value As String)
            _ErrorMessage = Value
        End Set
    End Property

    Protected _ErrorColor As Color = Color.Red
    Public Property ErrorColor() As Color
        Get
            Return _ErrorColor
        End Get
        Set(ByVal Value As Color)
            _ErrorColor = Value
        End Set
    End Property

    Public ReadOnly Property IsValid() As Boolean
        Get
            If Not _Regex1 Is Nothing Then
                Return _Regex1.IsMatch(Me.Text)
            Else
                Return True
            End If
        End Get
    End Property

    Protected _ValidationExpression As String  
    Protected _Regex1 As Regex
    Public Property ValidationExpression() As String
        Get
            Return _ValidationExpression
        End Get
        Set(ByVal Value1 As String)
            If Value1 Is Nothing Then Value1 = "^\d{3}$"
            _Regex1 = New Regex(Value1)
            _ValidationExpression = Value1
        End Set
    End Property

    Protected Overrides Sub OnValidated(ByVal e As System.EventArgs)
        If Not Me.IsValid Then
            Me.ForeColor = _ErrorColor
        Else
            Me.ForeColor = Me.DefaultForeColor
        End If

        MyBase.OnValidated(e)
    End Sub


End Class

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?